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)
23 def __init__(self, outer, mlines=21, mcols=52):
24 """Initialize help screen"""
26 (self.win, self.panel) = panel_create(mlines, mcols)
31 "Welcome to runner. Here are some keybindings",
32 "to get you started:",
34 "h, left previous card",
36 "j, k, up, down flip card",
38 "0, ^, home go to the start of the deck",
39 "$, end go to the end of the deck",
40 "H, ? open this screen",
41 "e open the input file in $EDITOR",
42 "m open the control menu",
43 "1, 2, 3 switch views",
45 "More information can be found in the man page, or",
46 "by running `lightcards --help`.",
48 "Press [H], or [?] to go back.",
55 curses.color_pair(1) + curses.A_BOLD,
57 self.win.hline(2, 1, curses.ACS_HLINE, mcols)
59 for t in enumerate(text):
60 self.win.addstr(t[0] + 3, 1, t[1])
65 """Display help screen"""
66 (mlines, mcols) = self.outer.win.getmaxyx()
67 self.win.mvwin(int(mlines / 2) - 10, int(mcols / 2) - 25)
71 key = self.win.getkey()
74 elif key in ["H", "?"]:
80 def __init__(self, outer, mlines=16, mcols=44):
81 """Initialize the menu with content"""
83 (self.win, self.panel) = panel_create(mlines, mcols)
91 curses.color_pair(1) + curses.A_BOLD,
93 self.win.hline(2, 1, curses.ACS_HLINE, mcols)
95 "[y]: reset stack to original state",
96 "[a]: alphabetize stack",
98 "[t]: reverse stack order",
101 "[s]: update stack to include starred only",
107 for t in enumerate(text):
108 self.win.addstr(t[0] + 3, 1, t[1])
112 def menu_print(self, string, err=False):
113 """Print messages on the menu screen"""
115 color = curses.color_pair(2)
117 color = curses.color_pair(1)
119 self.win.addstr(14, 1, string, color)
123 """Grab keypresses on the menu screen"""
125 key = self.win.getkey()
126 if key in ["r", "m"]:
132 self.outer.stack = runner.get_orig()[1]
133 self.menu_print("Stack reset!")
135 self.outer.stack.sort(key=lambda x: x.front)
136 self.menu_print("Stack alphabetized!")
138 [x.unStar() for x in self.outer.stack]
139 self.menu_print("All unstarred!")
141 [x.star() for x in self.outer.stack]
142 self.menu_print("All starred!")
144 self.outer.stack.reverse()
145 self.menu_print("Stack reversed!")
147 shuffle(self.outer.stack)
148 self.menu_print("Stack shuffled!")
150 # Check if there are any starred cards before proceeding, and
151 # if not, don't allow to proceed and show an error message
153 for x in self.outer.stack:
160 x for x in self.outer.stack if x.starred
162 self.menu_print("Stars only!")
164 self.menu_print("ERR: None are starred!", err=True)
166 self.outer.obj.index = 0
171 Display a menu offering multiple options on how to manipulate the deck
175 self.win.addch(14, i + 1, " ")
177 (mlines, mcols) = self.outer.win.getmaxyx()
178 self.win.mvwin(int(mlines / 2) - 8, int(mcols / 2) - 22)
185 def __init__(self, stack, headers, obj, view):
187 self.headers = headers
191 def run(self, stdscr):
192 """Set important options that require stdscr before starting"""
194 curses.curs_set(0) # Hide cursor
195 curses.use_default_colors() # Allow transparency
196 curses.init_pair(1, curses.COLOR_CYAN, -1)
197 curses.init_pair(2, curses.COLOR_RED, -1)
198 curses.init_pair(3, curses.COLOR_YELLOW, -1)
200 self.main_panel = curses.panel.new_panel(self.win)
201 self.menu_obj = Menu(self)
202 self.help_obj = Help(self)
206 def check_size(self):
207 (mlines, mcols) = self.win.getmaxyx()
209 while mlines < 24 or mcols < 60:
215 "Terminal too small! Min size 60x24", width=mcols
219 (mlines, mcols) = self.win.getmaxyx()
225 """Pickle stack before quitting"""
226 if self.obj.index + 1 == len(self.stack):
229 progress.dump(self.stack, runner.get_orig()[1])
233 """Get total number of starred cards"""
234 return [card for card in self.stack if card.starred]
238 Display the statusbar at the bottom of the screen with progress, star
239 status, and card side.
241 (mlines, mcols) = self.win.getmaxyx()
242 self.win.hline(mlines - 2, 0, 0, mcols)
244 # Calculate percent done
245 if len(self.stack) <= 1:
249 round(self.obj.index / (len(self.stack) - 1) * 100)
252 # Print yellow if starred
253 if self.current_card().starred:
254 star_color = curses.color_pair(3)
256 star_color = curses.color_pair(1)
260 bar_middle = self.current_card().printStar()
262 f"] [{len(self.nstarred())}/{str(len(self.stack))} starred] "
264 f"{str(self.obj.index).zfill(len(str(len(self.stack))))}"
265 f"/{str(len(self.stack))})]"
269 f" [{self.headers[self.current_card().side]} ("
270 f"{str(int(self.current_card().side) + 1)})]"
272 bar_end += f" [View {str(self.view)}]"
274 # Put it all togethor
275 self.win.addstr(mlines - 1, 0, bar_start, curses.color_pair(1))
276 self.win.addstr(mlines - 1, len(bar_start), bar_middle, star_color)
279 len(bar_start + bar_middle),
280 textwrap.shorten(bar_end, width=mcols - 20, placeholder="…"),
281 curses.color_pair(1),
284 def wrap_width(self):
285 """Calculate the width at which the body text should wrap"""
286 (_, mcols) = self.win.getmaxyx()
287 wrap_width = mcols - 20
293 (_, mcols) = self.win.getmaxyx()
294 self.main_panel.bottom()
296 num_done = str(self.obj.index + 1).zfill(len(str(len(self.stack))))
298 if self.view in [1, 2]:
300 Display the contents of the card.
301 Shows a header, a horizontal line, and the contents of the current
304 # If on the back of the card, show the content of the front side in
311 if self.current_card().side == 0:
312 top = num_done + " | " + self.headers[self.obj.side]
317 + self.headers[self.obj.side]
319 + str(self.current_card().get()[self.obj.get_reverse()])
326 textwrap.shorten(top, width=mcols - 20, placeholder="…"),
335 self.current_card().get()[self.obj.side],
336 width=self.wrap_width(),
342 Display the contents of the card with both the front and back sides.
344 (_, mcols) = self.win.getmaxyx()
345 self.main_panel.bottom()
364 self.headers[0] + ": " + self.current_card().front,
365 width=self.wrap_width(),
369 self.headers[1] + ": " + self.current_card().back,
370 width=self.wrap_width(),
374 self.win.hline(1, 0, curses.ACS_HLINE, mcols)
378 def current_card(self):
379 """Get current card object"""
380 return self.stack[self.obj.index]
384 Display a card and wait for the input.
385 Used as a general way of getting back into the card flow from a menu
389 key = self.win.getkey()
392 elif key in ["h", "KEY_LEFT"]:
394 self.current_card().side = 0
396 elif key in ["l", "KEY_RIGHT"]:
397 if self.obj.index + 1 == len(self.stack):
400 self.obj.forward(self.stack)
401 self.current_card().side = 0
403 elif key in ["j", "k", "KEY_UP", "KEY_DOWN"] and self.view != 3:
404 self.current_card().flip()
406 elif key in ["i", "/"]:
407 self.current_card().toggleStar()
409 elif key in ["0", "^", "KEY_HOME"]:
411 self.current_card().side = 0
413 elif key in ["$", "KEY_END"]:
414 self.obj.index = len(self.stack) - 1
415 self.current_card().side = 0
417 elif key in ["H", "?"]:
422 (self.headers, self.stack) = runner.reparse()
424 elif key in ["1", "2", "3"]:
427 def disp_sidebar(self):
428 """Display a sidebar with the starred terms"""
429 (mlines, mcols) = self.win.getmaxyx()
436 curses.color_pair(3) + curses.A_BOLD,
438 self.win.vline(0, mcols - 20, 0, mlines - 2)
440 nstarred = self.nstarred()
441 for i, card in enumerate(nstarred):
442 term = card.get()[self.obj.side]
444 term = term[:18] + "…"
448 self.win.addch(mlines - 3, left + i, " ")
453 f"({len(nstarred) - i - 2} more)",
456 self.win.addstr(2 + i, left, term)
458 if len(self.nstarred()) == 0:
459 self.win.addstr(2, left, "None starred")