Added controls to the playlist window
This commit is contained in:
		| @@ -38,7 +38,10 @@ default_bindings = {">": "next_track", | |||||||
|                     "p":     "play_pause", |                     "p":     "play_pause", | ||||||
|                     "q":     "quit", |                     "q":     "quit", | ||||||
|                     "h":     "help", |                     "h":     "help", | ||||||
|                     "i": "toggle_info" |                     "i":     "toggle_info", | ||||||
|  |                     "down":  "select_down", | ||||||
|  |                     "up":    "select_up", | ||||||
|  |                     "enter": "select" | ||||||
|                     } |                     } | ||||||
|  |  | ||||||
| if "keybindings" not in config.sections(): | if "keybindings" not in config.sections(): | ||||||
| @@ -134,6 +137,7 @@ class Player: | |||||||
|         # Curses initialisation |         # Curses initialisation | ||||||
|         self.stdscr = curses.initscr() |         self.stdscr = curses.initscr() | ||||||
|         self.stdscr.nodelay(True) |         self.stdscr.nodelay(True) | ||||||
|  |         self.stdscr.keypad(True) | ||||||
|  |  | ||||||
|         # Curses config |         # Curses config | ||||||
|         curses.noecho() |         curses.noecho() | ||||||
| @@ -205,6 +209,9 @@ class Player: | |||||||
|         # Flag to check if any music has been played |         # Flag to check if any music has been played | ||||||
|         self.has_music_been_played = False |         self.has_music_been_played = False | ||||||
|  |  | ||||||
|  |         # Selected song in playlist | ||||||
|  |         self.selected_song = 0 | ||||||
|  |  | ||||||
|  |  | ||||||
|     def fitText(self): |     def fitText(self): | ||||||
|         """ |         """ | ||||||
| @@ -423,7 +430,14 @@ class Player: | |||||||
|             'h' -- Help |             '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: |         if self.checkSongUpdate() == 1: | ||||||
|             stopped = True |             stopped = True | ||||||
| @@ -435,6 +449,9 @@ class Player: | |||||||
|  |  | ||||||
|         while key > 0: |         while key > 0: | ||||||
|             # Resolve every key in buffer |             # Resolve every key in buffer | ||||||
|  |             if key in special_key_map.keys(): | ||||||
|  |                 keyChar = special_key_map[key] | ||||||
|  |             else: | ||||||
|                 keyChar = chr(key).lower() |                 keyChar = chr(key).lower() | ||||||
|  |  | ||||||
|             # Parse key |             # Parse key | ||||||
| @@ -478,6 +495,19 @@ class Player: | |||||||
|                 self.toggleInfo() |                 self.toggleInfo() | ||||||
|                 self.update_needed = True |                 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() |             key = self.stdscr.getch() | ||||||
|  |  | ||||||
|     def drawInfo(self): |     def drawInfo(self): | ||||||
| @@ -529,16 +559,25 @@ class Player: | |||||||
|  |  | ||||||
|         self.art_win.refresh() |         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 |         # Determine where to start the playlist | ||||||
|             if currentpos > self.playlist_window_height // 2 and len(playlist) > self.playlist_window_height: |         if selected_pos > self.playlist_window_height // 2 and len(playlist) > self.playlist_window_height: | ||||||
|                 start = currentpos - (self.playlist_window_height - 1) // 2 |             start = selected_pos - (self.playlist_window_height - 1) // 2 | ||||||
|         else: |         else: | ||||||
|             start = 0 |             start = 0 | ||||||
|  |  | ||||||
| @@ -553,17 +592,20 @@ class Player: | |||||||
|                 playlist_item = None |                 playlist_item = None | ||||||
|  |  | ||||||
|             # Decide color |             # Decide color | ||||||
|                 if playlist_item == currentsong: |  | ||||||
|                     pair = curses.A_REVERSE | curses.color_pair(2) |  | ||||||
|                 else: |  | ||||||
|             pair = 0 |             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 |             # Move and write text | ||||||
|             self.playlist_win.move(line, 0) |             self.playlist_win.move(line, 0) | ||||||
|  |  | ||||||
|             if playlist_item is not None: |             if playlist_item is not None: | ||||||
|                 self.playlist_win.addstr( |                 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 |                     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.addstr(self.art_window_height // 2, (self.art_window_width - len(infomsg)) // 2, infomsg) | ||||||
|             self.art_win.refresh() |             self.art_win.refresh() | ||||||
|  |             self.drawPlaylist() | ||||||
|  |  | ||||||
|             return |             return | ||||||
|  |  | ||||||
| @@ -698,6 +741,7 @@ class Player: | |||||||
|             self.drawInfo() |             self.drawInfo() | ||||||
|  |  | ||||||
|         self.drawAlbumArt() |         self.drawAlbumArt() | ||||||
|  |         self.drawPlaylist() | ||||||
|  |  | ||||||
|  |  | ||||||
|     @ueberzug.Canvas() |     @ueberzug.Canvas() | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user