1 # Display card output and retreive input
6 from random import shuffle
10 from . import runner, progress
14 def __init__(self, stack, headers, obj):
16 self.headers = headers
19 def run(self, stdscr):
20 """Set important options that require stdscr before starting"""
22 (mlines, mcols) = self.win.getmaxyx()
23 curses.curs_set(0) # Hide cursor
24 curses.use_default_colors() # Allow transparency
25 curses.init_pair(1, curses.COLOR_CYAN, -1)
26 curses.init_pair(2, curses.COLOR_RED, -1)
27 curses.init_pair(3, curses.COLOR_YELLOW, -1)
29 (self.main_win, self.main_panel) = self.panel_create(mlines, mcols)
35 def panel_create(self, x, y):
36 """Create popup panels to a certain scale"""
37 win = curses.newwin(x, y)
38 panel = curses.panel.new_panel(win)
43 """Update panel and window contents"""
44 curses.panel.update_panels()
48 """Pickle stack before quitting"""
49 if self.obj.getIdx() + 1 == len(self.stack):
52 progress.dump(self.stack, runner.get_orig()[1])
56 """Get total number of starred cards"""
57 return [card for card in self.stack if card.getStar()]
61 Display the statusbar at the bottom of the screen with progress, star
62 status, and card side.
64 (mlines, mcols) = self.win.getmaxyx()
66 # Calculate percent done
67 if len(self.stack) <= 1:
71 round(self.obj.getIdx() / (len(self.stack) - 1) * 100)
74 # Print yellow if starred
75 if self.current_card().getStar():
76 star_color = curses.color_pair(3)
78 star_color = curses.color_pair(1)
80 # Create bar component
82 bar_middle = self.current_card().printStar()
84 f"] [{len(self.ntotal())}/{str(len(self.stack))} starred] "
86 f"{str(self.obj.getIdx()).zfill(len(str(len(self.stack))))}"
87 f"/{str(len(self.stack))})] ["
88 f"{self.headers[self.current_card().getSide()]} ("
89 f"{str(int(self.current_card().getSide()) + 1)})]"
93 self.win.addstr(mlines - 1, 0, bar_start, curses.color_pair(1))
94 self.win.addstr(mlines - 1, len(bar_start), bar_middle, star_color)
97 len(bar_start + bar_middle),
102 def menu_print(self, string, err=False):
103 """Print messages on the menu screen"""
105 color = curses.color_pair(2)
107 color = curses.color_pair(1)
110 self.menu_win.addch(15, i + 1, " ")
112 self.menu_win.addstr(15, 1, string, color)
117 """Grab keypresses on the menu screen"""
119 key = self.win.getkey()
120 if key in ["r", "q", "m"]:
121 self.menu_panel.hide()
123 if key in ["q", "m"]:
124 if len(self.stack) == self.obj.getIdx() + 1:
126 elif len(self.stack) < self.obj.getIdx() + 1:
130 self.stack = runner.get_orig()[1]
131 self.menu_print("Stack reset!")
134 self.menu_print("Stack alphabetized!")
136 [x.unStar() for x in self.stack]
137 self.menu_print("All unstarred!")
139 [x.star() for x in self.stack]
140 self.menu_print("All starred!")
143 self.menu_print("Stack reversed!")
146 self.menu_print("Stack shuffled!")
149 x[0], x[1] = x[1], x[0]
150 (self.headers[0], self.headers[1]) = (
154 self.menu_print("Cards flipped!")
156 # Check if there are any starred cards before proceeding, and
157 # if not, don't allow to proceed and show an error message
165 self.stack = [x for x in self.stack if x.getStar()]
166 self.menu_print("Stars only!")
168 self.menu_print("ERR: None are starred!", err=True)
169 elif key in ["h", "KEY_LEFT"]:
170 self.obj.setIdx(len(self.stack) - 1)
176 def menu_init(self, quit=True):
177 """Initialize the menu with content"""
178 (self.menu_win, self.menu_panel) = self.panel_create(17, 44)
179 self.menu_panel.top()
180 self.menu_panel.hide()
182 quit_text = "[q]: back"
184 quit_text = "[q]: quit"
186 self.menu_win.addstr(
187 1, 1, "LIGHTCARDS MENU", curses.color_pair(1) + curses.A_BOLD
189 self.menu_win.hline(2, 1, curses.ACS_HLINE, 15)
191 "[y]: reset stack to original state",
192 "[a]: alphabetize stack",
193 "[z]: shuffle stack",
194 "[f]: flip all cards in stack",
195 "[t]: reverse stack order",
198 "[s]: update stack to include starred only",
201 for t in enumerate(text):
202 self.menu_win.addstr(t[0] + 3, 1, t[1])
203 self.menu_win.addstr(len(text) + 4, 1, "[r]: restart")
204 self.menu_win.addstr(len(text) + 5, 1, quit_text)
209 def disp_menu(self, keygrab=True):
211 Display a menu offering multiple options on how to manipulate the deck
214 (mlines, mcols) = self.win.getmaxyx()
215 self.menu_win.mvwin(int(mlines / 2) - 9, int(mcols / 2) - 22)
216 self.menu_panel.show()
222 def wrap_width(self):
223 """Calculate the width at which the body text should wrap"""
224 (_, mcols) = self.win.getmaxyx()
225 wrap_width = mcols - 20
232 Display the contents of the card.
233 Shows a header, a horizontal line, and the contents of the current
236 (mlines, mcols) = self.win.getmaxyx()
237 self.main_panel.bottom()
238 self.main_win.clear()
239 # If on the back of the card, show the content of the front side in
241 num_done = str(self.obj.getIdx() + 1).zfill(len(str(len(self.stack))))
242 if self.current_card().getSide() == 0:
244 num_done + " | " + self.headers[self.current_card().getSide()]
250 + self.headers[self.current_card().getSide()]
252 + str(self.current_card().getFront())
259 self.main_win.addstr(
262 textwrap.shorten(top, width=header_width, placeholder="…"),
266 # Add horizontal line
267 lin_width = header_width
268 if len(top) < header_width:
270 self.main_win.hline(1, 0, curses.ACS_HLINE, lin_width)
273 self.main_win.addstr(
277 self.current_card().get(),
278 width=self.wrap_width(),
284 self.win.hline(mlines - 2, 0, 0, mcols)
287 """Initialize help screen"""
288 (self.help_win, self.help_panel) = self.panel_create(20, 52)
289 self.help_panel.top()
290 self.help_panel.hide()
291 self.help_win.clear()
292 self.help_win.addstr(
293 1, 1, "LIGHTCARDS HELP", curses.color_pair(1) + curses.A_BOLD
295 self.help_win.hline(2, 1, curses.ACS_HLINE, 15)
297 "Welcome to runner. Here are some keybindings",
298 "to get you started:",
300 "h, left previous card",
301 "l, right next card",
302 "j, k, up, down flip card",
304 "0, ^, home go to the start of the deck",
305 "$, end go to the end of the deck",
306 "H, ? open this screen",
307 "e open the input file in $EDITOR",
308 "m open the control menu",
310 "More information can be found in the man page, or",
311 "by running `lightcards --help`.",
313 "Press [q], [H], or [?] to go back.",
316 for t in enumerate(text):
317 self.help_win.addstr(t[0] + 3, 1, t[1])
322 """Display help screen"""
323 (mlines, mcols) = self.win.getmaxyx()
324 self.help_win.mvwin(int(mlines / 2) - 10, int(mcols / 2) - 26)
326 self.help_panel.show()
328 key = self.help_win.getkey()
329 if key in ["q", "H", "?"]:
330 self.help_panel.hide()
333 def current_card(self):
334 """Get current card object"""
335 return self.stack[self.obj.getIdx()]
339 Display a card and wait for the input.
340 Used as a general way of getting back into the card flow from a menu
344 key = self.win.getkey()
347 elif key in ["h", "KEY_LEFT"]:
349 self.current_card().setSide(0)
351 elif key in ["l", "KEY_RIGHT"]:
352 if self.obj.getIdx() + 1 == len(self.stack):
355 self.obj.forward(self.stack)
356 self.current_card().setSide(0)
358 elif key in ["j", "k", "KEY_UP", "KEY_DOWN"]:
359 self.current_card().flip()
361 elif key in ["i", "/"]:
362 self.current_card().toggleStar()
364 elif key in ["0", "^", "KEY_HOME"]:
366 self.current_card().setSide(0)
368 elif key in ["$", "KEY_END"]:
369 self.obj.setIdx(len(self.stack) - 1)
370 self.current_card().setSide(0)
372 elif key in ["H", "?"]:
377 (self.headers, self.stack) = runner.reparse()
380 def disp_sidebar(self):
381 """Display a sidebar with the starred terms"""
382 (mlines, mcols) = self.win.getmaxyx()
389 curses.color_pair(3) + curses.A_BOLD,
391 self.win.vline(0, mcols - 20, 0, mlines - 2)
392 self.win.hline(1, left, 0, mlines)
395 # TODO: Fix this, some off by one error
396 newntotal = self.ntotal()
397 if mlines - 5 < len(self.ntotal()):
398 newntotal = self.ntotal()[: mlines - 4]
399 elif mlines - 5 == len(self.ntotal()):
400 newntotal = self.ntotal()[: mlines - 3]
403 for i, card in enumerate(newntotal):
404 term = card.getFront()
407 self.win.addstr(2 + i, left, term)
408 if not newntotal == self.ntotal():
412 f"({len(self.ntotal()) - len(newntotal)} more)",
416 if len(self.ntotal()) == 0:
417 self.win.addstr(2, left, "None starred")