1 # Display card output and retreive input
6 from random import shuffle
10 from . import lightcards, progress
14 def __init__(self, stack, headers, obj):
16 self.headers = headers
19 def run(self, stdscr):
20 """Set important options before beginning"""
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 menus"""
36 win = curses.newwin(x, y)
37 panel = curses.panel.new_panel(win)
42 curses.panel.update_panels()
46 """Pickle stack before quitting"""
47 if self.obj.getIdx() + 1 == len(self.stack):
50 progress.dump(self.stack, lightcards.get_orig())
54 """Get toal number of starred cards"""
55 return [card for card in self.stack if card.getStar()]
59 Display the statusbar at the bottom of the screen with progress, star
60 status, and card side.
62 (mlines, mcols) = self.win.getmaxyx()
64 # Calculate percent done
65 if len(self.stack) <= 1:
69 round(self.obj.getIdx() / len(self.stack) * 100)
72 # Print yellow if starred
73 if self.current_card().getStar():
74 star_color = curses.color_pair(3)
76 star_color = curses.color_pair(1)
78 # Create bar component
80 bar_middle = self.current_card().printStar()
82 f"] [{len(self.ntotal())}/{str(len(self.stack))} starred] "
84 + str(self.obj.getIdx() + 1).zfill(len(str(len(self.stack))))
85 + f"/{str(len(self.stack))})] ["
86 + f"{self.headers[self.current_card().getSide()]} ("
87 + f"{str(self.current_card().getSide()) + str(1)})] "
91 self.win.addstr(mlines - 1, 0, bar_start, curses.color_pair(1))
92 self.win.addstr(mlines - 1, len(bar_start), bar_middle, star_color)
95 len(bar_start + bar_middle),
100 def menu_print(self, string, err=False):
101 """Print messages on the menu screen"""
103 color = curses.color_pair(2)
105 color = curses.color_pair(1)
108 self.menu_win.addch(15, i + 1, " ")
110 self.menu_win.addstr(15, 1, string, color)
115 """Grab keypresses for the menu screen"""
117 key = self.win.getkey()
118 if key in ["r", "q", "m"]:
119 self.menu_panel.hide()
121 if key in ["q", "m"]:
122 if len(self.stack) == self.obj.getIdx() + 1:
124 elif len(self.stack) < self.obj.getIdx() + 1:
128 self.stack = lightcards.get_orig()[1]
129 self.menu_print("Stack reset!")
132 self.menu_print("Stack alphabetized!")
134 [x.unStar() for x in self.stack]
135 self.menu_print("All unstarred!")
137 [x.star() for x in self.stack]
138 self.menu_print("All starred!")
141 self.menu_print("Stack reversed!")
144 self.menu_print("Stack shuffled!")
147 x[0], x[1] = x[1], x[0]
148 (self.headers[0], self.headers[1]) = (
152 self.menu_print("Cards flipped!")
154 # Check if there are any starred cards before proceeding, and
155 # if not, don't allow to proceed and show an error message
163 self.stack = [x for x in self.stack if x.getStar()]
164 self.menu_print("Stars only!")
166 self.menu_print("ERR: None are starred!", err=True)
167 elif key in ["h", "KEY_LEFT"]:
168 self.obj.setIdx(len(self.stack) - 1)
174 def menu_init(self, quit=True):
175 (self.menu_win, self.menu_panel) = self.panel_create(17, 44)
176 self.menu_panel.top()
177 self.menu_panel.hide()
179 quit_text = "[q]: back"
181 quit_text = "[q]: quit"
183 self.menu_win.addstr(
184 1, 1, "LIGHTCARDS MENU", curses.color_pair(1) + curses.A_BOLD
186 self.menu_win.hline(2, 1, curses.ACS_HLINE, 15)
188 "[y]: reset stack to original state",
189 "[a]: alphabetize stack",
190 "[z]: shuffle stack",
191 "[f]: flip all cards in stack",
192 "[t]: reverse stack order",
195 "[s]: update stack to include starred only",
198 for t in enumerate(text):
199 self.menu_win.addstr(t[0] + 3, 1, t[1])
200 self.menu_win.addstr(len(text) + 4, 1, "[r]: restart")
201 self.menu_win.addstr(len(text) + 5, 1, quit_text)
206 def disp_menu(self, keygrab=True):
208 Display a menu once the end of the deck has been reached, offering
209 multiple options on how to continue.
211 (mlines, mcols) = self.win.getmaxyx()
212 self.menu_win.mvwin(int(mlines / 2) - 9, int(mcols / 2) - 22)
213 self.menu_panel.show()
219 def wrap_width(self):
220 """Calculate the width at which the body should wrap"""
221 (_, mcols) = self.win.getmaxyx()
222 wrap_width = mcols - 20
229 Display the contents of the card
230 Shows a header, a horizontal line, and the contents of the current
233 (mlines, mcols) = self.win.getmaxyx()
234 self.main_panel.bottom()
235 self.main_win.clear()
236 # If on the back of the card, show the content of the front side in
238 num_done = str(self.obj.getIdx() + 1).zfill(len(str(len(self.stack))))
239 if self.current_card().getSide() == 0:
241 num_done + " | " + self.headers[self.current_card().getSide()]
247 + self.headers[self.current_card().getSide()]
249 + str(self.current_card().getFront())
256 self.main_win.addstr(
259 textwrap.shorten(top, width=header_width, placeholder="…"),
263 # Add horizontal line
264 lin_width = header_width
265 if len(top) < header_width:
267 self.main_win.hline(1, 0, curses.ACS_HLINE, lin_width)
270 self.main_win.addstr(
274 self.current_card().get(),
275 width=self.wrap_width(),
281 self.win.hline(mlines - 2, 0, 0, mcols)
284 """Display help screen"""
285 (self.help_win, self.help_panel) = self.panel_create(20, 52)
286 self.help_panel.top()
287 self.help_panel.hide()
288 self.help_win.clear()
289 self.help_win.addstr(
290 1, 1, "LIGHTCARDS HELP", curses.color_pair(1) + curses.A_BOLD
292 self.help_win.hline(2, 1, curses.ACS_HLINE, 15)
294 "Welcome to lightcards. Here are some keybindings",
295 "to get you started:",
297 "h, left previous card",
298 "l, right next card",
299 "j, k, up, down flip card",
301 "0, ^, home go to the start of the deck",
302 "$, end go to the end of the deck",
303 "H, ? open this screen",
304 "e open the input file in $EDITOR",
305 "m open the control menu",
307 "More information can be found in the man page, or",
308 "by running `lightcards --help`.",
310 "Press [q], [H], or [?] to go back.",
313 for t in enumerate(text):
314 self.help_win.addstr(t[0] + 3, 1, t[1])
319 (mlines, mcols) = self.win.getmaxyx()
320 self.help_win.mvwin(int(mlines / 2) - 10, int(mcols / 2) - 26)
322 self.help_panel.show()
324 key = self.help_win.getkey()
325 if key in ["q", "H", "?"]:
326 self.help_panel.hide()
329 def current_card(self):
330 return self.stack[self.obj.getIdx()]
334 Display a card and wait for the input.
335 Used as a general way of getting back into the card flow from a menu
340 key = self.win.getkey()
343 elif key in ["h", "KEY_LEFT"]:
345 self.current_card().setSide(0)
347 elif key in ["l", "KEY_RIGHT"]:
348 if self.obj.getIdx() + 1 == len(self.stack):
351 self.obj.forward(self.stack)
352 self.current_card().setSide(0)
354 elif key in ["j", "k", "KEY_UP", "KEY_DOWN"]:
355 self.current_card().flip()
357 elif key in ["i", "/"]:
358 self.current_card().toggleStar()
360 elif key in ["0", "^", "KEY_HOME"]:
362 self.current_card().setSide(0)
364 elif key in ["$", "KEY_END"]:
365 self.obj.setIdx(len(self.stack) - 1)
366 self.current_card().setSide(0)
368 elif key in ["H", "?"]:
373 (self.headers, self.stack) = lightcards.reparse()
376 def disp_sidebar(self):
377 """Display a sidebar with the starred terms"""
378 (mlines, mcols) = self.win.getmaxyx()
385 curses.color_pair(3) + curses.A_BOLD,
387 self.win.vline(0, mcols - 20, 0, mlines - 2)
388 self.win.hline(1, left, 0, mlines)
391 # TODO: Fix this, some off by one error
392 newntotal = self.ntotal()
393 if mlines - 5 < len(self.ntotal()):
394 newntotal = self.ntotal()[: mlines - 4]
395 elif mlines - 5 == len(self.ntotal()):
396 newntotal = self.ntotal()[: mlines - 3]
399 for i, card in enumerate(newntotal):
400 term = card.getFront()
403 self.win.addstr(2 + i, left, term)
404 if not newntotal == self.ntotal():
408 f"({len(self.ntotal()) - len(newntotal)} more)",
412 if len(self.ntotal()) == 0:
413 self.win.addstr(2, left, "None starred")