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)
24 def __init__(self, outer, mlines=5, mcols=20):
26 (self.win, self.panel) = panel_create(mlines, mcols)
34 curses.color_pair(1) + curses.A_BOLD,
36 self.win.hline(2, 1, curses.ACS_HLINE, mcols)
37 self.win.addstr(3, 1, "Quit? [y/n]")
42 """Display quit confirmation screen"""
43 (mlines, mcols) = self.outer.win.getmaxyx()
44 self.win.mvwin(int(mlines / 2) - 3, int(mcols / 2) - 10)
46 if self.outer.config["quit_confirmation"]:
48 key = self.win.getkey()
57 def __init__(self, outer, mlines, mcols):
59 (self.win, self.panel) = panel_create(mlines, mcols)
63 def config_get(self, setting, cut=28):
64 return str(list(self.outer.config[setting][:cut])).replace("'", "")
68 def __init__(self, outer, mlines=23, mcols=52):
69 """Initialize help screen"""
70 super().__init__(outer, mlines, mcols)
74 "Welcome to Lightcards. Here are some keybindings",
75 "to get you started:",
77 f"{c('card_prev')}: previous card",
78 f"{c('card_next')}: next card",
79 f"{c('card_flip')}: flip card",
80 f"{c('card_star')}: star card",
81 f"{c('card_first')}: go to first card",
82 f"{c('card_last')}: go to last card",
83 f"{c('help_disp')}: open this help menu",
84 f"{c('menu_disp')}: open the control menu",
85 f"{c('view_one')}: switch to view one",
86 f"{c('view_two')}: switch to view two",
87 f"{c('view_three')}: switch to view three",
88 f"{c('quit_key')}: quit",
90 "More information can be found in the man page, or",
91 "by running `lightcards --help`.",
93 f"Press {c('help_disp')} to go back.",
100 curses.color_pair(1) + curses.A_BOLD,
102 self.win.hline(2, 1, curses.ACS_HLINE, mcols)
104 for i, content in enumerate(text, 3):
105 self.win.addstr(i, 1, content)
110 """Display help screen"""
111 (mlines, mcols) = self.outer.win.getmaxyx()
112 self.win.mvwin(int(mlines / 2) - 12, int(mcols / 2) - 25)
116 key = self.win.getkey()
117 if key in self.outer.config["quit_key"]:
119 elif key in self.outer.config["help_disp"]:
125 def __init__(self, outer, mlines=17, mcols=44):
126 """Initialize the menu with content"""
127 super().__init__(outer, mlines, mcols)
134 curses.color_pair(1) + curses.A_BOLD,
136 self.win.hline(2, 1, curses.ACS_HLINE, mcols)
137 env = os.environ.get("EDITOR", "$EDITOR")[:15]
139 f"{c('menu_reset')}: reset stack to original state",
140 f"{c('menu_alphabetize')}: alphabetize stack",
141 f"{c('menu_shuffle')}: shuffle stack",
142 f"{c('menu_reverse')}: reverse stack order",
143 f"{c('menu_unstar')}: unstar all",
144 f"{c('menu_star')}: star all",
145 f"{c('menu_stars_only')}: update stack to include starred only",
146 f"{c('menu_open_file')}: open the input file in {env}",
148 f"{c('menu_restart')}: restart",
149 f"{c('menu_disp')}: close menu",
152 for i, content in enumerate(text, 3):
153 self.win.addstr(i, 1, content)
159 self.win.addch(15, i + 1, " ")
161 def menu_print(self, string, err=False):
162 """Print messages on the menu screen"""
164 color = curses.color_pair(2)
166 color = curses.color_pair(1)
168 self.win.addstr(15, 1, string, color)
172 """Grab keypresses on the menu screen"""
174 key = self.win.getkey()
177 in self.outer.config["menu_disp"]
178 + self.outer.config["menu_restart"]
182 elif key in self.outer.config["quit_key"]:
184 elif key in self.outer.config["menu_reset"]:
185 self.outer.stack = runner.get_orig()[1]
186 self.menu_print("Stack reset!")
187 elif key in self.outer.config["menu_alphabetize"]:
188 self.outer.stack.sort(key=lambda x: x.front)
189 self.menu_print("Stack alphabetized!")
190 elif key in self.outer.config["menu_unstar"]:
191 [x.unStar() for x in self.outer.stack]
192 self.menu_print("All unstarred!")
193 elif key in self.outer.config["menu_star"]:
194 [x.star() for x in self.outer.stack]
195 self.menu_print("All starred!")
196 elif key in self.outer.config["menu_reverse"]:
197 self.outer.stack.reverse()
198 self.menu_print("Stack reversed!")
199 elif key in self.outer.config["menu_shuffle"]:
200 shuffle(self.outer.stack)
201 self.menu_print("Stack shuffled!")
202 elif key in self.outer.config["menu_open_file"]:
204 os.system(f"$EDITOR {self.outer.input_file}"),
205 (self.outer.headers, self.outer.stack) = parse.parse_html(
206 parse.md2html(self.outer.input_file),
211 elif key in self.outer.config["menu_stars_only"]:
212 # Check if there are any starred cards before proceeding, and
213 # if not, don't allow to proceed and show an error message
215 for x in self.outer.stack:
222 x for x in self.outer.stack if x.starred
224 self.menu_print("Stars only!")
226 self.menu_print("ERR: None are starred!", err=True)
227 elif key in self.outer.config["menu_restart"]:
228 self.outer.obj.index = 0
233 Display a menu offering multiple options on how to manipulate the deck
238 (mlines, mcols) = self.outer.win.getmaxyx()
239 self.win.mvwin(int(mlines / 2) - 8, int(mcols / 2) - 22)
246 def __init__(self, stack, headers, obj, view, args, conf):
248 self.headers = headers
251 self.input_file = args.inp
255 def run(self, stdscr):
256 """Set important options that require stdscr before starting"""
258 curses.curs_set(0) # Hide cursor
259 curses.use_default_colors() # Allow transparency
260 curses.init_pair(1, self.config["highlight_color"], -1)
261 curses.init_pair(2, self.config["error_color"], -1)
262 curses.init_pair(3, self.config["starred_color"], -1)
264 self.main_panel = curses.panel.new_panel(self.win)
265 self.menu_obj = Menu(self)
266 self.help_obj = Help(self)
267 self.quit_obj = Quit(self)
271 def check_size(self):
272 (mlines, mcols) = self.win.getmaxyx()
274 while mlines < 24 or mcols < 60:
280 "Terminal too small! Min size 60x24", width=mcols
284 (mlines, mcols) = self.win.getmaxyx()
290 if self.config["cache"]:
291 progress.dump(self.stack, runner.get_orig()[1])
294 """Pickle stack and confirm before quitting"""
296 if self.obj.index + 1 == len(self.stack):
303 """Get total number of starred cards"""
304 return [card for card in self.stack if card.starred]
307 # Calculate percent done
308 if len(self.stack) <= 1:
312 round(self.obj.index / (len(self.stack) - 1) * 100)
315 # Print yellow if starred
316 if self.current_card().starred:
317 self.star_color = curses.color_pair(3)
319 self.star_color = curses.color_pair(1)
323 self.bar_middle = self.current_card().printStar()
325 f"] [{len(self.nstarred())}/{str(len(self.stack))} starred] "
329 f" [{self.get_side()} ("
330 f"{str(int(self.current_card().side) + 1)})]"
332 self.bar_end += f" [View {str(self.view)}]"
336 f"{str(self.obj.index + 1).zfill(len(str(len(self.stack))))}"
337 f"/{str(len(self.stack))})] "
342 Display the statusbar at the bottom of the screen with progress, star
343 status, and card side.
345 # Put it all togethor
346 (mlines, mcols) = self.win.getmaxyx()
348 self.win.addstr(height, 0, self.bar_start, curses.color_pair(1))
350 height, len(self.bar_start), self.bar_middle, self.star_color
354 len(self.bar_start + self.bar_middle),
355 textwrap.shorten(self.bar_end, width=mcols - 20, placeholder="…"),
356 curses.color_pair(1),
363 curses.color_pair(1),
369 / (len(self.stack) - 1)
370 * (mcols - len(self.progress))
376 i + len(self.progress),
377 self.config["progress_char"],
378 curses.color_pair(1),
381 self.win.hline(mlines - 3, 0, 0, mcols)
383 def wrap_width(self):
384 """Calculate the width at which the body text should wrap"""
385 (_, mcols) = self.win.getmaxyx()
386 wrap_width = mcols - 20
392 if self.obj.side == 0:
393 return self.headers[self.current_card().side]
395 return self.headers[self.current_card().get_reverse()]
398 (_, mcols) = self.win.getmaxyx()
399 self.main_panel.bottom()
401 num_done = str(self.obj.index + 1).zfill(len(str(len(self.stack))))
403 if self.view in [1, 2, 4]:
405 Display the contents of the card.
406 Shows a header, a horizontal line, and the contents of the current
409 # If on the back of the card, show the content of the front side in
416 if self.current_card().side == 0:
417 top = num_done + " | " + self.get_side()
424 + str(self.current_card().get()[self.obj.get_reverse()])
432 textwrap.shorten(top, width=mcols - 20, placeholder="…"),
441 self.current_card().get()[self.obj.side],
442 width=self.wrap_width(),
448 Display the contents of the card with both the front and back sides.
467 self.headers[0] + ": " + self.current_card().front,
468 width=self.wrap_width(),
472 self.headers[1] + ": " + self.current_card().back,
473 width=self.wrap_width(),
477 self.win.hline(1, 0, curses.ACS_HLINE, mcols)
481 def current_card(self):
482 """Get current card object"""
483 return self.stack[self.obj.index]
487 Display a card and wait for the input.
488 Used as a general way of getting back into the card flow from a menu
492 key = self.win.getkey()
493 if key in self.config["quit_key"]:
495 elif key in self.config["card_prev"]:
497 self.current_card().side = 0
499 elif key in self.config["card_next"]:
500 if self.obj.index + 1 == len(self.stack):
501 if self.config["show_menu_at_end"]:
504 self.obj.forward(self.stack)
505 self.current_card().side = 0
507 elif key in self.config["card_flip"] and self.view != 3:
508 self.current_card().flip()
510 elif key in self.config["card_star"]:
511 self.current_card().toggleStar()
513 elif key in self.config["card_first"]:
515 self.current_card().side = 0
517 elif key in self.config["card_last"]:
518 self.obj.index = len(self.stack) - 1
519 self.current_card().side = 0
521 elif key in self.config["help_disp"]:
523 elif key in self.config["menu_disp"]:
525 elif key in self.config["view_one"]:
527 elif key in self.config["view_two"]:
529 elif key in self.config["view_three"]:
532 def disp_sidebar(self):
533 """Display a sidebar with the starred terms"""
534 (mlines, mcols) = self.win.getmaxyx()
541 curses.color_pair(3) + curses.A_BOLD,
543 self.win.vline(0, mcols - 20, 0, mlines - 3)
545 nstarred = self.nstarred()
546 for i, card in enumerate(nstarred):
547 term = card.get(smart=False)[0]
549 term = term[:18] + "…"
553 self.win.addch(mlines - 4, left + i, " ")
558 f"({len(nstarred) - i - 2} more)",
561 self.win.addstr(2 + i, left, term)
563 if len(self.nstarred()) == 0:
564 self.win.addstr(2, left, "None starred")