1 # Display card output and retreive input
6 from random import shuffle
11 from . import runner, progress
14 def panel_create(x, y):
15 """Create popup panels to a certain scale"""
16 win = curses.newwin(x, y)
17 panel = curses.panel.new_panel(win)
22 class CursesError(BaseException):
23 def __init__(self, message="lightcards: Curses error!"):
24 self.message = message
30 def __init__(self, outer, mlines=21, mcols=52):
31 """Initialize help screen"""
33 (self.win, self.panel) = panel_create(mlines, mcols)
38 "Welcome to runner. Here are some keybindings",
39 "to get you started:",
41 "h, left previous card",
43 "j, k, up, down flip card",
45 "0, ^, home go to the start of the deck",
46 "$, end go to the end of the deck",
47 "H, ? open this screen",
48 "e open the input file in $EDITOR",
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=16, 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",
114 for t in enumerate(text):
115 self.win.addstr(t[0] + 3, 1, t[1])
119 def menu_print(self, string, err=False):
120 """Print messages on the menu screen"""
122 color = curses.color_pair(2)
124 color = curses.color_pair(1)
126 self.win.addstr(14, 1, string, color)
130 """Grab keypresses on the menu screen"""
132 key = self.win.getkey()
133 if key in ["r", "m"]:
139 self.outer.stack = runner.get_orig()[1]
140 self.menu_print("Stack reset!")
142 self.outer.stack.sort(key=lambda x: x.front)
143 self.menu_print("Stack alphabetized!")
145 [x.unStar() for x in self.outer.stack]
146 self.menu_print("All unstarred!")
148 [x.star() for x in self.outer.stack]
149 self.menu_print("All starred!")
151 self.outer.stack.reverse()
152 self.menu_print("Stack reversed!")
154 shuffle(self.outer.stack)
155 self.menu_print("Stack shuffled!")
157 # Check if there are any starred cards before proceeding, and
158 # if not, don't allow to proceed and show an error message
160 for x in self.outer.stack:
167 x for x in self.outer.stack if x.starred
169 self.menu_print("Stars only!")
171 self.menu_print("ERR: None are starred!", err=True)
173 self.outer.obj.index = 0
178 Display a menu offering multiple options on how to manipulate the deck
182 self.win.addch(14, i + 1, " ")
184 (mlines, mcols) = self.outer.win.getmaxyx()
185 self.win.mvwin(int(mlines / 2) - 8, int(mcols / 2) - 22)
192 def __init__(self, stack, headers, obj, view):
194 self.headers = headers
198 def run(self, stdscr):
199 """Set important options that require stdscr before starting"""
201 curses.curs_set(0) # Hide cursor
202 curses.use_default_colors() # Allow transparency
203 curses.init_pair(1, curses.COLOR_CYAN, -1)
204 curses.init_pair(2, curses.COLOR_RED, -1)
205 curses.init_pair(3, curses.COLOR_YELLOW, -1)
207 self.main_panel = curses.panel.new_panel(self.win)
208 self.menu_obj = Menu(self)
209 self.help_obj = Help(self)
213 def check_size(self):
214 (mlines, mcols) = self.win.getmaxyx()
216 while mlines < 24 or mcols < 60:
222 "Terminal too small! Min size 60x24", width=mcols
226 (mlines, mcols) = self.win.getmaxyx()
232 """Pickle stack before quitting"""
233 if self.obj.index + 1 == len(self.stack):
236 progress.dump(self.stack, runner.get_orig()[1])
240 """Get total number of starred cards"""
241 return [card for card in self.stack if card.starred]
245 Display the statusbar at the bottom of the screen with progress, star
246 status, and card side.
248 (mlines, mcols) = self.win.getmaxyx()
249 self.win.hline(mlines - 2, 0, 0, mcols)
251 # Calculate percent done
252 if len(self.stack) <= 1:
256 round(self.obj.index / (len(self.stack) - 1) * 100)
259 # Print yellow if starred
260 if self.current_card().starred:
261 star_color = curses.color_pair(3)
263 star_color = curses.color_pair(1)
267 bar_middle = self.current_card().printStar()
269 f"] [{len(self.nstarred())}/{str(len(self.stack))} starred] "
271 f"{str(self.obj.index).zfill(len(str(len(self.stack))))}"
272 f"/{str(len(self.stack))})]"
276 f" [{self.get_side()} ("
277 f"{str(int(self.current_card().side) + 1)})]"
279 bar_end += f" [View {str(self.view)}]"
281 # Put it all togethor
282 self.win.addstr(mlines - 1, 0, bar_start, curses.color_pair(1))
283 self.win.addstr(mlines - 1, len(bar_start), bar_middle, star_color)
286 len(bar_start + bar_middle),
287 textwrap.shorten(bar_end, width=mcols - 20, placeholder="…"),
288 curses.color_pair(1),
291 def wrap_width(self):
292 """Calculate the width at which the body text should wrap"""
293 (_, mcols) = self.win.getmaxyx()
294 wrap_width = mcols - 20
300 if self.obj.side == 0:
301 return self.headers[self.current_card().side]
303 return self.headers[self.current_card().get_reverse()]
306 (_, mcols) = self.win.getmaxyx()
307 self.main_panel.bottom()
309 num_done = str(self.obj.index + 1).zfill(len(str(len(self.stack))))
311 if self.view in [1, 2]:
313 Display the contents of the card.
314 Shows a header, a horizontal line, and the contents of the current
317 # If on the back of the card, show the content of the front side in
324 if self.current_card().side == 0:
325 top = num_done + " | " + self.get_side()
332 + str(self.current_card().get()[self.obj.get_reverse()])
339 textwrap.shorten(top, width=mcols - 20, placeholder="…"),
348 self.current_card().get()[self.obj.side],
349 width=self.wrap_width(),
355 Display the contents of the card with both the front and back sides.
357 (_, mcols) = self.win.getmaxyx()
358 self.main_panel.bottom()
377 self.headers[0] + ": " + self.current_card().front,
378 width=self.wrap_width(),
382 self.headers[1] + ": " + self.current_card().back,
383 width=self.wrap_width(),
387 self.win.hline(1, 0, curses.ACS_HLINE, mcols)
391 def current_card(self):
392 """Get current card object"""
393 return self.stack[self.obj.index]
397 Display a card and wait for the input.
398 Used as a general way of getting back into the card flow from a menu
402 key = self.win.getkey()
405 elif key in ["h", "KEY_LEFT"]:
407 self.current_card().side = 0
409 elif key in ["l", "KEY_RIGHT"]:
410 if self.obj.index + 1 == len(self.stack):
413 self.obj.forward(self.stack)
414 self.current_card().side = 0
416 elif key in ["j", "k", "KEY_UP", "KEY_DOWN"] and self.view != 3:
417 self.current_card().flip()
419 elif key in ["i", "/"]:
420 self.current_card().toggleStar()
422 elif key in ["0", "^", "KEY_HOME"]:
424 self.current_card().side = 0
426 elif key in ["$", "KEY_END"]:
427 self.obj.index = len(self.stack) - 1
428 self.current_card().side = 0
430 elif key in ["H", "?"]:
435 (self.headers, self.stack) = runner.reparse()
437 elif key in ["1", "2", "3"]:
440 def disp_sidebar(self):
441 """Display a sidebar with the starred terms"""
442 (mlines, mcols) = self.win.getmaxyx()
449 curses.color_pair(3) + curses.A_BOLD,
451 self.win.vline(0, mcols - 20, 0, mlines - 2)
453 nstarred = self.nstarred()
454 for i, card in enumerate(nstarred):
455 term = card.get()[self.obj.side]
457 term = term[:18] + "…"
461 self.win.addch(mlines - 3, left + i, " ")
466 f"({len(nstarred) - i - 2} more)",
469 self.win.addstr(2 + i, left, term)
471 if len(self.nstarred()) == 0:
472 self.win.addstr(2, left, "None starred")