|
| 1 | +import curses |
| 2 | +import curses.ascii |
| 3 | +import os |
| 4 | +from math import isclose |
| 5 | +from operator import attrgetter |
| 6 | +from typing import Tuple, List |
| 7 | + |
| 8 | +from podcaster import Podcast, Episode, AudioPlayer |
| 9 | +from podcaster.utils import date2str, time2str |
| 10 | + |
| 11 | + |
| 12 | +class Keys: |
| 13 | + L = ord("l") |
| 14 | + K = ord("k") |
| 15 | + J = ord("j") |
| 16 | + H = ord("h") |
| 17 | + Q = ord("q") |
| 18 | + P = ord("p") |
| 19 | + ENTER = ord("\n") |
| 20 | + SPACE = ord(" ") |
| 21 | + SQUARE_BRACKET_LEFT = ord("[") |
| 22 | + SQUARE_BRACKET_RIGHT = ord("]") |
| 23 | + LEFT = 260 |
| 24 | + RIGHT = 261 |
| 25 | + UP = 259 |
| 26 | + DOWN = 258 |
| 27 | + ESC = 27 |
| 28 | + |
| 29 | + |
| 30 | +class GUI: |
| 31 | + BACK = "Back" |
| 32 | + QUIT = "Quit" |
| 33 | + SEPARATOR = "---" |
| 34 | + |
| 35 | + def __init__(self, podcasts: Tuple[Podcast]): |
| 36 | + self.podcasts = sorted(podcasts, key=attrgetter("date"))[::-1] |
| 37 | + os.environ['ESCDELAY'] = '0' # disable delayed registration of the Escape key by curses |
| 38 | + curses.wrapper(self.run) |
| 39 | + |
| 40 | + def run(self, screen): |
| 41 | + curses.curs_set(0) |
| 42 | + curses.noecho() |
| 43 | + if curses.has_colors(): |
| 44 | + curses.start_color() |
| 45 | + curses.use_default_colors() |
| 46 | + self.screen = screen |
| 47 | + self.height, self.width = self.screen.getmaxyx() |
| 48 | + os.environ.setdefault('ESCDELAY', '25') |
| 49 | + self.select_podcast() |
| 50 | + |
| 51 | + def draw_title(self, title: str): |
| 52 | + self.screen.addnstr(0, 0, title, self.width, curses.A_BOLD) |
| 53 | + self.screen.noutrefresh() |
| 54 | + |
| 55 | + def select_podcast(self): |
| 56 | + self.draw_title("Select podcast:") |
| 57 | + |
| 58 | + podcast_strings = [f"{date2str(podcast.date)} - {podcast.title}" for podcast in self.podcasts] |
| 59 | + selection_entries = podcast_strings + [self.SEPARATOR, self.QUIT] |
| 60 | + podcast_selector = SelectionPad((1, 0), (self.height, self.width), selection_entries) |
| 61 | + |
| 62 | + while True: |
| 63 | + selected_index = podcast_selector.run() |
| 64 | + self.screen.clear() |
| 65 | + |
| 66 | + selected_entry = selection_entries[selected_index] |
| 67 | + if selected_entry == self.QUIT: |
| 68 | + return |
| 69 | + selected_podcast = self.podcasts[selected_index] |
| 70 | + |
| 71 | + ret = self.select_episodes(selected_podcast) |
| 72 | + if ret == self.BACK: |
| 73 | + continue |
| 74 | + if ret == self.QUIT: |
| 75 | + return |
| 76 | + |
| 77 | + def select_episodes(self, podcast: Podcast): |
| 78 | + self.draw_title("Select episode:") |
| 79 | + |
| 80 | + episode_strings = [f"{date2str(episode.date)} - {episode.title}" for episode in podcast.episodes] |
| 81 | + selection_entries = episode_strings + [self.SEPARATOR, self.BACK, self.QUIT] |
| 82 | + episode_selector = SelectionPad((1, 0), (self.height, self.width), selection_entries) |
| 83 | + |
| 84 | + while True: |
| 85 | + selected_index = episode_selector.run() |
| 86 | + self.screen.clear() |
| 87 | + |
| 88 | + selected_entry = selection_entries[selected_index] |
| 89 | + if selected_entry == self.BACK: |
| 90 | + return self.BACK |
| 91 | + if selected_entry == self.QUIT: |
| 92 | + return self.QUIT |
| 93 | + episode = podcast.episodes[selected_index] |
| 94 | + |
| 95 | + ret = self.play(podcast, episode) |
| 96 | + if ret == self.QUIT: |
| 97 | + return self.QUIT |
| 98 | + |
| 99 | + def play(self, podcast: Podcast, episode: Episode): |
| 100 | + self.draw_title(f"{podcast.title}") |
| 101 | + player = EpisodePlayWindow((1, 0), (self.height, self.width), episode) |
| 102 | + return player.run() |
| 103 | + |
| 104 | + |
| 105 | +class SelectionPad: |
| 106 | + def __init__(self, top_left: Tuple[int, int], bottom_right: Tuple[int, int], entry_list: List[str]): |
| 107 | + self.entries = entry_list |
| 108 | + self.width = bottom_right[1] - top_left[1] |
| 109 | + self.win_height = bottom_right[0] - top_left[0] |
| 110 | + self.top_left = top_left |
| 111 | + self.bottom_right = bottom_right |
| 112 | + |
| 113 | + self.has_back_option = GUI.BACK in entry_list |
| 114 | + |
| 115 | + self.pad = curses.newpad(len(entry_list), self.width) |
| 116 | + self.pad.keypad(True) |
| 117 | + self.current_index = 0 |
| 118 | + self.item_offset = 0 |
| 119 | + |
| 120 | + def run(self): |
| 121 | + self.draw() |
| 122 | + while True: |
| 123 | + c = self.pad.getch() |
| 124 | + if c in (curses.KEY_DOWN, ord('j')): |
| 125 | + if self.current_index == len(self.entries) - 1: |
| 126 | + self.current_index = 0 |
| 127 | + elif self.entries[self.current_index + 1] == GUI.SEPARATOR: |
| 128 | + self.current_index += 2 |
| 129 | + else: |
| 130 | + self.current_index += 1 |
| 131 | + elif c in (Keys.K, Keys.UP): |
| 132 | + if self.current_index == 0: |
| 133 | + self.current_index = len(self.entries) - 1 |
| 134 | + elif self.entries[self.current_index - 1] == GUI.SEPARATOR: |
| 135 | + self.current_index -= 2 |
| 136 | + else: |
| 137 | + self.current_index -= 1 |
| 138 | + elif self.has_back_option and c in (Keys.ESC, Keys.H): |
| 139 | + return self.entries.index(GUI.BACK) |
| 140 | + elif c in (Keys.Q, Keys.ESC): |
| 141 | + return self.entries.index(GUI.QUIT) |
| 142 | + elif c in (Keys.ENTER, Keys.L): |
| 143 | + return self.current_index |
| 144 | + self.draw() |
| 145 | + |
| 146 | + def draw(self): |
| 147 | + self.pad.erase() |
| 148 | + |
| 149 | + if self.current_index >= self.win_height + self.item_offset: |
| 150 | + # scroll down |
| 151 | + self.item_offset = self.current_index - self.win_height + 1 |
| 152 | + elif self.current_index == self.item_offset - 1: |
| 153 | + # scroll up |
| 154 | + self.item_offset -= 1 |
| 155 | + elif self.item_offset == 0 and self.current_index == len(self.entries) - 1: |
| 156 | + # scroll past top |
| 157 | + self.item_offset = len(self.entries) - 1 - self.win_height |
| 158 | + elif self.current_index == 0 and self.item_offset > 0: |
| 159 | + self.item_offset = 0 |
| 160 | + |
| 161 | + for i, entry in enumerate(self.entries): |
| 162 | + mode = curses.A_REVERSE if i == self.current_index else curses.A_NORMAL |
| 163 | + self.pad.addnstr(i, 0, entry, self.width, mode) |
| 164 | + |
| 165 | + # noinspection PyArgumentList |
| 166 | + self.pad.noutrefresh(self.item_offset, 0, *self.top_left, self.win_height, self.width) |
| 167 | + curses.doupdate() |
| 168 | + |
| 169 | + |
| 170 | +class EpisodePlayWindow: |
| 171 | + def __init__(self, top_left: Tuple[int, int], bottom_right: Tuple[int, int], episode: Episode): |
| 172 | + self.episode = episode |
| 173 | + self.player = AudioPlayer() |
| 174 | + |
| 175 | + self.width = bottom_right[1] - top_left[1] |
| 176 | + self.win_height = bottom_right[0] - top_left[0] |
| 177 | + self.window = curses.newwin(self.win_height, self.width, *top_left) |
| 178 | + self.window.keypad(True) |
| 179 | + |
| 180 | + def run(self): |
| 181 | + self.draw() |
| 182 | + |
| 183 | + self.player.play(self.episode.url) |
| 184 | + |
| 185 | + curses.halfdelay(3) |
| 186 | + while True: |
| 187 | + c = self.window.getch() |
| 188 | + if c in (Keys.P, Keys.SPACE): |
| 189 | + self.player.pause_toggle() |
| 190 | + elif c in (Keys.Q, Keys.ESC): |
| 191 | + self.player.quit() |
| 192 | + return GUI.QUIT |
| 193 | + elif c in (Keys.LEFT, Keys.H): |
| 194 | + self.player.backward() |
| 195 | + elif c in (Keys.RIGHT, Keys.L): |
| 196 | + self.player.forward() |
| 197 | + elif c in (Keys.UP, Keys.K): |
| 198 | + self.player.volume_up() |
| 199 | + elif c in (Keys.DOWN, Keys.J): |
| 200 | + self.player.volume_down() |
| 201 | + elif c in (Keys.SQUARE_BRACKET_LEFT,): |
| 202 | + self.player.speed_down() |
| 203 | + elif c in (Keys.SQUARE_BRACKET_RIGHT,): |
| 204 | + self.player.speed_up() |
| 205 | + |
| 206 | + self.draw() |
| 207 | + |
| 208 | + def draw_header(self): |
| 209 | + self.window.addnstr(0, 0, self.episode.title, self.width, curses.A_BOLD) |
| 210 | + |
| 211 | + def draw(self): |
| 212 | + self.window.erase() |
| 213 | + self.draw_header() |
| 214 | + |
| 215 | + if self.player.time is None: |
| 216 | + progress_str = "Loading..." |
| 217 | + else: |
| 218 | + progress_str = f"{time2str(self.player.time)} / {time2str(self.player.duration)}" |
| 219 | + |
| 220 | + if self.player.is_paused: |
| 221 | + progress_str += " (paused)" |
| 222 | + |
| 223 | + speed_str = "" if isclose(self.player.speed, 1) else f"Speed: {self.player.speed:.2f}x" |
| 224 | + volume_string = "" if isclose(self.player.volume, 100) else f"Volume: {int(self.player.volume)} %" |
| 225 | + status_string = ", ".join([s for s in [volume_string, speed_str] if s]) |
| 226 | + |
| 227 | + self.window.addnstr(2, 0, progress_str, self.width) |
| 228 | + self.window.addnstr(3, 0, status_string, self.width) |
| 229 | + self.window.noutrefresh() |
| 230 | + curses.doupdate() |
0 commit comments