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",
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) - 11, int(mcols / 2) - 25)
71 key = self.win.getkey()
74 elif key in ["H", "?"]:
80 def __init__(self, outer, mlines=17, 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 "[f]: flip all cards in stack",
99 "[t]: reverse stack order",
102 "[s]: update stack to include starred only",
108 for t in enumerate(text):
109 self.win.addstr(t[0] + 3, 1, t[1])
113 def menu_print(self, string, err=False):
114 """Print messages on the menu screen"""
116 color = curses.color_pair(2)
118 color = curses.color_pair(1)
120 self.win.addstr(15, 1, string, color)
124 """Grab keypresses on the menu screen"""
126 key = self.win.getkey()
127 if key in ["r", "m"]:
133 self.outer.stack = runner.get_orig()[1]
134 self.menu_print("Stack reset!")
136 self.outer.stack.sort(key=lambda x: x.front)
137 self.menu_print("Stack alphabetized!")
139 [x.unStar() for x in self.outer.stack]
140 self.menu_print("All unstarred!")
142 [x.star() for x in self.outer.stack]
143 self.menu_print("All starred!")
145 self.outer.stack.reverse()
146 self.menu_print("Stack reversed!")
148 shuffle(self.outer.stack)
149 self.menu_print("Stack shuffled!")
151 for x in self.outer.stack:
152 x.front, x.back = x.back, x.front
153 (self.outer.headers[0], self.outer.headers[1]) = (
154 self.outer.headers[1],
155 self.outer.headers[0],
157 self.menu_print("Cards flipped!")
159 # Check if there are any starred cards before proceeding, and
160 # if not, don't allow to proceed and show an error message
162 for x in self.outer.stack:
169 x for x in self.outer.stack if x.starred
171 self.menu_print("Stars only!")
173 self.menu_print("ERR: None are starred!", err=True)
175 self.outer.obj.index = 0
180 Display a menu offering multiple options on how to manipulate the deck
184 self.win.addch(15, i + 1, " ")
186 (mlines, mcols) = self.outer.win.getmaxyx()
187 self.win.mvwin(int(mlines / 2) - 9, int(mcols / 2) - 21)
194 def __init__(self, stack, headers, obj):
196 self.headers = headers
200 def run(self, stdscr):
201 """Set important options that require stdscr before starting"""
203 curses.curs_set(0) # Hide cursor
204 curses.use_default_colors() # Allow transparency
205 curses.init_pair(1, curses.COLOR_CYAN, -1)
206 curses.init_pair(2, curses.COLOR_RED, -1)
207 curses.init_pair(3, curses.COLOR_YELLOW, -1)
209 self.main_panel = curses.panel.new_panel(self.win)
210 self.menu_obj = Menu(self)
211 self.help_obj = Help(self)
215 def check_size(self):
216 (mlines, mcols) = self.win.getmaxyx()
218 while mlines < 24 or mcols < 60:
224 "Terminal too small! Min size 60x24", width=mcols
228 (mlines, mcols) = self.win.getmaxyx()
234 """Pickle stack before quitting"""
235 if self.obj.index + 1 == len(self.stack):
238 progress.dump(self.stack, runner.get_orig()[1])
242 """Get total number of starred cards"""
243 return [card for card in self.stack if card.starred]
247 Display the statusbar at the bottom of the screen with progress, star
248 status, and card side.
250 (mlines, mcols) = self.win.getmaxyx()
251 self.win.hline(mlines - 2, 0, 0, mcols)
253 # Calculate percent done
254 if len(self.stack) <= 1:
258 round(self.obj.index / (len(self.stack) - 1) * 100)
261 # Print yellow if starred
262 if self.current_card().starred:
263 star_color = curses.color_pair(3)
265 star_color = curses.color_pair(1)
269 bar_middle = self.current_card().printStar()
271 f"] [{len(self.nstarred())}/{str(len(self.stack))} starred] "
273 f"{str(self.obj.index).zfill(len(str(len(self.stack))))}"
274 f"/{str(len(self.stack))})]"
278 f" [{self.headers[self.current_card().side]} ("
279 f"{str(int(self.current_card().side) + 1)})]"
281 bar_end += f" [View {str(self.view)}]"
283 # Put it all togethor
284 self.win.addstr(mlines - 1, 0, bar_start, curses.color_pair(1))
285 self.win.addstr(mlines - 1, len(bar_start), bar_middle, star_color)
288 len(bar_start + bar_middle),
289 textwrap.shorten(bar_end, width=mcols - 20, placeholder="…"),
290 curses.color_pair(1),
293 def wrap_width(self):
294 """Calculate the width at which the body text should wrap"""
295 (_, mcols) = self.win.getmaxyx()
296 wrap_width = mcols - 20
302 (_, mcols) = self.win.getmaxyx()
303 self.main_panel.bottom()
305 num_done = str(self.obj.index + 1).zfill(len(str(len(self.stack))))
309 Display the contents of the card.
310 Shows a header, a horizontal line, and the contents of the current
313 # If on the back of the card, show the content of the front side in
315 if self.current_card().side == 0:
316 top = num_done + " | " + self.headers[self.current_card().side]
321 + self.headers[self.current_card().side]
323 + str(self.current_card().front)
330 textwrap.shorten(top, width=mcols - 20, placeholder="…"),
339 self.current_card().get(),
340 width=self.wrap_width(),
346 Display the contents of the card with both the front and back sides.
348 (_, mcols) = self.win.getmaxyx()
349 self.main_panel.bottom()
368 self.headers[0] + ": " + self.current_card().front,
369 width=self.wrap_width(),
373 self.headers[1] + ": " + self.current_card().back,
374 width=self.wrap_width(),
378 self.win.hline(1, 0, curses.ACS_HLINE, mcols)
382 def current_card(self):
383 """Get current card object"""
384 return self.stack[self.obj.index]
388 Display a card and wait for the input.
389 Used as a general way of getting back into the card flow from a menu
393 key = self.win.getkey()
396 elif key in ["h", "KEY_LEFT"]:
398 self.current_card().side = 0
400 elif key in ["l", "KEY_RIGHT"]:
401 if self.obj.index + 1 == len(self.stack):
404 self.obj.forward(self.stack)
405 self.current_card().side = 0
407 elif key in ["j", "k", "KEY_UP", "KEY_DOWN"] and self.view == 1:
408 self.current_card().flip()
410 elif key in ["i", "/"]:
411 self.current_card().toggleStar()
413 elif key in ["0", "^", "KEY_HOME"]:
415 self.current_card().side = 0
417 elif key in ["$", "KEY_END"]:
418 self.obj.index = len(self.stack) - 1
419 self.current_card().side = 0
421 elif key in ["H", "?"]:
426 (self.headers, self.stack) = runner.reparse()
428 elif key in ["1", "2"]:
431 def disp_sidebar(self):
432 """Display a sidebar with the starred terms"""
433 (mlines, mcols) = self.win.getmaxyx()
440 curses.color_pair(3) + curses.A_BOLD,
442 self.win.vline(0, mcols - 20, 0, mlines - 2)
444 nstarred = self.nstarred()
445 if mlines - 5 < len(self.nstarred()):
446 nstarred = self.nstarred()[: mlines - 4]
447 elif mlines - 5 == len(self.nstarred()):
448 nstarred = self.nstarred()[: mlines - 3]
451 for i, card in enumerate(nstarred):
455 self.win.addstr(2 + i, left, term)
456 if not nstarred == self.nstarred():
460 f"({len(self.nstarred()) - len(nstarred)} more)",
464 if len(self.nstarred()) == 0:
465 self.win.addstr(2, left, "None starred")