Added controls to the playlist window

This commit is contained in:
GuardKenzie
2021-05-13 01:04:41 +00:00
parent d8adba0a1a
commit a0c9ae3ea6

View File

@ -38,7 +38,10 @@ default_bindings = {">": "next_track",
"p": "play_pause",
"q": "quit",
"h": "help",
"i": "toggle_info"
"i": "toggle_info",
"down": "select_down",
"up": "select_up",
"enter": "select"
}
if "keybindings" not in config.sections():
@ -134,6 +137,7 @@ class Player:
# Curses initialisation
self.stdscr = curses.initscr()
self.stdscr.nodelay(True)
self.stdscr.keypad(True)
# Curses config
curses.noecho()
@ -205,6 +209,9 @@ class Player:
# Flag to check if any music has been played
self.has_music_been_played = False
# Selected song in playlist
self.selected_song = 0
def fitText(self):
"""
@ -423,7 +430,14 @@ class Player:
'h' -- Help
"""
anytime_keys = ["quit", "help"]
anytime_keys = ["quit", "help", "select_up", "select_down", "select"]
special_key_map = {curses.KEY_UP: "up",
curses.KEY_DOWN: "down",
curses.KEY_LEFT: "left",
curses.KEY_RIGHT: "right",
10: "enter"
}
if self.checkSongUpdate() == 1:
stopped = True
@ -435,6 +449,9 @@ class Player:
while key > 0:
# Resolve every key in buffer
if key in special_key_map.keys():
keyChar = special_key_map[key]
else:
keyChar = chr(key).lower()
# Parse key
@ -478,6 +495,19 @@ class Player:
self.toggleInfo()
self.update_needed = True
elif action == "select_up":
self.selected_song -= 1
self.update_needed = True
elif action == "select_down":
self.selected_song += 1
self.update_needed = True
elif action == "select":
self.client.play(self.selected_song % len(self.client.playlist()))
self.update_needed = True
key = self.stdscr.getch()
def drawInfo(self):
@ -529,16 +559,25 @@ class Player:
self.art_win.refresh()
# Draw playlist
if self.draw_playlist:
playlist = self.client.playlistinfo()
currentsong = self.client.currentsong()
currentpos = int(currentsong["pos"])
def drawPlaylist(self):
"""
A function that draws the playlist
"""
# Draw playlist
if not self.draw_playlist:
return
playlist = self.client.playlistinfo()
current_song = self.client.currentsong()
# selected_pos = int(current_song["pos"])
selected_pos = self.selected_song % len(playlist)
# Determine where to start the playlist
if currentpos > self.playlist_window_height // 2 and len(playlist) > self.playlist_window_height:
start = currentpos - (self.playlist_window_height - 1) // 2
if selected_pos > self.playlist_window_height // 2 and len(playlist) > self.playlist_window_height:
start = selected_pos - (self.playlist_window_height - 1) // 2
else:
start = 0
@ -553,17 +592,20 @@ class Player:
playlist_item = None
# Decide color
if playlist_item == currentsong:
pair = curses.A_REVERSE | curses.color_pair(2)
else:
pair = 0
if playlist_item == current_song:
pair = curses.color_pair(2)
if playlist_item == playlist[selected_pos]:
pair = curses.color_pair(2) | curses.A_REVERSE
# Move and write text
self.playlist_win.move(line, 0)
if playlist_item is not None:
self.playlist_win.addstr(
f"{playlist_item['artist']} - {playlist_item['title']}"[:self.playlist_window_width],
f"{playlist_item['artist']} - {playlist_item['title']}"[:self.playlist_window_width - 1],
pair
)
@ -688,6 +730,7 @@ class Player:
self.art_win.addstr(self.art_window_height // 2, (self.art_window_width - len(infomsg)) // 2, infomsg)
self.art_win.refresh()
self.drawPlaylist()
return
@ -698,6 +741,7 @@ class Player:
self.drawInfo()
self.drawAlbumArt()
self.drawPlaylist()
@ueberzug.Canvas()