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.init_pair(1, curses.COLOR_CYAN, 0)
25 curses.init_pair(2, curses.COLOR_RED, 0)
26 curses.init_pair(3, curses.COLOR_YELLOW, 0)
28 (self.main_win, self.main_panel) = self.panel_create(mlines, mcols)
34 def panel_create(self, x, y):
35 """Create popup panels to a certain scale"""
36 win = curses.newwin(x, y)
37 panel = curses.panel.new_panel(win)
42 """Update panel and window contents"""
43 curses.panel.update_panels()
47 """Pickle stack before quitting"""
48 if self.obj.getIdx() + 1 == len(self.stack):
51 progress.dump(self.stack, runner.get_orig())
55 """Get total number of starred cards"""
56 return [card for card in self.stack if card.getStar()]
60 Display the statusbar at the bottom of the screen with progress, star
61 status, and card side.
63 (mlines, mcols) = self.win.getmaxyx()
65 # Calculate percent done
66 if len(self.stack) <= 1:
70 round(self.obj.getIdx() / len(self.stack) * 100)
73 # Print yellow if starred
74 if self.current_card().getStar():
75 star_color = curses.color_pair(3)
77 star_color = curses.color_pair(1)
79 # Create bar component
81 bar_middle = self.current_card().printStar()
83 f"] [{len(self.ntotal())}/{str(len(self.stack))} starred] "
85 + str(self.obj.getIdx() + 1).zfill(len(str(len(self.stack))))
86 + f"/{str(len(self.stack))})] ["
87 + f"{self.headers[self.current_card().getSide()]} ("
88 + f"{str(int(self.current_card().getSide()) + 1)})] "
92 self.win.addstr(mlines - 1, 0, bar_start, curses.color_pair(1))
93 self.win.addstr(mlines - 1, len(bar_start), bar_middle, star_color)
96 len(bar_start + bar_middle),
101 def menu_print(self, string, err=False):
102 """Print messages on the menu screen"""
104 color = curses.color_pair(2)
106 color = curses.color_pair(1)
109 self.menu_win.addch(15, i + 1, " ")
111 self.menu_win.addstr(15, 1, string, color)
116 """Grab keypresses on the menu screen"""
118 key = self.win.getkey()
119 if key in ["r", "q", "m"]:
120 self.menu_panel.hide()
122 if key in ["q", "m"]:
123 if len(self.stack) == self.obj.getIdx() + 1:
125 elif len(self.stack) < self.obj.getIdx() + 1:
129 self.stack = runner.get_orig()[1]
130 self.menu_print("Stack reset!")
133 self.menu_print("Stack alphabetized!")
135 [x.unStar() for x in self.stack]
136 self.menu_print("All unstarred!")
138 [x.star() for x in self.stack]
139 self.menu_print("All starred!")
142 self.menu_print("Stack reversed!")
145 self.menu_print("Stack shuffled!")
148 x[0], x[1] = x[1], x[0]
149 (self.headers[0], self.headers[1]) = (
153 self.menu_print("Cards flipped!")
155 # Check if there are any starred cards before proceeding, and
156 # if not, don't allow to proceed and show an error message
164 self.stack = [x for x in self.stack if x.getStar()]
165 self.menu_print("Stars only!")
167 self.menu_print("ERR: None are starred!", err=True)
168 elif key in ["h", "KEY_LEFT"]:
169 self.obj.setIdx(len(self.stack) - 1)
175 def menu_init(self, quit=True):
176 """Initialize the menu with content"""
177 (self.menu_win, self.menu_panel) = self.panel_create(17, 44)
178 self.menu_panel.top()
179 self.menu_panel.hide()
181 quit_text = "[q]: back"
183 quit_text = "[q]: quit"
185 self.menu_win.addstr(
186 1, 1, "LIGHTCARDS MENU", curses.color_pair(1) + curses.A_BOLD
188 self.menu_win.hline(2, 1, curses.ACS_HLINE, 15)
190 "[y]: reset stack to original state",
191 "[a]: alphabetize stack",
192 "[z]: shuffle stack",
193 "[f]: flip all cards in stack",
194 "[t]: reverse stack order",
197 "[s]: update stack to include starred only",
200 for t in enumerate(text):
201 self.menu_win.addstr(t[0] + 3, 1, t[1])
202 self.menu_win.addstr(len(text) + 4, 1, "[r]: restart")
203 self.menu_win.addstr(len(text) + 5, 1, quit_text)
208 def disp_menu(self, keygrab=True):
210 Display a menu offering multiple options on how to manipulate the deck
213 (mlines, mcols) = self.win.getmaxyx()
214 self.menu_win.mvwin(int(mlines / 2) - 9, int(mcols / 2) - 22)
215 self.menu_panel.show()
221 def wrap_width(self):
222 """Calculate the width at which the body text should wrap"""
223 (_, mcols) = self.win.getmaxyx()
224 wrap_width = mcols - 20
231 Display the contents of the card.
232 Shows a header, a horizontal line, and the contents of the current
235 (mlines, mcols) = self.win.getmaxyx()
236 self.main_panel.bottom()
237 self.main_win.clear()
238 # If on the back of the card, show the content of the front side in
240 num_done = str(self.obj.getIdx() + 1).zfill(len(str(len(self.stack))))
241 if self.current_card().getSide() == 0:
243 num_done + " | " + self.headers[self.current_card().getSide()]
249 + self.headers[self.current_card().getSide()]
251 + str(self.current_card().getFront())
258 self.main_win.addstr(
261 textwrap.shorten(top, width=header_width, placeholder="…"),
265 # Add horizontal line
266 lin_width = header_width
267 if len(top) < header_width:
269 self.main_win.hline(1, 0, curses.ACS_HLINE, lin_width)
272 self.main_win.addstr(
276 self.current_card().get(),
277 width=self.wrap_width(),
283 self.win.hline(mlines - 2, 0, 0, mcols)
286 """Initialize help screen"""
287 (self.help_win, self.help_panel) = self.panel_create(20, 52)
288 self.help_panel.top()
289 self.help_panel.hide()
290 self.help_win.clear()
291 self.help_win.addstr(
292 1, 1, "LIGHTCARDS HELP", curses.color_pair(1) + curses.A_BOLD
294 self.help_win.hline(2, 1, curses.ACS_HLINE, 15)
296 "Welcome to runner. Here are some keybindings",
297 "to get you started:",
299 "h, left previous card",
300 "l, right next card",
301 "j, k, up, down flip card",
303 "0, ^, home go to the start of the deck",
304 "$, end go to the end of the deck",
305 "H, ? open this screen",
306 "e open the input file in $EDITOR",
307 "m open the control menu",
309 "More information can be found in the man page, or",
310 "by running `lightcards --help`.",
312 "Press [q], [H], or [?] to go back.",
315 for t in enumerate(text):
316 self.help_win.addstr(t[0] + 3, 1, t[1])
321 """Display help screen"""
322 (mlines, mcols) = self.win.getmaxyx()
323 self.help_win.mvwin(int(mlines / 2) - 10, int(mcols / 2) - 26)
325 self.help_panel.show()
327 key = self.help_win.getkey()
328 if key in ["q", "H", "?"]:
329 self.help_panel.hide()
332 def current_card(self):
333 """Get current card object"""
334 return self.stack[self.obj.getIdx()]
338 Display a card and wait for the input.
339 Used as a general way of getting back into the card flow from a menu
343 key = self.win.getkey()
346 elif key in ["h", "KEY_LEFT"]:
348 self.current_card().setSide(0)
350 elif key in ["l", "KEY_RIGHT"]:
351 if self.obj.getIdx() + 1 == len(self.stack):
354 self.obj.forward(self.stack)
355 self.current_card().setSide(0)
357 elif key in ["j", "k", "KEY_UP", "KEY_DOWN"]:
358 self.current_card().flip()
360 elif key in ["i", "/"]:
361 self.current_card().toggleStar()
363 elif key in ["0", "^", "KEY_HOME"]:
365 self.current_card().setSide(0)
367 elif key in ["$", "KEY_END"]:
368 self.obj.setIdx(len(self.stack) - 1)
369 self.current_card().setSide(0)
371 elif key in ["H", "?"]:
376 (self.headers, self.stack) = runner.reparse()
379 def disp_sidebar(self):
380 """Display a sidebar with the starred terms"""
381 (mlines, mcols) = self.win.getmaxyx()
388 curses.color_pair(3) + curses.A_BOLD,
390 self.win.vline(0, mcols - 20, 0, mlines - 2)
391 self.win.hline(1, left, 0, mlines)
394 # TODO: Fix this, some off by one error
395 newntotal = self.ntotal()
396 if mlines - 5 < len(self.ntotal()):
397 newntotal = self.ntotal()[: mlines - 4]
398 elif mlines - 5 == len(self.ntotal()):
399 newntotal = self.ntotal()[: mlines - 3]
402 for i, card in enumerate(newntotal):
403 term = card.getFront()
406 self.win.addstr(2 + i, left, term)
407 if not newntotal == self.ntotal():
411 f"({len(self.ntotal()) - len(newntotal)} more)",
415 if len(self.ntotal()) == 0:
416 self.win.addstr(2, left, "None starred")