def display(self): '''输出界面''' self.screen_height, self.screen_width = self.linesnum() # 屏幕显示行数 subprocess.call('clear') print print self.win.TITLE print for linenum in range(self.screen_height - 2): if self.screen_height/2 - linenum > self.markline - self.topline or \ linenum - self.screen_height/2 >= len(self.lines) - self.markline: print '\r' else: line = self.lines[self.markline - (self.screen_height / 2 - linenum)] line = line.strip() l = self.center_num(line) flag_num = (self.screen_width - l) / 2 if linenum == self.screen_height / 2: i = color_func(self.c['LRC']['highlight'])(line) print ' ' * flag_num + i + '\r' else: line = color_func(self.c['LRC']['line'])(line) print ' ' * flag_num + line + '\r' print '\r' # 歌曲信息居中 song = self.win.playingsong tmp = (song['title'] + song['albumtitle'] + song['artist'] + song['public_time']).replace('\\', '').strip() l = self.center_num(tmp) if song['like']: l += 2 flag_num = (self.screen_width - l) / 2 self.title = ' ' * flag_num + self.win.SUFFIX_SELECTED + '\r' # 歌词页面标题 print self.title
def __init__(self, douban): # 线程锁 self.lock_start = False # 播放锁,play之前需要加 self.lock_rate = False # 加心锁 self.lock_loop = False # 循环锁 self.lock_muted = False # 静音锁 self.lock_pause = True # 暂停锁 self.q = False # 退出 self.songtime = 0 # 歌曲时间 self.playingsong = None # 当前播放歌曲 # state 0 1 2 3 4 # main lrc help history quit self.state = 0 self.history = db_config.history self.douban = douban # default volume self._volume = douban.login_data['volume'] # player controler self._player_exit_event = threading.Event() self.player = player.MPlayer(self._player_exit_event, self._volume) # 快捷键配置 self.KEYS = db_config.keys # 桌面通知 self.noti = notification.Notify() # 存储歌曲信息 self.lines = self.douban.channels self._channel = self.douban.login_data['channel'] self.playingsong = None self.playlist = None self.find_lrc = False self.lrc_dict = {} # 歌词 super(Win, self).__init__(self.lines) self.TITLE += color_func(self.c['TITLE']['doubanfm'])(' Douban Fm ') self.TITLE += '\ ' + \ color_func(self.c['TITLE']['username'])(self.douban.login_data['user_name']) + \ ' >>\r' # 启动自动播放 self.markline = self.displayline = self._channel self.lock_start = True self.SUFFIX_SELECTED = '正在加载请稍后...' self.display() self.thread(self.play) # 播放控制 self.thread(self.watchdog) # 播放器守护线程 self.thread(self.display_time) # 时间显示 self.run()
def reload_theme(self): # 箭头所指行前缀 cli.Cli.c = db_config.theme cli.Cli.PREFIX_SELECTED = color_func(self.c['LINE']['arrow'])(' > ') cli.Cli.LOVE = color_func(self.c['PLAYINGSONG']['like'])(' ❤ ', 'red') self.TITLE = cli.Cli.TITLE + color_func(self.c['TITLE']['doubanfm'])(' Douban Fm ') self.TITLE += '\ ' + \ color_func(self.c['TITLE']['username'])(self.douban.login_data['user_name']) + \ ' >>' self.set_suffix_selected(self.playingsong)
def reload_theme(self): # 箭头所指行前缀 cli.Cli.c = db_config.theme cli.Cli.PREFIX_SELECTED = color_func(self.c['LINE']['arrow'])(' > ') cli.Cli.LOVE = color_func(self.c['PLAYINGSONG']['like'])(' ❤ ', 'red') self.TITLE = cli.Cli.TITLE + color_func( self.c['TITLE']['doubanfm'])(' Douban Fm ') self.TITLE += '\ ' + \ color_func(self.c['TITLE']['username'])(self.douban.login_data['user_name']) + \ ' >>' self.set_suffix_selected(self.playingsong)
def set_suffix_selected(self, song): if song['like'] == 1: love = self.LOVE else: love = '' title = color_func(self.c['PLAYINGSONG']['title'])(song['title']) albumtitle = color_func(self.c['PLAYINGSONG']['albumtitle'])( song['albumtitle']) artist = color_func(self.c['PLAYINGSONG']['artist'])(song['artist']) public_time = color_func(self.c['PLAYINGSONG']['publictime'])( song['public_time']) or '' self.SUFFIX_SELECTED = (love + title + ' • ' + albumtitle + ' • ' + artist + ' ' + public_time).replace('\\', '')
def get_lines(self): """因为历史列表动态更新,需要刷新""" self.lines = [] width = self.screen_width - 24 if self.state == 0: # 播放列表 for index, i in enumerate(self.win.playlist): line = i['title'] if len( i['title']) < width else i['title'][:width] line = color_func(self.c['PLAYINGSONG']['title'])(line) line = str(index) + ' ' + line if i['like'] == 1: line += self.LOVE if i == self.win.playingsong: line += self.play_tag self.lines.append(line) elif self.state == 1: # 历史列表 for index, i in enumerate(self.win.history): line = i['title'] if len( i['title']) < width else i['title'][:width] line = color_func(self.c['PLAYINGSONG']['title'])(line) line = i['time'][5:] + ' ' + line if i['like'] == 1: line += self.LOVE if i == self.win.playingsong: line += self.play_tag self.lines.append(line) elif self.state == 2: # 红心列表 self.rate = [] for i in reversed(self.win.history): if i['like'] == 1: if i in self.rate: self.rate.remove(i) self.rate.insert(0, i) else: self.rate.insert(0, i) for index, i in enumerate(self.rate): line = i['title'] if len( i['title']) < width else i['title'][:width] line = color_func(self.c['PLAYINGSONG']['title'])(line) line = str(index) + ' ' + line + self.LOVE if i == self.win.playingsong: line += self.play_tag self.lines.append(line) self.lines.insert(0, self.subtitle[self.state])
def set_suffix_selected(self, song): if song['like'] == 1: love = self.LOVE else: love = '' title = color_func(self.c['PLAYINGSONG']['title'])(song['title']) albumtitle = color_func(self.c['PLAYINGSONG']['albumtitle'])(song['albumtitle']) artist = color_func(self.c['PLAYINGSONG']['artist'])(song['artist']) public_time = color_func(self.c['PLAYINGSONG']['publictime'])(song['public_time']) or '' self.SUFFIX_SELECTED = ( love + title + ' • ' + albumtitle + ' • ' + artist + ' ' + public_time ).replace('\\', '')
def get_lines(self): """因为历史列表动态更新,需要刷新""" self.lines = [] width = self.screen_width - 24 if self.state == 0: # 播放列表 for index, i in enumerate(self.win.playlist): line = i['title'] if len(i['title']) < width else i['title'][:width] line = color_func(self.c['PLAYINGSONG']['title'])(line) line = str(index) + ' ' + line if i['like'] == 1: line += self.LOVE if i == self.win.playingsong: line += self.play_tag self.lines.append(line) elif self.state == 1: # 历史列表 for index, i in enumerate(self.win.history): line = i['title'] if len(i['title']) < width else i['title'][:width] line = color_func(self.c['PLAYINGSONG']['title'])(line) line = i['time'][5:] + ' ' + line if i['like'] == 1: line += self.LOVE if i == self.win.playingsong: line += self.play_tag self.lines.append(line) elif self.state == 2: # 红心列表 self.rate = [] for i in reversed(self.win.history): if i['like'] == 1: if i in self.rate: self.rate.remove(i) self.rate.insert(0, i) else: self.rate.insert(0, i) for index, i in enumerate(self.rate): line = i['title'] if len(i['title']) < width else i['title'][:width] line = color_func(self.c['PLAYINGSONG']['title'])(line) line = str(index) + ' ' + line + self.LOVE if i == self.win.playingsong: line += self.play_tag self.lines.append(line) self.lines.insert(0, self.subtitle[self.state])
def display(self): '''输出界面''' self.screen_height, self.screen_width = self.linesnum() # 屏幕显示行数 subprocess.call('clear') print print self.win.TITLE print for linenum in range(self.screen_height - 2): if self.screen_height/2 - linenum > self.markline - self.topline or \ linenum - self.screen_height/2 >= len(self.lines) - self.markline: print '\r' else: line = self.lines[self.markline - (self.screen_height/2 - linenum)] line = line.strip() l = self.center_num(line) flag_num = (self.screen_width - l) / 2 if linenum == self.screen_height/2: i = color_func(self.c['LRC']['highlight'])(line) print ' ' * flag_num + i + '\r' else: line = color_func(self.c['LRC']['line'])(line) print ' ' * flag_num + line + '\r' print '\r' # 歌曲信息居中 song = self.win.playingsong tmp = ( song['title'] + song['albumtitle'] + song['artist'] + song['public_time'] ).replace('\\', '').strip() l = self.center_num(tmp) if song['like']: l += 2 flag_num = (self.screen_width - l) / 2 self.title = ' ' * flag_num + self.win.SUFFIX_SELECTED + '\r' # 歌词页面标题 print self.title
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 display_time(self): '''时间/音量显示线程''' length = len(self.TITLE) rest_time = 0 while not self.q: if self.lock_pause or self.lock_start: time.sleep(1) continue if self.player.is_alive: songtime = self.player.time_pos if songtime: self.songtime = songtime # 181s -> 03:01 rest_time = int(self.playingsong['length']) - self.songtime - 1 minute = int(rest_time) / 60 sec = int(rest_time) % 60 show_time = str(minute).zfill(2) + ':' + str(sec).zfill(2) title_pro = '' if self.playingsong['kbps'] == '64' else self.PRO title_kbps = self.playingsong['kbps'] + 'kbps' title_time = show_time title_rate = self.RATE[ int(round(self.playingsong['rating_avg'])) - 1] title_vol = '✖' if self.lock_muted else str(self._volume) + '%' title_loop = '↺' if self.lock_loop else '→' title = [ color_func(self.c['TITLE']['pro'])(title_pro), color_func(self.c['TITLE']['kbps'])(title_kbps), color_func(self.c['TITLE']['time'])(title_time), color_func(self.c['TITLE']['rate'])(title_rate), color_func(self.c['TITLE']['vol'])(title_vol), color_func(self.c['TITLE']['state'])(title_loop) ] self.TITLE = \ self.TITLE[:length - 1] + ' ' + ' '.join(title) + '\r' else: self.TITLE = self.TITLE[:length] self.display() time.sleep(1)
def display_time(self): '''时间/音量显示线程''' length = len(self.TITLE) rest_time = 0 while not self.q: if self.lock_pause or self.lock_start: time.sleep(1) continue if self.player.is_alive: songtime = self.player.time_pos if songtime: self.songtime = songtime # 181s -> 03:01 rest_time = int(self.playingsong['length']) - self.songtime - 1 minute = int(rest_time) / 60 sec = int(rest_time) % 60 show_time = str(minute).zfill(2) + ':' + str(sec).zfill(2) title_pro = '' if self.playingsong['kbps'] == '64' else self.PRO title_kbps = self.playingsong['kbps'] + 'kbps' title_time = show_time title_rate = self.RATE[int(round(self.playingsong['rating_avg'])) - 1] title_vol = '✖' if self.lock_muted else str(self._volume) + '%' title_loop = '↺' if self.lock_loop else '→' title = [ color_func(self.c['TITLE']['pro'])(title_pro), color_func(self.c['TITLE']['kbps'])(title_kbps), color_func(self.c['TITLE']['time'])(title_time), color_func(self.c['TITLE']['rate'])(title_rate), color_func(self.c['TITLE']['vol'])(title_vol), color_func(self.c['TITLE']['state'])(title_loop) ] self.TITLE = \ self.TITLE[:length - 1] + ' ' + ' '.join(title) + '\r' else: self.TITLE = self.TITLE[:length] self.display() time.sleep(1)
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