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])
154 def menu_print(self, string, err=False):
155 """Print messages on the menu screen"""
157 color = curses.color_pair(2)
159 color = curses.color_pair(1)
161 self.win.addstr(15, 1, string, color)
165 """Grab keypresses on the menu screen"""
167 key = self.win.getkey()
168 if key in ["r", "m"]:
174 self.outer.stack = runner.get_orig()[1]
175 self.menu_print("Stack reset!")
177 self.outer.stack.sort(key=lambda x: x.front)
178 self.menu_print("Stack alphabetized!")
180 [x.unStar() for x in self.outer.stack]
181 self.menu_print("All unstarred!")
183 [x.star() for x in self.outer.stack]
184 self.menu_print("All starred!")
186 self.outer.stack.reverse()
187 self.menu_print("Stack reversed!")
189 shuffle(self.outer.stack)
190 self.menu_print("Stack shuffled!")
193 os.system(f"$EDITOR {self.outer.input_file}"),
194 (self.outer.headers, self.outer.stack) = parse.parse_html(
195 parse.md2html(self.outer.input_file)
199 # Check if there are any starred cards before proceeding, and
200 # if not, don't allow to proceed and show an error message
202 for x in self.outer.stack:
209 x for x in self.outer.stack if x.starred
211 self.menu_print("Stars only!")
213 self.menu_print("ERR: None are starred!", err=True)
215 self.outer.obj.index = 0
220 Display a menu offering multiple options on how to manipulate the deck
224 self.win.addch(14, i + 1, " ")
226 (mlines, mcols) = self.outer.win.getmaxyx()
227 self.win.mvwin(int(mlines / 2) - 8, int(mcols / 2) - 22)
234 def __init__(self, stack, headers, obj, view, args, conf):
236 self.headers = headers
239 self.input_file = args.inp[0]
242 def run(self, stdscr):
243 """Set important options that require stdscr before starting"""
245 curses.curs_set(0) # Hide cursor
246 curses.use_default_colors() # Allow transparency
247 curses.init_pair(1, curses.COLOR_CYAN, -1)
248 curses.init_pair(2, curses.COLOR_RED, -1)
249 curses.init_pair(3, curses.COLOR_YELLOW, -1)
251 self.main_panel = curses.panel.new_panel(self.win)
252 self.menu_obj = Menu(self)
253 self.help_obj = Help(self)
254 self.quit_obj = Quit(self)
258 def check_size(self):
259 (mlines, mcols) = self.win.getmaxyx()
261 while mlines < 24 or mcols < 60:
267 "Terminal too small! Min size 60x24", width=mcols
271 (mlines, mcols) = self.win.getmaxyx()
277 """Pickle stack and confirm before quitting"""
279 if self.obj.index + 1 == len(self.stack):
282 progress.dump(self.stack, runner.get_orig()[1])
286 """Get total number of starred cards"""
287 return [card for card in self.stack if card.starred]
291 Display the statusbar at the bottom of the screen with progress, star
292 status, and card side.
294 (mlines, mcols) = self.win.getmaxyx()
295 self.win.hline(mlines - 3, 0, 0, mcols)
297 # Calculate percent done
298 if len(self.stack) <= 1:
302 round(self.obj.index / (len(self.stack) - 1) * 100)
305 # Print yellow if starred
306 if self.current_card().starred:
307 star_color = curses.color_pair(3)
309 star_color = curses.color_pair(1)
313 bar_middle = self.current_card().printStar()
314 bar_end = f"] [{len(self.nstarred())}/{str(len(self.stack))} starred] "
317 f" [{self.get_side()} ("
318 f"{str(int(self.current_card().side) + 1)})]"
320 bar_end += f" [View {str(self.view)}]"
322 # Put it all togethor
324 self.win.addstr(height, 0, bar_start, curses.color_pair(1))
325 self.win.addstr(height, len(bar_start), bar_middle, star_color)
328 len(bar_start + bar_middle),
329 textwrap.shorten(bar_end, width=mcols - 20, placeholder="…"),
330 curses.color_pair(1),
335 f"{str(self.obj.index + 1).zfill(len(str(len(self.stack))))}"
336 f"/{str(len(self.stack))})] "
343 curses.color_pair(1),
349 / (len(self.stack) - 1)
350 * (mcols - len(progress))
354 # TODO: Use the variying width unicode block characters to make a
357 height + 1, i + len(progress), "»", curses.color_pair(1)
360 def wrap_width(self):
361 """Calculate the width at which the body text should wrap"""
362 (_, mcols) = self.win.getmaxyx()
363 wrap_width = mcols - 20
369 if self.obj.side == 0:
370 return self.headers[self.current_card().side]
372 return self.headers[self.current_card().get_reverse()]
375 (_, mcols) = self.win.getmaxyx()
376 self.main_panel.bottom()
378 num_done = str(self.obj.index + 1).zfill(len(str(len(self.stack))))
380 if self.view in [1, 2, 4]:
382 Display the contents of the card.
383 Shows a header, a horizontal line, and the contents of the current
386 # If on the back of the card, show the content of the front side in
393 if self.current_card().side == 0:
394 top = num_done + " | " + self.get_side()
401 + str(self.current_card().get()[self.obj.get_reverse()])
408 textwrap.shorten(top, width=mcols - 20, placeholder="…"),
417 self.current_card().get()[self.obj.side],
418 width=self.wrap_width(),
424 Display the contents of the card with both the front and back sides.
426 (_, mcols) = self.win.getmaxyx()
427 self.main_panel.bottom()
446 self.headers[0] + ": " + self.current_card().front,
447 width=self.wrap_width(),
451 self.headers[1] + ": " + self.current_card().back,
452 width=self.wrap_width(),
456 self.win.hline(1, 0, curses.ACS_HLINE, mcols)
460 def current_card(self):
461 """Get current card object"""
462 return self.stack[self.obj.index]
466 Display a card and wait for the input.
467 Used as a general way of getting back into the card flow from a menu
471 key = self.win.getkey()
474 elif key in self.config["card_prev"]:
476 self.current_card().side = 0
478 elif key in self.config["card_next"]:
479 if self.obj.index + 1 == len(self.stack):
482 self.obj.forward(self.stack)
483 self.current_card().side = 0
485 elif key in self.config["card_flip"] and self.view != 3:
486 self.current_card().flip()
488 elif key in self.config["card_star"]:
489 self.current_card().toggleStar()
491 elif key in self.config["card_first"]:
493 self.current_card().side = 0
495 elif key in self.config["card_last"]:
496 self.obj.index = len(self.stack) - 1
497 self.current_card().side = 0
499 elif key in self.config["help_disp"]:
501 elif key in self.config["menu_disp"]:
503 elif key in self.config["view_one"]:
505 elif key in self.config["view_two"]:
507 elif key in self.config["view_three"]:
510 def disp_sidebar(self):
511 """Display a sidebar with the starred terms"""
512 (mlines, mcols) = self.win.getmaxyx()
519 curses.color_pair(3) + curses.A_BOLD,
521 self.win.vline(0, mcols - 20, 0, mlines - 3)
523 nstarred = self.nstarred()
524 for i, card in enumerate(nstarred):
525 term = card.get()[self.obj.side]
527 term = term[:18] + "…"
531 self.win.addch(mlines - 3, left + i, " ")
536 f"({len(nstarred) - i - 2} more)",
539 self.win.addstr(2 + i, left, term)
541 if len(self.nstarred()) == 0:
542 self.win.addstr(2, left, "None starred")