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() == 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.stack[self.obj.getIdx()].getStar():
74 star_color = curses.color_pair(3)
76 star_color = curses.color_pair(1)
78 # Create bar component
80 bar_middle = self.stack[self.obj.getIdx()].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.stack[self.obj.getIdx()].getSide()]} ("
87 + f"{str(self.stack[self.obj.getIdx()].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():
124 elif len(self.stack) < self.obj.getIdx():
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 if self.obj.getIdx() == len(self.stack):
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(
242 len(str(len(self.stack)))
244 if self.stack[self.obj.getIdx()].getSide() == 0:
248 + self.headers[self.stack[self.obj.getIdx()].getSide()]
254 + self.headers[self.stack[self.obj.getIdx()].getSide()]
256 + str(self.stack[self.obj.getIdx()][0])
263 self.main_win.addstr(
266 textwrap.shorten(top, width=header_width, placeholder="…"),
270 # Add horizontal line
271 lin_width = header_width
272 if len(top) < header_width:
274 self.main_win.hline(1, 0, curses.ACS_HLINE, lin_width)
277 self.main_win.addstr(
281 self.stack[self.obj.getIdx()][
282 self.stack[self.obj.getIdx()].getSide()
284 width=self.wrap_width(),
290 self.win.hline(mlines - 2, 0, 0, mcols)
293 """Display help screen"""
294 (self.help_win, self.help_panel) = self.panel_create(20, 52)
295 self.help_panel.top()
296 self.help_panel.hide()
297 self.help_win.clear()
298 self.help_win.addstr(
299 1, 1, "LIGHTCARDS HELP", curses.color_pair(1) + curses.A_BOLD
301 self.help_win.hline(2, 1, curses.ACS_HLINE, 15)
303 "Welcome to lightcards. Here are some keybindings",
304 "to get you started:",
306 "h, left previous card",
307 "l, right next card",
308 "j, k, up, down flip card",
310 "0, ^, home go to the start of the deck",
311 "$, end go to the end of the deck",
312 "H, ? open this screen",
313 "e open the input file in $EDITOR",
314 "m open the control menu",
316 "More information can be found in the man page, or",
317 "by running `lightcards --help`.",
319 "Press [q], [H], or [?] to go back.",
322 for t in enumerate(text):
323 self.help_win.addstr(t[0] + 3, 1, t[1])
328 (mlines, mcols) = self.win.getmaxyx()
329 self.help_win.mvwin(int(mlines / 2) - 10, int(mcols / 2) - 26)
331 self.help_panel.show()
333 key = self.help_win.getkey()
334 if key in ["q", "H", "?"]:
335 self.help_panel.hide()
340 Display a card and wait for the input.
341 Used as a general way of getting back into the card flow from a menu
346 key = self.win.getkey()
349 elif key in ["h", "KEY_LEFT"]:
351 self.stack[self.obj.getIdx()].setSide(0)
353 elif key in ["l", "KEY_RIGHT"]:
354 self.obj.forward(self.stack)
355 self.stack[self.obj.getIdx()].setSide(0)
357 elif key in ["j", "k", "KEY_UP", "KEY_DOWN"]:
358 self.stack[self.obj.getIdx()].flip()
360 elif key in ["i", "/"]:
361 self.stack[self.obj.getIdx()].toggleStar()
363 elif key in ["0", "^", "KEY_HOME"]:
365 self.stack[self.obj.getIdx()].setSide(0)
367 elif key in ["$", "KEY_END"]:
368 self.obj.setIdx(len(self.stack) - 1)
369 self.stack[self.obj.getIdx()].setSide(0)
371 elif key in ["H", "?"]:
376 (self.headers, self.stack) = lightcards.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]
401 for card in newntotal:
402 for i in enumerate(newntotal):
404 if len(i[1][0]) > 18:
405 term = i[1][0][:18] + "…"
406 self.win.addstr(2 + i[0], 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")