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", "m"]:
121 self.menu_panel.hide()
127 self.stack = runner.get_orig()[1]
128 self.menu_print("Stack reset!")
131 self.menu_print("Stack alphabetized!")
133 [x.unStar() for x in self.stack]
134 self.menu_print("All unstarred!")
136 [x.star() for x in self.stack]
137 self.menu_print("All starred!")
140 self.menu_print("Stack reversed!")
143 self.menu_print("Stack shuffled!")
146 x[0], x[1] = x[1], x[0]
147 (self.headers[0], self.headers[1]) = (
151 self.menu_print("Cards flipped!")
153 # Check if there are any starred cards before proceeding, and
154 # if not, don't allow to proceed and show an error message
162 self.stack = [x for x in self.stack if x.getStar()]
163 self.menu_print("Stars only!")
165 self.menu_print("ERR: None are starred!", err=True)
166 elif key in ["h", "KEY_LEFT"]:
167 self.obj.setIdx(len(self.stack) - 1)
174 """Initialize the menu with content"""
175 (self.menu_win, self.menu_panel) = self.panel_create(17, 44)
176 self.menu_panel.top()
177 self.menu_panel.hide()
179 self.menu_win.addstr(
180 1, 1, "LIGHTCARDS MENU", curses.color_pair(1) + curses.A_BOLD
182 self.menu_win.hline(2, 1, curses.ACS_HLINE, 15)
184 "[y]: reset stack to original state",
185 "[a]: alphabetize stack",
186 "[z]: shuffle stack",
187 "[f]: flip all cards in stack",
188 "[t]: reverse stack order",
191 "[s]: update stack to include starred only",
197 for t in enumerate(text):
198 self.menu_win.addstr(t[0] + 3, 1, t[1])
203 def disp_menu(self, keygrab=True):
205 Display a menu offering multiple options on how to manipulate the deck
208 (mlines, mcols) = self.win.getmaxyx()
209 self.menu_win.mvwin(int(mlines / 2) - 9, int(mcols / 2) - 22)
210 self.menu_panel.show()
216 def wrap_width(self):
217 """Calculate the width at which the body text should wrap"""
218 (_, mcols) = self.win.getmaxyx()
219 wrap_width = mcols - 20
226 Display the contents of the card.
227 Shows a header, a horizontal line, and the contents of the current
230 (mlines, mcols) = self.win.getmaxyx()
231 self.main_panel.bottom()
232 self.main_win.clear()
233 # If on the back of the card, show the content of the front side in
235 num_done = str(self.obj.getIdx() + 1).zfill(len(str(len(self.stack))))
236 if self.current_card().getSide() == 0:
238 num_done + " | " + self.headers[self.current_card().getSide()]
244 + self.headers[self.current_card().getSide()]
246 + str(self.current_card().getFront())
253 self.main_win.addstr(
256 textwrap.shorten(top, width=header_width, placeholder="…"),
260 # Add horizontal line
261 lin_width = header_width
262 if len(top) < header_width:
264 self.main_win.hline(1, 0, curses.ACS_HLINE, lin_width)
267 self.main_win.addstr(
271 self.current_card().get(),
272 width=self.wrap_width(),
278 self.win.hline(mlines - 2, 0, 0, mcols)
281 """Initialize help screen"""
282 (self.help_win, self.help_panel) = self.panel_create(20, 52)
283 self.help_panel.top()
284 self.help_panel.hide()
285 self.help_win.clear()
286 self.help_win.addstr(
287 1, 1, "LIGHTCARDS HELP", curses.color_pair(1) + curses.A_BOLD
289 self.help_win.hline(2, 1, curses.ACS_HLINE, 15)
291 "Welcome to runner. Here are some keybindings",
292 "to get you started:",
294 "h, left previous card",
295 "l, right next card",
296 "j, k, up, down flip card",
298 "0, ^, home go to the start of the deck",
299 "$, end go to the end of the deck",
300 "H, ? open this screen",
301 "e open the input file in $EDITOR",
302 "m open the control menu",
304 "More information can be found in the man page, or",
305 "by running `lightcards --help`.",
307 "Press [H], or [?] to go back.",
310 for t in enumerate(text):
311 self.help_win.addstr(t[0] + 3, 1, t[1])
316 """Display help screen"""
317 (mlines, mcols) = self.win.getmaxyx()
318 self.help_win.mvwin(int(mlines / 2) - 10, int(mcols / 2) - 26)
320 self.help_panel.show()
322 key = self.help_win.getkey()
325 elif key in ["H", "?"]:
326 self.help_panel.hide()
329 def current_card(self):
330 """Get current card object"""
331 return self.stack[self.obj.getIdx()]
335 Display a card and wait for the input.
336 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) = runner.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")