def run(self): """主交互逻辑 (key event loop)""" while True: self.display() k = getch.getch() if self.state != 1: # 歌词模式下除了方向键都可以用 # getch will return multiple ASCII codes for arrow keys # A, B, C, D are the first code of UP, DOWN, LEFT, RIGHT if k == self.KEYS["UP"] or k == "A": self.updown(-1) elif k == self.KEYS["DOWN"] or k == "B": self.updown(1) elif k == self.KEYS["TOP"]: # g键返回顶部 self.markline = 0 self.topline = 0 elif k == self.KEYS["BOTTOM"]: # G键返回底部 self.markline = self.screen_height self.topline = len(self.lines) - self.screen_height - 1 if k == self.KEYS["HELP"]: # help界面 self.state = 2 Help(self) elif k == self.KEYS["LRC"]: # o歌词 self.set_lrc() self.state = 1 self.thread(self.display_lrc) elif k == "e" and self.state == 0: self.state = 3 History(self) elif k == self.KEYS["RATE"]: # r标记红心/取消标记 self.thread(self.set_rate) elif k == self.KEYS["NEXT"]: # n下一首 self.set_next() elif k == " ": # 空格选择频道,播放歌曲 if self.markline + self.topline != self.displayline: self.displaysong() self.set_channel() elif k == self.KEYS["OPENURL"]: # l打开当前播放歌曲豆瓣页 self.set_url() elif k == self.KEYS["BYE"]: # b不再播放 self.set_bye() elif k == self.KEYS["PAUSE"]: # p暂停 self.pause() elif k == self.KEYS["MUTE"]: # m静音 self.mute() elif k == self.KEYS["LOOP"]: # l单曲循环 self.set_loop() elif k == self.KEYS["QUIT"]: # q退出程序 if self.state == 0: self.state = 4 Quit(self) else: self.state = 0 self.display() elif k == "=" or k == "+": # 提高音量 self.change_volume(1) elif k == "-" or k == "_": # 降低音量 self.change_volume(-1) elif k in ["1", "2", "3", "4"]: cli.Cli.c = config.get_default_theme(THEME[int(k) - 1]) self.reload_theme()
def run(self): '''主交互逻辑 (key event loop)''' while True: self.display() k = getch.getch() if self.state != 1: # 歌词模式下除了方向键都可以用 # getch will return multiple ASCII codes for arrow keys # A, B, C, D are the first code of UP, DOWN, LEFT, RIGHT if k == self.KEYS['UP'] or k == 'A': self.updown(-1) elif k == self.KEYS['DOWN'] or k == 'B': self.updown(1) elif k == self.KEYS['TOP']: # g键返回顶部 self.markline = 0 self.topline = 0 elif k == self.KEYS['BOTTOM']: # G键返回底部 self.markline = self.screen_height self.topline = len(self.lines) - self.screen_height - 1 if k == self.KEYS['HELP']: # help界面 self.state = 2 Help(self) elif k == self.KEYS['LRC']: # o歌词 self.set_lrc() self.state = 1 self.thread(self.display_lrc) elif k == 'e' and self.state == 0: self.state = 3 History(self) elif k == self.KEYS['RATE']: # r标记红心/取消标记 self.thread(self.set_rate) elif k == self.KEYS['NEXT']: # n下一首 self.set_next() elif k == ' ': # 空格选择频道,播放歌曲 if self.markline + self.topline != self.displayline: self.displaysong() self.set_channel() elif k == self.KEYS['OPENURL']: # l打开当前播放歌曲豆瓣页 self.set_url() elif k == self.KEYS['BYE']: # b不再播放 self.set_bye() elif k == self.KEYS['PAUSE']: # p暂停 self.pause() elif k == self.KEYS['MUTE']: # m静音 self.mute() elif k == self.KEYS['LOOP']: # l单曲循环 self.set_loop() elif k == self.KEYS['QUIT']: # q退出程序 if self.state == 0: self.state = 4 Quit(self) else: self.state = 0 self.display() elif k == '=' or k == '+': # 提高音量 self.change_volume(1) elif k == '-' or k == '_': # 降低音量 self.change_volume(-1) elif k in ['1', '2', '3', '4']: cli.Cli.c = config.get_default_theme(THEME[int(k) - 1]) self.reload_theme()
light_green = basic_color('92') light_yellow = basic_color('93') light_blue = basic_color('94') light_magenta = basic_color('95') light_cyan = basic_color('96') white = basic_color('97') ############################## # 16 basic colors on background on_default = basic_color('49') on_black = basic_color('40') on_red = basic_color('41') on_green = basic_color('42') on_yellow = basic_color('43') on_blue = basic_color('44') on_magenta = basic_color('45') on_cyan = basic_color('46') on_grey = basic_color('100') on_light_red = basic_color('101') on_light_green = basic_color('102') on_light_yellow = basic_color('103') on_light_blue = basic_color('104') on_light_magenta = basic_color('105') on_light_cyan = basic_color('106') on_white = basic_color('107') THEME = ['default', 'larapaste', 'monokai', 'tomorrow'] c = config.get_default_theme(THEME[0])
class Cli(object): THEME = ['default', 'larapaste', 'monokai', 'tomorrow'] c = config.get_default_theme(THEME[0]) PREFIX_SELECTED = color_func(c['LINE']['arrow'])(' > ') # 箭头所指行前缀 LOVE = color_func(c['PLAYINGSONG']['like'])(' ❤ ', 'red') PREFIX_DESELECTED = ' ' SUFFIX_SELECTED = '' # 空格标记行后缀 SUFFIX_DESELECTED = '' TITLE = PREFIX_DESELECTED # 标题 def __init__(self, lines): self.lines = lines self.markline = 0 # 箭头行 初始化设置默认频道 self.topline = 0 # lines self.displayline = self.markline # 初始化歌曲信息显示行 self.screen_height, self.screen_width = self.linesnum() # 屏幕显示行数 subprocess.call('echo "\033[?25l"', shell=True) # 取消光标 def linesnum(self): '''测试屏幕显示行数,每行字符数''' num = subprocess.check_output('stty size', shell=True) tmp = num.split(' ') return int(tmp[0]) - 4, int(tmp[1]) # -4上下空余 def display(self): '''展示窗口''' self.screen_height, self.screen_width = self.linesnum() # 屏幕显示行数 subprocess.call('clear', shell=True) # 清屏 print print self.TITLE top = self.topline bottom = self.topline + self.screen_height + 1 for index, i in enumerate(self.lines[top:bottom]): # 箭头指向 if index == self.markline: prefix = self.PREFIX_SELECTED i = color_func(self.c['LINE']['highlight'])(i) else: prefix = self.PREFIX_DESELECTED # 选择频道 if index + self.topline == self.displayline: suffix = self.SUFFIX_SELECTED else: suffix = self.SUFFIX_DESELECTED line = '%s %s %s' % (prefix, i, suffix) line = color_func(self.c['LINE']['line'])(line) print line + '\r' # 为什么加\r,我不知道,如果不加会出bug def displaysong(self): '''显示歌曲的行号''' self.displayline = self.markline + self.topline def run(self): '''界面执行程序''' while True: self.display() c = getch.getch() if c == 'k': self.updown(-1) if c == 'j': self.updown(1) if c == 'q': break def updown(self, increment): '''对上下键进行反应,调成page和scroll''' # paging if increment == -1 and self.markline == 0 and self.topline != 0: self.topline -= 1 elif increment == 1 and self.markline + self.topline != len( self.lines) - 1 and self.markline == self.screen_height: self.topline += 1 # scroll if increment == -1 and self.markline != 0: self.markline -= 1 elif increment == 1 and self.markline != self.screen_height and self.markline < len( self.lines) - 1: self.markline += 1 def is_cn_char(self, i): '''判断中文字符''' return 0x4e00 <= ord(i) < 0x9fa6 def center_num(self, tmp): ''' 考虑英文和中文在终端上所占字块 ''' l = 0 for i in tmp: l += 2 if self.is_cn_char(i) else 1 return l
grey = basic_color('90') light_red = basic_color('91') light_green = basic_color('92') light_yellow = basic_color('93') light_blue = basic_color('94') light_magenta = basic_color('95') light_cyan = basic_color('96') white = basic_color('97') """ 16 basic colors on background """ on_default = basic_color('49') on_black = basic_color('40') on_red = basic_color('41') on_green = basic_color('42') on_yellow = basic_color('43') on_blue = basic_color('44') on_magenta = basic_color('45') on_cyan = basic_color('46') on_grey = basic_color('100') on_light_red = basic_color('101') on_light_green = basic_color('102') on_light_yellow = basic_color('103') on_light_blue = basic_color('104') on_light_magenta = basic_color('105') on_light_cyan = basic_color('106') on_white = basic_color('107') c = config.get_default_theme()