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)
181 elif key in ["h", "KEY_LEFT"]:
182 self.outer.obj.index = len(self.outer.stack) - 1
185 self.outer.obj.index = 0
190 Display a menu offering multiple options on how to manipulate the deck
193 (mlines, mcols) = self.outer.win.getmaxyx()
194 self.win.mvwin(int(mlines / 2) - 9, int(mcols / 2) - 22)
196 self.outer.update_panels()
202 def __init__(self, stack, headers, obj):
204 self.headers = headers
207 def run(self, stdscr):
208 """Set important options that require stdscr before starting"""
210 (mlines, mcols) = self.win.getmaxyx()
211 curses.curs_set(0) # Hide cursor
212 curses.use_default_colors() # Allow transparency
213 curses.init_pair(1, curses.COLOR_CYAN, -1)
214 curses.init_pair(2, curses.COLOR_RED, -1)
215 curses.init_pair(3, curses.COLOR_YELLOW, -1)
217 (self.main_win, self.main_panel) = panel_create(mlines, mcols)
218 self.menu_obj = Menu(self)
219 self.help_obj = Help(self)
223 def check_size(self):
224 (mlines, mcols) = self.win.getmaxyx()
226 while mlines < 24 or mcols < 60:
227 self.main_win.clear()
228 self.main_win.addstr(
232 "Terminal too small! Min size 60x24", width=mcols
235 self.main_win.redrawwin()
236 self.main_win.refresh()
237 (mlines, mcols) = self.win.getmaxyx()
242 def update_panels(self):
243 """Update panel and window contents"""
244 curses.panel.update_panels()
248 """Pickle stack before quitting"""
249 if self.obj.index + 1 == len(self.stack):
252 progress.dump(self.stack, runner.get_orig()[1])
256 """Get total number of starred cards"""
257 return [card for card in self.stack if card.starred]
261 Display the statusbar at the bottom of the screen with progress, star
262 status, and card side.
264 (mlines, mcols) = self.win.getmaxyx()
265 self.win.hline(mlines - 2, 0, 0, mcols)
267 # Calculate percent done
268 if len(self.stack) <= 1:
272 round(self.obj.index / (len(self.stack) - 1) * 100)
275 # Print yellow if starred
276 if self.current_card().starred:
277 star_color = curses.color_pair(3)
279 star_color = curses.color_pair(1)
281 # Create bar component
283 bar_middle = self.current_card().printStar()
285 f"] [{len(self.nstarred())}/{str(len(self.stack))} starred] "
287 f"{str(self.obj.index).zfill(len(str(len(self.stack))))}"
288 f"/{str(len(self.stack))})] ["
289 f"{self.headers[self.current_card().side]} ("
290 f"{str(int(self.current_card().side) + 1)})]"
293 # Put it all togethor
294 self.win.addstr(mlines - 1, 0, bar_start, curses.color_pair(1))
295 self.win.addstr(mlines - 1, len(bar_start), bar_middle, star_color)
298 len(bar_start + bar_middle),
299 textwrap.shorten(bar_end, width=mcols - 20, placeholder="…"),
300 curses.color_pair(1),
303 def wrap_width(self):
304 """Calculate the width at which the body text should wrap"""
305 (_, mcols) = self.win.getmaxyx()
306 wrap_width = mcols - 20
313 Display the contents of the card.
314 Shows a header, a horizontal line, and the contents of the current
317 (mlines, mcols) = self.win.getmaxyx()
318 self.main_panel.bottom()
319 self.main_win.clear()
320 # If on the back of the card, show the content of the front side in
322 num_done = str(self.obj.index + 1).zfill(len(str(len(self.stack))))
323 if self.current_card().side == 0:
324 top = num_done + " | " + self.headers[self.current_card().side]
329 + self.headers[self.current_card().side]
331 + str(self.current_card().front)
338 self.main_win.addstr(
341 textwrap.shorten(top, width=header_width, placeholder="…"),
345 # Add horizontal line
346 self.main_win.hline(1, 0, curses.ACS_HLINE, mcols)
349 self.main_win.addstr(
353 self.current_card().get(),
354 width=self.wrap_width(),
361 def current_card(self):
362 """Get current card object"""
363 return self.stack[self.obj.index]
367 Display a card and wait for the input.
368 Used as a general way of getting back into the card flow from a menu
372 key = self.win.getkey()
375 elif key in ["h", "KEY_LEFT"]:
377 self.current_card().side = 0
379 elif key in ["l", "KEY_RIGHT"]:
380 if self.obj.index + 1 == len(self.stack):
383 self.obj.forward(self.stack)
384 self.current_card().side = 0
386 elif key in ["j", "k", "KEY_UP", "KEY_DOWN"]:
387 self.current_card().flip()
389 elif key in ["i", "/"]:
390 self.current_card().toggleStar()
392 elif key in ["0", "^", "KEY_HOME"]:
394 self.current_card().side = 0
396 elif key in ["$", "KEY_END"]:
397 self.obj.index = len(self.stack) - 1
398 self.current_card().side = 0
400 elif key in ["H", "?"]:
405 (self.headers, self.stack) = runner.reparse()
408 def disp_sidebar(self):
409 """Display a sidebar with the starred terms"""
410 (mlines, mcols) = self.win.getmaxyx()
414 self.win.addch(0, mcols - 20 + i, " ")
420 curses.color_pair(3) + curses.A_BOLD,
422 self.win.vline(0, mcols - 20, 0, mlines - 2)
424 nstarred = self.nstarred()
425 if mlines - 5 < len(self.nstarred()):
426 nstarred = self.nstarred()[: mlines - 4]
427 elif mlines - 5 == len(self.nstarred()):
428 nstarred = self.nstarred()[: mlines - 3]
431 for i, card in enumerate(nstarred):
435 self.win.addstr(2 + i, left, term)
436 if not nstarred == self.nstarred():
440 f"({len(self.nstarred()) - len(nstarred)} more)",
444 if len(self.nstarred()) == 0:
445 self.win.addstr(2, left, "None starred")