1 # Display card output and retreive input
6 from random import shuffle
10 from . import runner, progress
13 def panel_create(x, y):
14 """Create popup panels to a certain scale"""
15 win = curses.newwin(x, y)
16 panel = curses.panel.new_panel(win)
22 def __init__(self, outer):
23 """Initialize help screen"""
25 (self.win, self.panel) = panel_create(20, 52)
31 "Welcome to runner. Here are some keybindings",
32 "to get you started:",
34 "h, left previous card",
36 "j, k, up, down flip card",
38 "0, ^, home go to the start of the deck",
39 "$, end go to the end of the deck",
40 "H, ? open this screen",
41 "e open the input file in $EDITOR",
42 "m open the control menu",
44 "More information can be found in the man page, or",
45 "by running `lightcards --help`.",
47 "Press [H], or [?] to go back.",
51 1, 1, "LIGHTCARDS HELP", curses.color_pair(1) + curses.A_BOLD
53 self.win.hline(2, 1, curses.ACS_HLINE, 15)
55 for t in enumerate(text):
56 self.win.addstr(t[0] + 3, 1, t[1])
61 """Display help screen"""
62 (mlines, mcols) = self.outer.win.getmaxyx()
63 self.win.mvwin(int(mlines / 2) - 10, int(mcols / 2) - 26)
64 self.outer.update_panels()
68 key = self.win.getkey()
71 elif key in ["H", "?"]:
77 def __init__(self, outer):
78 """Initialize the menu with content"""
80 (self.win, self.panel) = panel_create(17, 44)
85 1, 1, "LIGHTCARDS MENU", curses.color_pair(1) + curses.A_BOLD
87 self.win.hline(2, 1, curses.ACS_HLINE, 15)
89 "[y]: reset stack to original state",
90 "[a]: alphabetize stack",
92 "[f]: flip all cards in stack",
93 "[t]: reverse stack order",
96 "[s]: update stack to include starred only",
102 for t in enumerate(text):
103 self.win.addstr(t[0] + 3, 1, t[1])
106 self.outer.update_panels()
108 def menu_print(self, string, err=False):
109 """Print messages on the menu screen"""
111 color = curses.color_pair(2)
113 color = curses.color_pair(1)
116 self.win.addch(15, i + 1, " ")
118 self.win.addstr(15, 1, string, color)
119 self.outer.update_panels()
123 """Grab keypresses on the menu screen"""
125 key = self.win.getkey()
126 if key in ["r", "m"]:
128 self.outer.update_panels()
133 self.outer.stack = runner.get_orig()[1]
134 self.menu_print("Stack reset!")
136 self.outer.stack.sort()
137 self.menu_print("Stack alphabetized!")
139 [x.unStar() for x in self.outer.stack]
140 self.menu_print("All unstarred!")
142 [x.star() for x in self.outer.stack]
143 self.menu_print("All starred!")
145 self.outer.stack.reverse()
146 self.menu_print("Stack reversed!")
148 shuffle(self.outer.stack)
149 self.menu_print("Stack shuffled!")
151 for x in self.outer.stack:
152 x.front, x.back = x.back, x.front
153 (self.outer.headers[0], self.outer.headers[1]) = (
154 self.outer.headers[1],
155 self.outer.headers[0],
157 self.menu_print("Cards flipped!")
159 # Check if there are any starred cards before proceeding, and
160 # if not, don't allow to proceed and show an error message
162 for x in self.outer.stack:
169 x for x in self.outer.stack if x.getStar()
171 self.menu_print("Stars only!")
173 self.menu_print("ERR: None are starred!", err=True)
174 elif key in ["h", "KEY_LEFT"]:
175 self.outer.obj.setIdx(len(self.outer.stack) - 1)
178 self.outer.obj.setIdx(0)
183 Display a menu offering multiple options on how to manipulate the deck
186 (mlines, mcols) = self.outer.win.getmaxyx()
187 self.win.mvwin(int(mlines / 2) - 9, int(mcols / 2) - 22)
189 self.outer.update_panels()
195 def __init__(self, stack, headers, obj):
197 self.headers = headers
200 def run(self, stdscr):
201 """Set important options that require stdscr before starting"""
203 (mlines, mcols) = self.win.getmaxyx()
204 curses.curs_set(0) # Hide cursor
205 curses.use_default_colors() # Allow transparency
206 curses.init_pair(1, curses.COLOR_CYAN, -1)
207 curses.init_pair(2, curses.COLOR_RED, -1)
208 curses.init_pair(3, curses.COLOR_YELLOW, -1)
210 (self.main_win, self.main_panel) = panel_create(mlines, mcols)
211 self.menu_obj = Menu(self)
212 self.help_obj = Help(self)
216 def update_panels(self):
217 """Update panel and window contents"""
218 curses.panel.update_panels()
222 """Pickle stack before quitting"""
223 if self.obj.getIdx() + 1 == len(self.stack):
226 progress.dump(self.stack, runner.get_orig()[1])
230 """Get total number of starred cards"""
231 return [card for card in self.stack if card.getStar()]
235 Display the statusbar at the bottom of the screen with progress, star
236 status, and card side.
238 (mlines, _) = self.win.getmaxyx()
240 # Calculate percent done
241 if len(self.stack) <= 1:
245 round(self.obj.getIdx() / (len(self.stack) - 1) * 100)
248 # Print yellow if starred
249 if self.current_card().getStar():
250 star_color = curses.color_pair(3)
252 star_color = curses.color_pair(1)
254 # Create bar component
256 bar_middle = self.current_card().printStar()
258 f"] [{len(self.nstarred())}/{str(len(self.stack))} starred] "
260 f"{str(self.obj.getIdx()).zfill(len(str(len(self.stack))))}"
261 f"/{str(len(self.stack))})] ["
262 f"{self.headers[self.current_card().getSide()]} ("
263 f"{str(int(self.current_card().getSide()) + 1)})]"
266 # Put it all togethor
267 self.win.addstr(mlines - 1, 0, bar_start, curses.color_pair(1))
268 self.win.addstr(mlines - 1, len(bar_start), bar_middle, star_color)
271 len(bar_start + bar_middle),
273 curses.color_pair(1),
276 def wrap_width(self):
277 """Calculate the width at which the body text should wrap"""
278 (_, mcols) = self.win.getmaxyx()
279 wrap_width = mcols - 20
286 Display the contents of the card.
287 Shows a header, a horizontal line, and the contents of the current
290 (mlines, mcols) = self.win.getmaxyx()
291 self.main_panel.bottom()
292 self.main_win.clear()
293 # If on the back of the card, show the content of the front side in
295 num_done = str(self.obj.getIdx() + 1).zfill(len(str(len(self.stack))))
296 if self.current_card().getSide() == 0:
298 num_done + " | " + self.headers[self.current_card().getSide()]
304 + self.headers[self.current_card().getSide()]
306 + str(self.current_card().getFront())
313 self.main_win.addstr(
316 textwrap.shorten(top, width=header_width, placeholder="…"),
320 # Add horizontal line
321 lin_width = header_width
322 if len(top) < header_width:
324 self.main_win.hline(1, 0, curses.ACS_HLINE, lin_width)
327 self.main_win.addstr(
331 self.current_card().get(),
332 width=self.wrap_width(),
338 self.win.hline(mlines - 2, 0, 0, mcols)
340 def current_card(self):
341 """Get current card object"""
342 return self.stack[self.obj.getIdx()]
346 Display a card and wait for the input.
347 Used as a general way of getting back into the card flow from a menu
351 key = self.win.getkey()
354 elif key in ["h", "KEY_LEFT"]:
356 self.current_card().setSide(0)
358 elif key in ["l", "KEY_RIGHT"]:
359 if self.obj.getIdx() + 1 == len(self.stack):
362 self.obj.forward(self.stack)
363 self.current_card().setSide(0)
365 elif key in ["j", "k", "KEY_UP", "KEY_DOWN"]:
366 self.current_card().flip()
368 elif key in ["i", "/"]:
369 self.current_card().toggleStar()
371 elif key in ["0", "^", "KEY_HOME"]:
373 self.current_card().setSide(0)
375 elif key in ["$", "KEY_END"]:
376 self.obj.setIdx(len(self.stack) - 1)
377 self.current_card().setSide(0)
379 elif key in ["H", "?"]:
384 (self.headers, self.stack) = runner.reparse()
387 def disp_sidebar(self):
388 """Display a sidebar with the starred terms"""
389 (mlines, mcols) = self.win.getmaxyx()
396 curses.color_pair(3) + curses.A_BOLD,
398 self.win.vline(0, mcols - 20, 0, mlines - 2)
399 self.win.hline(1, left, 0, mlines)
401 nstarred = self.nstarred()
402 if mlines - 5 < len(self.nstarred()):
403 nstarred = self.nstarred()[: mlines - 4]
404 elif mlines - 5 == len(self.nstarred()):
405 nstarred = self.nstarred()[: mlines - 3]
408 for i, card in enumerate(nstarred):
409 term = card.getFront()
412 self.win.addstr(2 + i, left, term)
413 if not nstarred == self.nstarred():
417 f"({len(self.nstarred()) - len(nstarred)} more)",
421 if len(self.nstarred()) == 0:
422 self.win.addstr(2, left, "None starred")