X-Git-Url: https://git.armaanb.net/?a=blobdiff_plain;f=lightcards%2Fdisplay.py;h=5ecb14c0d984d98351efbf393d4bde71bb9aa0ae;hb=a32f80e459c2fa88cfde61fd80d1a8543cc51e8b;hp=2b4a51fa868a0e6acc388ead17f7c0bd551fe18d;hpb=eab52aa7f3cc154f25e578dc66de522259eacbd2;p=lightcards.git diff --git a/lightcards/display.py b/lightcards/display.py index 2b4a51f..5ecb14c 100644 --- a/lightcards/display.py +++ b/lightcards/display.py @@ -20,13 +20,6 @@ def panel_create(x, y): return (win, panel) -class CursesError(BaseException): - def __init__(self, message="lightcards: Curses error!"): - self.message = message - print(self.message) - sys.exit(3) - - class Quit: def __init__(self, outer, mlines=5, mcols=20): self.outer = outer @@ -207,11 +200,12 @@ class Menu(Panel): shuffle(self.outer.stack) self.menu_print("Stack shuffled!") elif key in self.outer.config["menu_open_file"]: - self.outer.dump() curses.endwin() os.system(f"$EDITOR {self.outer.input_file}"), (self.outer.headers, self.outer.stack) = parse.parse_html( - parse.md2html(self.outer.input_file) + parse.md2html(self.outer.input_file), + self.outer.args, + self.outer.config, ) self.outer.get_key() elif key in self.outer.config["menu_stars_only"]: @@ -254,8 +248,9 @@ class Display: self.headers = headers self.obj = obj self.view = view - self.input_file = args.inp[0] + self.input_file = args.inp self.config = conf + self.args = args def run(self, stdscr): """Set important options that require stdscr before starting""" @@ -308,60 +303,63 @@ class Display: """Get total number of starred cards""" return [card for card in self.stack if card.starred] - def disp_bar(self): - """ - Display the statusbar at the bottom of the screen with progress, star - status, and card side. - """ - (mlines, mcols) = self.win.getmaxyx() - self.win.hline(mlines - 3, 0, 0, mcols) - + def prep_bar(self): # Calculate percent done if len(self.stack) <= 1: percent = "100" else: percent = str( round(self.obj.index / (len(self.stack) - 1) * 100) - ).zfill(2) + ).zfill(3) # Print yellow if starred if self.current_card().starred: - star_color = curses.color_pair(3) + self.star_color = curses.color_pair(3) else: - star_color = curses.color_pair(1) + self.star_color = curses.color_pair(1) # Compose bar text - bar_start = "[" - bar_middle = self.current_card().printStar() - bar_end = f"] [{len(self.nstarred())}/{str(len(self.stack))} starred] " + self.bar_start = "[" + self.bar_middle = self.current_card().printStar() + self.bar_end = ( + f"] [{len(self.nstarred())}/{str(len(self.stack))} starred] " + ) if self.view != 3: - bar_end += ( + self.bar_end += ( f" [{self.get_side()} (" f"{str(int(self.current_card().side) + 1)})]" ) - bar_end += f" [View {str(self.view)}]" + self.bar_end += f" [View {str(self.view)}]" + self.progress = ( + f"[{percent}% (" + f"{str(self.obj.index + 1).zfill(len(str(len(self.stack))))}" + f"/{str(len(self.stack))})] " + ) + + def disp_bar(self): + """ + Display the statusbar at the bottom of the screen with progress, star + status, and card side. + """ # Put it all togethor + (mlines, mcols) = self.win.getmaxyx() height = mlines - 2 - self.win.addstr(height, 0, bar_start, curses.color_pair(1)) - self.win.addstr(height, len(bar_start), bar_middle, star_color) + self.win.addstr(height, 0, self.bar_start, curses.color_pair(1)) + self.win.addstr( + height, len(self.bar_start), self.bar_middle, self.star_color + ) self.win.addstr( height, - len(bar_start + bar_middle), - textwrap.shorten(bar_end, width=mcols - 20, placeholder="…"), + len(self.bar_start + self.bar_middle), + textwrap.shorten(self.bar_end, width=mcols - 20, placeholder="…"), curses.color_pair(1), ) - progress = ( - f"[{percent}% (" - f"{str(self.obj.index + 1).zfill(len(str(len(self.stack))))}" - f"/{str(len(self.stack))})] " - ) - self.win.addstr( height + 1, 0, - progress, + self.progress, curses.color_pair(1), ) @@ -369,17 +367,19 @@ class Display: int( self.obj.index / (len(self.stack) - 1) - * (mcols - len(progress)) + * (mcols - len(self.progress)) - 1 ) ): self.win.addch( height + 1, - i + len(progress), + i + len(self.progress), self.config["progress_char"], curses.color_pair(1), ) + self.win.hline(mlines - 3, 0, 0, mcols) + def wrap_width(self): """Calculate the width at which the body text should wrap""" (_, mcols) = self.win.getmaxyx() @@ -397,7 +397,7 @@ class Display: def disp_card(self): (_, mcols) = self.win.getmaxyx() self.main_panel.bottom() - self.win.clear() + self.prep_bar() num_done = str(self.obj.index + 1).zfill(len(str(len(self.stack)))) if self.view in [1, 2, 4]: @@ -425,6 +425,7 @@ class Display: + '"' ) + self.win.clear() self.win.addstr( 0, 0, @@ -446,10 +447,7 @@ class Display: """ Display the contents of the card with both the front and back sides. """ - (_, mcols) = self.win.getmaxyx() - self.main_panel.bottom() self.win.clear() - self.win.addstr( 0, 0, @@ -477,8 +475,8 @@ class Display: ) self.win.hline(1, 0, curses.ACS_HLINE, mcols) - self.disp_bar() self.disp_sidebar() + self.disp_bar() def current_card(self): """Get current card object""" @@ -546,13 +544,13 @@ class Display: nstarred = self.nstarred() for i, card in enumerate(nstarred): - term = card.get()[self.obj.side] + term = card.get(smart=False)[0] if len(term) > 18: term = term[:18] + "…" if i > mlines - 6: for i in range(19): - self.win.addch(mlines - 3, left + i, " ") + self.win.addch(mlines - 4, left + i, " ") self.win.addstr( mlines - 4,