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.starred
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.index = len(self.outer.stack) - 1
178 self.outer.obj.index = 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.index + 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.starred]
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.index / (len(self.stack) - 1) * 100)
248 # Print yellow if starred
249 if self.current_card().starred:
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.index).zfill(len(str(len(self.stack))))}"
261 f"/{str(len(self.stack))})] ["
262 f"{self.headers[self.current_card().side]} ("
263 f"{str(int(self.current_card().side) + 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.index + 1).zfill(len(str(len(self.stack))))
296 if self.current_card().side == 0:
297 top = num_done + " | " + self.headers[self.current_card().side]
302 + self.headers[self.current_card().side]
304 + str(self.current_card().front)
311 self.main_win.addstr(
314 textwrap.shorten(top, width=header_width, placeholder="…"),
318 # Add horizontal line
319 lin_width = header_width
320 if len(top) < header_width:
322 self.main_win.hline(1, 0, curses.ACS_HLINE, lin_width)
325 self.main_win.addstr(
329 self.current_card().get(),
330 width=self.wrap_width(),
336 self.win.hline(mlines - 2, 0, 0, mcols)
338 def current_card(self):
339 """Get current card object"""
340 return self.stack[self.obj.index]
344 Display a card and wait for the input.
345 Used as a general way of getting back into the card flow from a menu
349 key = self.win.getkey()
352 elif key in ["h", "KEY_LEFT"]:
354 self.current_card().side = 0
356 elif key in ["l", "KEY_RIGHT"]:
357 if self.obj.index + 1 == len(self.stack):
360 self.obj.forward(self.stack)
361 self.current_card().side = 0
363 elif key in ["j", "k", "KEY_UP", "KEY_DOWN"]:
364 self.current_card().flip()
366 elif key in ["i", "/"]:
367 self.current_card().toggleStar()
369 elif key in ["0", "^", "KEY_HOME"]:
371 self.current_card().side = 0
373 elif key in ["$", "KEY_END"]:
374 self.obj.index = len(self.stack) - 1
375 self.current_card().side = 0
377 elif key in ["H", "?"]:
382 (self.headers, self.stack) = runner.reparse()
385 def disp_sidebar(self):
386 """Display a sidebar with the starred terms"""
387 (mlines, mcols) = self.win.getmaxyx()
394 curses.color_pair(3) + curses.A_BOLD,
396 self.win.vline(0, mcols - 20, 0, mlines - 2)
397 self.win.hline(1, left, 0, mlines)
399 nstarred = self.nstarred()
400 if mlines - 5 < len(self.nstarred()):
401 nstarred = self.nstarred()[: mlines - 4]
402 elif mlines - 5 == len(self.nstarred()):
403 nstarred = self.nstarred()[: mlines - 3]
406 for i, card in enumerate(nstarred):
410 self.win.addstr(2 + i, left, term)
411 if not nstarred == self.nstarred():
415 f"({len(self.nstarred()) - len(nstarred)} more)",
419 if len(self.nstarred()) == 0:
420 self.win.addstr(2, left, "None starred")