From 01fe4c5f344e355fa9b4ceb032757032b90d3680 Mon Sep 17 00:00:00 2001 From: Armaan Bhojwani Date: Fri, 12 Feb 2021 11:16:14 -0500 Subject: [PATCH] Add preliminary views support --- lightcards/display.py | 133 +++++++++++++++++++++++++++--------------- man/lightcards.1 | 3 + man/lightcards.1.md | 3 + 3 files changed, 91 insertions(+), 48 deletions(-) diff --git a/lightcards/display.py b/lightcards/display.py index d0c2420..0e07f9d 100644 --- a/lightcards/display.py +++ b/lightcards/display.py @@ -20,7 +20,7 @@ def panel_create(x, y): class Help: - def __init__(self, outer, mlines=20, mcols=52): + def __init__(self, outer, mlines=21, mcols=52): """Initialize help screen""" self.outer = outer (self.win, self.panel) = panel_create(mlines, mcols) @@ -40,6 +40,7 @@ class Help: "H, ? open this screen", "e open the input file in $EDITOR", "m open the control menu", + "1, 2 switch views", "", "More information can be found in the man page, or", "by running `lightcards --help`.", @@ -63,7 +64,7 @@ class Help: def disp(self): """Display help screen""" (mlines, mcols) = self.outer.win.getmaxyx() - self.win.mvwin(int(mlines / 2) - 10, int(mcols / 2) - 25) + self.win.mvwin(int(mlines / 2) - 11, int(mcols / 2) - 25) self.panel.show() while True: @@ -116,9 +117,6 @@ class Menu: else: color = curses.color_pair(1) - for i in range(42): - self.win.addch(15, i + 1, " ") - self.win.addstr(15, 1, string, color) self.menu_grab() @@ -182,6 +180,9 @@ class Menu: Display a menu offering multiple options on how to manipulate the deck and to continue """ + for i in range(42): + self.win.addch(15, i + 1, " ") + (mlines, mcols) = self.outer.win.getmaxyx() self.win.mvwin(int(mlines / 2) - 9, int(mcols / 2) - 21) self.panel.show() @@ -194,6 +195,7 @@ class Display: self.stack = stack self.headers = headers self.obj = obj + self.view = 1 def run(self, stdscr): """Set important options that require stdscr before starting""" @@ -262,17 +264,21 @@ class Display: else: star_color = curses.color_pair(1) - # Create bar component + # Compose bar text bar_start = "[" bar_middle = self.current_card().printStar() bar_end = ( f"] [{len(self.nstarred())}/{str(len(self.stack))} starred] " f"[{percent}% (" f"{str(self.obj.index).zfill(len(str(len(self.stack))))}" - f"/{str(len(self.stack))})] [" - f"{self.headers[self.current_card().side]} (" - f"{str(int(self.current_card().side) + 1)})]" + f"/{str(len(self.stack))})]" ) + if self.view == 1: + bar_end += ( + f" [{self.headers[self.current_card().side]} (" + f"{str(int(self.current_card().side) + 1)})]" + ) + bar_end += f" [View {str(self.view)}]" # Put it all togethor self.win.addstr(mlines - 1, 0, bar_start, curses.color_pair(1)) @@ -293,54 +299,83 @@ class Display: return wrap_width def disp_card(self): - """ - Display the contents of the card. - Shows a header, a horizontal line, and the contents of the current - side. - """ (_, mcols) = self.win.getmaxyx() self.main_panel.bottom() self.win.clear() - - # If on the back of the card, show the content of the front side in - # the header num_done = str(self.obj.index + 1).zfill(len(str(len(self.stack)))) - if self.current_card().side == 0: - top = num_done + " | " + self.headers[self.current_card().side] - else: - top = ( - num_done - + " | " - + self.headers[self.current_card().side] - + ' | "' - + str(self.current_card().front) - + '"' + + if self.view == 1: + """ + Display the contents of the card. + Shows a header, a horizontal line, and the contents of the current + side. + """ + # If on the back of the card, show the content of the front side in + # the header + if self.current_card().side == 0: + top = num_done + " | " + self.headers[self.current_card().side] + else: + top = ( + num_done + + " | " + + self.headers[self.current_card().side] + + ' | "' + + str(self.current_card().front) + + '"' + ) + + self.win.addstr( + 0, + 0, + textwrap.shorten(top, width=mcols - 20, placeholder="…"), + curses.A_BOLD, ) - header_width = mcols - if mcols > 80: - header_width = 80 + # Show current side + self.win.addstr( + 2, + 0, + textwrap.fill( + self.current_card().get(), + width=self.wrap_width(), + ), + ) - self.win.addstr( - 0, - 0, - textwrap.shorten(top, width=header_width, placeholder="…"), - curses.A_BOLD, - ) + elif self.view == 2: + """ + Display the contents of the card with both the front and back sides. + """ + (_, mcols) = self.win.getmaxyx() + self.main_panel.bottom() + self.win.clear() - # Add horizontal line - self.win.hline(1, 0, curses.ACS_HLINE, mcols) + self.win.addstr( + 0, + 0, + textwrap.shorten( + num_done, + width=mcols - 20, + placeholder="…", + ), + curses.A_BOLD, + ) - # Show current side - self.win.addstr( - 2, - 0, - textwrap.fill( - self.current_card().get(), - width=self.wrap_width(), - ), - ) + # Show card content + self.win.addstr( + 2, + 0, + textwrap.fill( + self.headers[0] + ": " + self.current_card().front, + width=self.wrap_width(), + ) + + "\n\n" + + textwrap.fill( + self.headers[1] + ": " + self.current_card().back, + width=self.wrap_width(), + ), + ) + self.win.hline(1, 0, curses.ACS_HLINE, mcols) self.disp_bar() self.disp_sidebar() @@ -369,7 +404,7 @@ class Display: self.obj.forward(self.stack) self.current_card().side = 0 self.disp_card() - elif key in ["j", "k", "KEY_UP", "KEY_DOWN"]: + elif key in ["j", "k", "KEY_UP", "KEY_DOWN"] and self.view == 1: self.current_card().flip() self.disp_card() elif key in ["i", "/"]: @@ -390,6 +425,8 @@ class Display: elif key == "e": (self.headers, self.stack) = runner.reparse() self.get_key() + elif key in ["1", "2"]: + self.view = int(key) def disp_sidebar(self): """Display a sidebar with the starred terms""" diff --git a/man/lightcards.1 b/man/lightcards.1 index ccbf2cd..7bf1cc1 100644 --- a/man/lightcards.1 +++ b/man/lightcards.1 @@ -65,6 +65,9 @@ Open input file in $EDITOR .TP \f[B]m\f[R] Open control menu +.TP +\f[B]1\f[R], \f[B]2\f[R] +Switch views .SH INPUT FILE .PP Lightcards takes the first table from a valid Markdown or HTML file. diff --git a/man/lightcards.1.md b/man/lightcards.1.md index 96b62ee..3a0388b 100644 --- a/man/lightcards.1.md +++ b/man/lightcards.1.md @@ -67,6 +67,9 @@ lightcards \[options\] \[input file\] **m** : Open control menu +**1**, **2** +: Switch views + # INPUT FILE Lightcards takes the first table from a valid Markdown or HTML file. Each row is a card, and the two columns are the front and back. -- 2.39.2