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=20, mcols=52):
32 """Initialize help screen"""
34 (self.win, self.panel) = panel_create(mlines, mcols)
39 "Welcome to runner. Here are some keybindings",
40 "to get you started:",
42 "h, left previous card",
44 "j, k, up, down flip card",
46 "0, ^, home go to the start of the deck",
47 "$, end go to the end of the deck",
48 "H, ? open this screen",
49 "m open the control menu",
50 "1, 2, 3 switch views",
52 "More information can be found in the man page, or",
53 "by running `lightcards --help`.",
55 "Press [H], or [?] to go back.",
62 curses.color_pair(1) + curses.A_BOLD,
64 self.win.hline(2, 1, curses.ACS_HLINE, mcols)
66 for t in enumerate(text):
67 self.win.addstr(t[0] + 3, 1, t[1])
72 """Display help screen"""
73 (mlines, mcols) = self.outer.win.getmaxyx()
74 self.win.mvwin(int(mlines / 2) - 10, int(mcols / 2) - 25)
78 key = self.win.getkey()
81 elif key in ["H", "?"]:
87 def __init__(self, outer, mlines=17, mcols=44):
88 """Initialize the menu with content"""
90 (self.win, self.panel) = panel_create(mlines, mcols)
98 curses.color_pair(1) + curses.A_BOLD,
100 self.win.hline(2, 1, curses.ACS_HLINE, mcols)
102 "[y]: reset stack to original state",
103 "[a]: alphabetize stack",
104 "[z]: shuffle stack",
105 "[t]: reverse stack order",
108 "[s]: update stack to include starred only",
109 "[e]: open the input file in $EDITOR",
115 for t in enumerate(text):
116 self.win.addstr(t[0] + 3, 1, t[1])
120 def menu_print(self, string, err=False):
121 """Print messages on the menu screen"""
123 color = curses.color_pair(2)
125 color = curses.color_pair(1)
127 self.win.addstr(15, 1, string, color)
131 """Grab keypresses on the menu screen"""
133 key = self.win.getkey()
134 if key in ["r", "m"]:
140 self.outer.stack = runner.get_orig()[1]
141 self.menu_print("Stack reset!")
143 self.outer.stack.sort(key=lambda x: x.front)
144 self.menu_print("Stack alphabetized!")
146 [x.unStar() for x in self.outer.stack]
147 self.menu_print("All unstarred!")
149 [x.star() for x in self.outer.stack]
150 self.menu_print("All starred!")
152 self.outer.stack.reverse()
153 self.menu_print("Stack reversed!")
155 shuffle(self.outer.stack)
156 self.menu_print("Stack shuffled!")
159 os.system(f"$EDITOR {self.outer.input_file}"),
160 (self.outer.headers, self.outer.stack) = parse.parse_html(
161 parse.md2html(self.outer.input_file)
165 # Check if there are any starred cards before proceeding, and
166 # if not, don't allow to proceed and show an error message
168 for x in self.outer.stack:
175 x for x in self.outer.stack if x.starred
177 self.menu_print("Stars only!")
179 self.menu_print("ERR: None are starred!", err=True)
181 self.outer.obj.index = 0
186 Display a menu offering multiple options on how to manipulate the deck
190 self.win.addch(14, i + 1, " ")
192 (mlines, mcols) = self.outer.win.getmaxyx()
193 self.win.mvwin(int(mlines / 2) - 8, int(mcols / 2) - 22)
200 def __init__(self, stack, headers, obj, view, input_file):
202 self.headers = headers
205 self.input_file = input_file
207 def run(self, stdscr):
208 """Set important options that require stdscr before starting"""
210 curses.curs_set(0) # Hide cursor
211 curses.use_default_colors() # Allow transparency
212 curses.init_pair(1, curses.COLOR_CYAN, -1)
213 curses.init_pair(2, curses.COLOR_RED, -1)
214 curses.init_pair(3, curses.COLOR_YELLOW, -1)
216 self.main_panel = curses.panel.new_panel(self.win)
217 self.menu_obj = Menu(self)
218 self.help_obj = Help(self)
222 def check_size(self):
223 (mlines, mcols) = self.win.getmaxyx()
225 while mlines < 24 or mcols < 60:
231 "Terminal too small! Min size 60x24", width=mcols
235 (mlines, mcols) = self.win.getmaxyx()
241 """Pickle stack before quitting"""
242 if self.obj.index + 1 == len(self.stack):
245 progress.dump(self.stack, runner.get_orig()[1])
249 """Get total number of starred cards"""
250 return [card for card in self.stack if card.starred]
254 Display the statusbar at the bottom of the screen with progress, star
255 status, and card side.
257 (mlines, mcols) = self.win.getmaxyx()
258 self.win.hline(mlines - 3, 0, 0, mcols)
260 # Calculate percent done
261 if len(self.stack) <= 1:
265 round(self.obj.index / (len(self.stack) - 1) * 100)
268 # Print yellow if starred
269 if self.current_card().starred:
270 star_color = curses.color_pair(3)
272 star_color = curses.color_pair(1)
276 f"{str(self.obj.index).zfill(len(str(len(self.stack))))})]"
281 bar_middle = self.current_card().printStar()
282 bar_end = f"] [{len(self.nstarred())}/{str(len(self.stack))} starred] "
285 f" [{self.get_side()} ("
286 f"{str(int(self.current_card().side) + 1)})]"
288 bar_end += f" [View {str(self.view)}]"
290 # Put it all togethor
292 self.win.addstr(height, 0, bar_start, curses.color_pair(1))
293 self.win.addstr(height, len(bar_start), bar_middle, star_color)
296 len(bar_start + bar_middle),
297 textwrap.shorten(bar_end, width=mcols - 20, placeholder="…"),
298 curses.color_pair(1),
303 mcols - len(progress) - 1,
305 curses.color_pair(1),
311 / (len(self.stack) - 1)
312 * (mcols - len(progress))
316 # TODO: Use the variying width unicode block characters to make a
318 self.win.addch(height + 1, i, "»", curses.color_pair(1))
320 def wrap_width(self):
321 """Calculate the width at which the body text should wrap"""
322 (_, mcols) = self.win.getmaxyx()
323 wrap_width = mcols - 20
329 if self.obj.side == 0:
330 return self.headers[self.current_card().side]
332 return self.headers[self.current_card().get_reverse()]
335 (_, mcols) = self.win.getmaxyx()
336 self.main_panel.bottom()
338 num_done = str(self.obj.index + 1).zfill(len(str(len(self.stack))))
340 if self.view in [1, 2, 4]:
342 Display the contents of the card.
343 Shows a header, a horizontal line, and the contents of the current
346 # If on the back of the card, show the content of the front side in
353 if self.current_card().side == 0:
354 top = num_done + " | " + self.get_side()
361 + str(self.current_card().get()[self.obj.get_reverse()])
368 textwrap.shorten(top, width=mcols - 20, placeholder="…"),
377 self.current_card().get()[self.obj.side],
378 width=self.wrap_width(),
384 Display the contents of the card with both the front and back sides.
386 (_, mcols) = self.win.getmaxyx()
387 self.main_panel.bottom()
406 self.headers[0] + ": " + self.current_card().front,
407 width=self.wrap_width(),
411 self.headers[1] + ": " + self.current_card().back,
412 width=self.wrap_width(),
416 self.win.hline(1, 0, curses.ACS_HLINE, mcols)
420 def current_card(self):
421 """Get current card object"""
422 return self.stack[self.obj.index]
426 Display a card and wait for the input.
427 Used as a general way of getting back into the card flow from a menu
431 key = self.win.getkey()
434 elif key in ["h", "KEY_LEFT"]:
436 self.current_card().side = 0
438 elif key in ["l", "KEY_RIGHT"]:
439 if self.obj.index + 1 == len(self.stack):
442 self.obj.forward(self.stack)
443 self.current_card().side = 0
445 elif key in ["j", "k", "KEY_UP", "KEY_DOWN"] and self.view != 3:
446 self.current_card().flip()
448 elif key in ["i", "/"]:
449 self.current_card().toggleStar()
451 elif key in ["0", "^", "KEY_HOME"]:
453 self.current_card().side = 0
455 elif key in ["$", "KEY_END"]:
456 self.obj.index = len(self.stack) - 1
457 self.current_card().side = 0
459 elif key in ["H", "?"]:
463 elif key in ["1", "2", "3", "4"]:
466 def disp_sidebar(self):
467 """Display a sidebar with the starred terms"""
468 (mlines, mcols) = self.win.getmaxyx()
475 curses.color_pair(3) + curses.A_BOLD,
477 self.win.vline(0, mcols - 20, 0, mlines - 3)
479 nstarred = self.nstarred()
480 for i, card in enumerate(nstarred):
481 term = card.get()[self.obj.side]
483 term = term[:18] + "…"
487 self.win.addch(mlines - 3, left + i, " ")
492 f"({len(nstarred) - i - 2} more)",
495 self.win.addstr(2 + i, left, term)
497 if len(self.nstarred()) == 0:
498 self.win.addstr(2, left, "None starred")