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=20, mcols=52):
24 """Initialize help screen"""
26 (self.win, self.panel) = panel_create(mlines, mcols)
32 "Welcome to runner. Here are some keybindings",
33 "to get you started:",
35 "h, left previous card",
37 "j, k, up, down flip card",
39 "0, ^, home go to the start of the deck",
40 "$, end go to the end of the deck",
41 "H, ? open this screen",
42 "e open the input file in $EDITOR",
43 "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) - 10, int(mcols / 2) - 26)
68 self.outer.update_panels()
72 key = self.win.getkey()
75 elif key in ["H", "?"]:
81 def __init__(self, outer, mlines=17, mcols=44):
82 """Initialize the menu with content"""
84 (self.win, self.panel) = panel_create(mlines, mcols)
92 curses.color_pair(1) + curses.A_BOLD,
94 self.win.hline(2, 1, curses.ACS_HLINE, mcols)
96 "[y]: reset stack to original state",
97 "[a]: alphabetize stack",
99 "[f]: flip all cards in stack",
100 "[t]: reverse stack order",
103 "[s]: update stack to include starred only",
109 for t in enumerate(text):
110 self.win.addstr(t[0] + 3, 1, t[1])
113 self.outer.update_panels()
115 def menu_print(self, string, err=False):
116 """Print messages on the menu screen"""
118 color = curses.color_pair(2)
120 color = curses.color_pair(1)
123 self.win.addch(15, i + 1, " ")
125 self.win.addstr(15, 1, string, color)
126 self.outer.update_panels()
130 """Grab keypresses on the menu screen"""
132 key = self.win.getkey()
133 if key in ["r", "m"]:
135 self.outer.update_panels()
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!")
158 for x in self.outer.stack:
159 x.front, x.back = x.back, x.front
160 (self.outer.headers[0], self.outer.headers[1]) = (
161 self.outer.headers[1],
162 self.outer.headers[0],
164 self.menu_print("Cards flipped!")
166 # Check if there are any starred cards before proceeding, and
167 # if not, don't allow to proceed and show an error message
169 for x in self.outer.stack:
176 x for x in self.outer.stack if x.starred
178 self.menu_print("Stars only!")
180 self.menu_print("ERR: None are starred!", err=True)
182 self.outer.obj.index = 0
187 Display a menu offering multiple options on how to manipulate the deck
190 (mlines, mcols) = self.outer.win.getmaxyx()
191 self.win.mvwin(int(mlines / 2) - 9, int(mcols / 2) - 22)
193 self.outer.update_panels()
199 def __init__(self, stack, headers, obj):
201 self.headers = headers
204 def run(self, stdscr):
205 """Set important options that require stdscr before starting"""
207 (mlines, mcols) = self.win.getmaxyx()
208 curses.curs_set(0) # Hide cursor
209 curses.use_default_colors() # Allow transparency
210 curses.init_pair(1, curses.COLOR_CYAN, -1)
211 curses.init_pair(2, curses.COLOR_RED, -1)
212 curses.init_pair(3, curses.COLOR_YELLOW, -1)
214 self.main_panel = curses.panel.new_panel(self.win)
215 self.menu_obj = Menu(self)
216 self.help_obj = Help(self)
220 def check_size(self):
221 (mlines, mcols) = self.win.getmaxyx()
223 while mlines < 24 or mcols < 60:
229 "Terminal too small! Min size 60x24", width=mcols
234 (mlines, mcols) = self.win.getmaxyx()
239 def update_panels(self):
240 """Update panel and window contents"""
241 curses.panel.update_panels()
245 """Pickle stack before quitting"""
246 if self.obj.index + 1 == len(self.stack):
249 progress.dump(self.stack, runner.get_orig()[1])
253 """Get total number of starred cards"""
254 return [card for card in self.stack if card.starred]
258 Display the statusbar at the bottom of the screen with progress, star
259 status, and card side.
261 (mlines, mcols) = self.win.getmaxyx()
262 self.win.hline(mlines - 2, 0, 0, mcols)
264 # Calculate percent done
265 if len(self.stack) <= 1:
269 round(self.obj.index / (len(self.stack) - 1) * 100)
272 # Print yellow if starred
273 if self.current_card().starred:
274 star_color = curses.color_pair(3)
276 star_color = curses.color_pair(1)
278 # Create bar component
280 bar_middle = self.current_card().printStar()
282 f"] [{len(self.nstarred())}/{str(len(self.stack))} starred] "
284 f"{str(self.obj.index).zfill(len(str(len(self.stack))))}"
285 f"/{str(len(self.stack))})] ["
286 f"{self.headers[self.current_card().side]} ("
287 f"{str(int(self.current_card().side) + 1)})]"
290 # Put it all togethor
291 self.win.addstr(mlines - 1, 0, bar_start, curses.color_pair(1))
292 self.win.addstr(mlines - 1, len(bar_start), bar_middle, star_color)
295 len(bar_start + bar_middle),
296 textwrap.shorten(bar_end, width=mcols - 20, placeholder="…"),
297 curses.color_pair(1),
300 def wrap_width(self):
301 """Calculate the width at which the body text should wrap"""
302 (_, mcols) = self.win.getmaxyx()
303 wrap_width = mcols - 20
310 Display the contents of the card.
311 Shows a header, a horizontal line, and the contents of the current
314 (mlines, mcols) = self.win.getmaxyx()
315 self.main_panel.bottom()
317 # If on the back of the card, show the content of the front side in
319 num_done = str(self.obj.index + 1).zfill(len(str(len(self.stack))))
320 if self.current_card().side == 0:
321 top = num_done + " | " + self.headers[self.current_card().side]
326 + self.headers[self.current_card().side]
328 + str(self.current_card().front)
338 textwrap.shorten(top, width=header_width, placeholder="…"),
342 # Add horizontal line
343 self.win.hline(1, 0, curses.ACS_HLINE, mcols)
350 self.current_card().get(),
351 width=self.wrap_width(),
358 def current_card(self):
359 """Get current card object"""
360 return self.stack[self.obj.index]
364 Display a card and wait for the input.
365 Used as a general way of getting back into the card flow from a menu
369 key = self.win.getkey()
372 elif key in ["h", "KEY_LEFT"]:
374 self.current_card().side = 0
376 elif key in ["l", "KEY_RIGHT"]:
377 if self.obj.index + 1 == len(self.stack):
380 self.obj.forward(self.stack)
381 self.current_card().side = 0
383 elif key in ["j", "k", "KEY_UP", "KEY_DOWN"]:
384 self.current_card().flip()
386 elif key in ["i", "/"]:
387 self.current_card().toggleStar()
389 elif key in ["0", "^", "KEY_HOME"]:
391 self.current_card().side = 0
393 elif key in ["$", "KEY_END"]:
394 self.obj.index = len(self.stack) - 1
395 self.current_card().side = 0
397 elif key in ["H", "?"]:
402 (self.headers, self.stack) = runner.reparse()
405 def disp_sidebar(self):
406 """Display a sidebar with the starred terms"""
407 (mlines, mcols) = self.win.getmaxyx()
411 self.win.addch(0, mcols - 20 + i, " ")
417 curses.color_pair(3) + curses.A_BOLD,
419 self.win.vline(0, mcols - 20, 0, mlines - 2)
421 nstarred = self.nstarred()
422 if mlines - 5 < len(self.nstarred()):
423 nstarred = self.nstarred()[: mlines - 4]
424 elif mlines - 5 == len(self.nstarred()):
425 nstarred = self.nstarred()[: mlines - 3]
428 for i, card in enumerate(nstarred):
432 self.win.addstr(2 + i, left, term)
433 if not nstarred == self.nstarred():
437 f"({len(self.nstarred()) - len(nstarred)} more)",
441 if len(self.nstarred()) == 0:
442 self.win.addstr(2, left, "None starred")