1 # Display card output and retreive input
7 from random import shuffle
12 from . import runner, progress, parse
15 def panel_create(x, y):
16 """Create popup panels to a certain scale"""
17 win = curses.newwin(x, y)
18 panel = curses.panel.new_panel(win)
23 class CursesError(BaseException):
24 def __init__(self, message="lightcards: Curses error!"):
25 self.message = message
31 def __init__(self, outer, mlines=5, mcols=20):
33 (self.win, self.panel) = panel_create(mlines, mcols)
41 curses.color_pair(1) + curses.A_BOLD,
43 self.win.hline(2, 1, curses.ACS_HLINE, mcols)
44 self.win.addstr(3, 1, "Quit? [y/n]")
49 """Display quit confirmation screen"""
50 (mlines, mcols) = self.outer.win.getmaxyx()
51 self.win.mvwin(int(mlines / 2) - 3, int(mcols / 2) - 10)
53 if self.outer.config["confirm_quit"]:
55 key = self.win.getkey()
64 def __init__(self, outer, mlines=21, mcols=52):
65 """Initialize help screen"""
67 (self.win, self.panel) = panel_create(mlines, mcols)
72 "Welcome to Lightcards. Here are the default",
73 "keybindings to get you started:",
75 "h, left previous card",
77 "j, k, up, down flip card",
79 "0, ^, home go to the start of the deck",
80 "$, end go to the end of the deck",
81 "H, ? open this screen",
82 "m open the control menu",
83 "1, 2, 3 switch views",
86 "More information can be found in the man page, or",
87 "by running `lightcards --help`.",
89 "Press [H], or [?] to go back.",
96 curses.color_pair(1) + curses.A_BOLD,
98 self.win.hline(2, 1, curses.ACS_HLINE, mcols)
100 for t in enumerate(text):
101 self.win.addstr(t[0] + 3, 1, t[1])
106 """Display help screen"""
107 (mlines, mcols) = self.outer.win.getmaxyx()
108 self.win.mvwin(int(mlines / 2) - 11, int(mcols / 2) - 25)
112 key = self.win.getkey()
115 elif key in ["H", "?"]:
121 def __init__(self, outer, mlines=17, mcols=44):
122 """Initialize the menu with content"""
124 (self.win, self.panel) = panel_create(mlines, mcols)
132 curses.color_pair(1) + curses.A_BOLD,
134 self.win.hline(2, 1, curses.ACS_HLINE, mcols)
136 "[y]: reset stack to original state",
137 "[a]: alphabetize stack",
138 "[z]: shuffle stack",
139 "[t]: reverse stack order",
142 "[s]: update stack to include starred only",
143 "[e]: open the input file in $EDITOR",
149 for t in enumerate(text):
150 self.win.addstr(t[0] + 3, 1, t[1])
156 self.win.addch(15, i + 1, " ")
158 def menu_print(self, string, err=False):
159 """Print messages on the menu screen"""
161 color = curses.color_pair(2)
163 color = curses.color_pair(1)
165 self.win.addstr(15, 1, string, color)
169 """Grab keypresses on the menu screen"""
171 key = self.win.getkey()
172 if key in ["r", "m"]:
178 self.outer.stack = runner.get_orig()[1]
179 self.menu_print("Stack reset!")
181 self.outer.stack.sort(key=lambda x: x.front)
182 self.menu_print("Stack alphabetized!")
184 [x.unStar() for x in self.outer.stack]
185 self.menu_print("All unstarred!")
187 [x.star() for x in self.outer.stack]
188 self.menu_print("All starred!")
190 self.outer.stack.reverse()
191 self.menu_print("Stack reversed!")
193 shuffle(self.outer.stack)
194 self.menu_print("Stack shuffled!")
196 progress.dump(self.outer.stack, runner.get_orig()[1])
198 os.system(f"$EDITOR {self.outer.input_file}"),
199 (self.outer.headers, self.outer.stack) = parse.parse_html(
200 parse.md2html(self.outer.input_file)
204 # Check if there are any starred cards before proceeding, and
205 # if not, don't allow to proceed and show an error message
207 for x in self.outer.stack:
214 x for x in self.outer.stack if x.starred
216 self.menu_print("Stars only!")
218 self.menu_print("ERR: None are starred!", err=True)
220 self.outer.obj.index = 0
225 Display a menu offering multiple options on how to manipulate the deck
230 (mlines, mcols) = self.outer.win.getmaxyx()
231 self.win.mvwin(int(mlines / 2) - 8, int(mcols / 2) - 22)
238 def __init__(self, stack, headers, obj, view, args, conf):
240 self.headers = headers
243 self.input_file = args.inp[0]
246 def run(self, stdscr):
247 """Set important options that require stdscr before starting"""
249 curses.curs_set(0) # Hide cursor
250 curses.use_default_colors() # Allow transparency
251 curses.init_pair(1, self.config["highlight_color"], -1)
252 curses.init_pair(2, self.config["error_color"], -1)
253 curses.init_pair(3, self.config["starred_color"], -1)
255 self.main_panel = curses.panel.new_panel(self.win)
256 self.menu_obj = Menu(self)
257 self.help_obj = Help(self)
258 self.quit_obj = Quit(self)
262 def check_size(self):
263 (mlines, mcols) = self.win.getmaxyx()
265 while mlines < 24 or mcols < 60:
271 "Terminal too small! Min size 60x24", width=mcols
275 (mlines, mcols) = self.win.getmaxyx()
281 """Pickle stack and confirm before quitting"""
283 if self.obj.index + 1 == len(self.stack):
286 progress.dump(self.stack, runner.get_orig()[1])
290 """Get total number of starred cards"""
291 return [card for card in self.stack if card.starred]
295 Display the statusbar at the bottom of the screen with progress, star
296 status, and card side.
298 (mlines, mcols) = self.win.getmaxyx()
299 self.win.hline(mlines - 3, 0, 0, mcols)
301 # Calculate percent done
302 if len(self.stack) <= 1:
306 round(self.obj.index / (len(self.stack) - 1) * 100)
309 # Print yellow if starred
310 if self.current_card().starred:
311 star_color = curses.color_pair(3)
313 star_color = curses.color_pair(1)
317 bar_middle = self.current_card().printStar()
318 bar_end = f"] [{len(self.nstarred())}/{str(len(self.stack))} starred] "
321 f" [{self.get_side()} ("
322 f"{str(int(self.current_card().side) + 1)})]"
324 bar_end += f" [View {str(self.view)}]"
326 # Put it all togethor
328 self.win.addstr(height, 0, bar_start, curses.color_pair(1))
329 self.win.addstr(height, len(bar_start), bar_middle, star_color)
332 len(bar_start + bar_middle),
333 textwrap.shorten(bar_end, width=mcols - 20, placeholder="…"),
334 curses.color_pair(1),
339 f"{str(self.obj.index + 1).zfill(len(str(len(self.stack))))}"
340 f"/{str(len(self.stack))})] "
347 curses.color_pair(1),
353 / (len(self.stack) - 1)
354 * (mcols - len(progress))
361 self.config["progress_char"],
362 curses.color_pair(1),
365 def wrap_width(self):
366 """Calculate the width at which the body text should wrap"""
367 (_, mcols) = self.win.getmaxyx()
368 wrap_width = mcols - 20
374 if self.obj.side == 0:
375 return self.headers[self.current_card().side]
377 return self.headers[self.current_card().get_reverse()]
380 (_, mcols) = self.win.getmaxyx()
381 self.main_panel.bottom()
383 num_done = str(self.obj.index + 1).zfill(len(str(len(self.stack))))
385 if self.view in [1, 2, 4]:
387 Display the contents of the card.
388 Shows a header, a horizontal line, and the contents of the current
391 # If on the back of the card, show the content of the front side in
398 if self.current_card().side == 0:
399 top = num_done + " | " + self.get_side()
406 + str(self.current_card().get()[self.obj.get_reverse()])
413 textwrap.shorten(top, width=mcols - 20, placeholder="…"),
422 self.current_card().get()[self.obj.side],
423 width=self.wrap_width(),
429 Display the contents of the card with both the front and back sides.
431 (_, mcols) = self.win.getmaxyx()
432 self.main_panel.bottom()
451 self.headers[0] + ": " + self.current_card().front,
452 width=self.wrap_width(),
456 self.headers[1] + ": " + self.current_card().back,
457 width=self.wrap_width(),
461 self.win.hline(1, 0, curses.ACS_HLINE, mcols)
465 def current_card(self):
466 """Get current card object"""
467 return self.stack[self.obj.index]
471 Display a card and wait for the input.
472 Used as a general way of getting back into the card flow from a menu
476 key = self.win.getkey()
479 elif key in self.config["card_prev"]:
481 self.current_card().side = 0
483 elif key in self.config["card_next"]:
484 if self.obj.index + 1 == len(self.stack):
485 if self.config["show_menu_at_end"]:
488 self.obj.forward(self.stack)
489 self.current_card().side = 0
491 elif key in self.config["card_flip"] and self.view != 3:
492 self.current_card().flip()
494 elif key in self.config["card_star"]:
495 self.current_card().toggleStar()
497 elif key in self.config["card_first"]:
499 self.current_card().side = 0
501 elif key in self.config["card_last"]:
502 self.obj.index = len(self.stack) - 1
503 self.current_card().side = 0
505 elif key in self.config["help_disp"]:
507 elif key in self.config["menu_disp"]:
509 elif key in self.config["view_one"]:
511 elif key in self.config["view_two"]:
513 elif key in self.config["view_three"]:
516 def disp_sidebar(self):
517 """Display a sidebar with the starred terms"""
518 (mlines, mcols) = self.win.getmaxyx()
525 curses.color_pair(3) + curses.A_BOLD,
527 self.win.vline(0, mcols - 20, 0, mlines - 3)
529 nstarred = self.nstarred()
530 for i, card in enumerate(nstarred):
531 term = card.get()[self.obj.side]
533 term = term[:18] + "…"
537 self.win.addch(mlines - 3, left + i, " ")
542 f"({len(nstarred) - i - 2} more)",
545 self.win.addstr(2 + i, left, term)
547 if len(self.nstarred()) == 0:
548 self.win.addstr(2, left, "None starred")