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, mlines=20, mcols=52):
23 """Initialize help screen"""
25 (self.win, self.panel) = panel_create(mlines, mcols)
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.",
54 curses.color_pair(1) + curses.A_BOLD,
56 self.win.hline(2, 1, curses.ACS_HLINE, mcols)
58 for t in enumerate(text):
59 self.win.addstr(t[0] + 3, 1, t[1])
64 """Display help screen"""
65 (mlines, mcols) = self.outer.win.getmaxyx()
66 self.win.mvwin(int(mlines / 2) - 10, int(mcols / 2) - 26)
67 self.outer.update_panels()
71 key = self.win.getkey()
74 elif key in ["H", "?"]:
80 def __init__(self, outer, mlines=17, mcols=44):
81 """Initialize the menu with content"""
83 (self.win, self.panel) = panel_create(mlines, mcols)
91 curses.color_pair(1) + curses.A_BOLD,
93 self.win.hline(2, 1, curses.ACS_HLINE, mcols)
95 "[y]: reset stack to original state",
96 "[a]: alphabetize stack",
98 "[f]: flip all cards in stack",
99 "[t]: reverse stack order",
102 "[s]: update stack to include starred only",
108 for t in enumerate(text):
109 self.win.addstr(t[0] + 3, 1, t[1])
112 self.outer.update_panels()
114 def menu_print(self, string, err=False):
115 """Print messages on the menu screen"""
117 color = curses.color_pair(2)
119 color = curses.color_pair(1)
122 self.win.addch(15, i + 1, " ")
124 self.win.addstr(15, 1, string, color)
125 self.outer.update_panels()
129 """Grab keypresses on the menu screen"""
131 key = self.win.getkey()
132 if key in ["r", "m"]:
134 self.outer.update_panels()
139 self.outer.stack = runner.get_orig()[1]
140 self.menu_print("Stack reset!")
142 self.outer.stack.sort(key=lambda x: x.front)
143 self.menu_print("Stack alphabetized!")
145 [x.unStar() for x in self.outer.stack]
146 self.menu_print("All unstarred!")
148 [x.star() for x in self.outer.stack]
149 self.menu_print("All starred!")
151 self.outer.stack.reverse()
152 self.menu_print("Stack reversed!")
154 shuffle(self.outer.stack)
155 self.menu_print("Stack shuffled!")
157 for x in self.outer.stack:
158 x.front, x.back = x.back, x.front
159 (self.outer.headers[0], self.outer.headers[1]) = (
160 self.outer.headers[1],
161 self.outer.headers[0],
163 self.menu_print("Cards flipped!")
165 # Check if there are any starred cards before proceeding, and
166 # if not, don't allow to proceed and show an error message
168 for x in self.outer.stack:
175 x for x in self.outer.stack if x.starred
177 self.menu_print("Stars only!")
179 self.menu_print("ERR: None are starred!", err=True)
180 elif key in ["h", "KEY_LEFT"]:
181 self.outer.obj.index = len(self.outer.stack) - 1
184 self.outer.obj.index = 0
189 Display a menu offering multiple options on how to manipulate the deck
192 (mlines, mcols) = self.outer.win.getmaxyx()
193 self.win.mvwin(int(mlines / 2) - 9, int(mcols / 2) - 22)
195 self.outer.update_panels()
201 def __init__(self, stack, headers, obj):
203 self.headers = headers
206 def run(self, stdscr):
207 """Set important options that require stdscr before starting"""
209 (mlines, mcols) = self.win.getmaxyx()
210 curses.curs_set(0) # Hide cursor
211 curses.use_default_colors() # Allow transparency
212 curses.init_pair(1, curses.COLOR_CYAN, -1)
213 curses.init_pair(2, curses.COLOR_RED, -1)
214 curses.init_pair(3, curses.COLOR_YELLOW, -1)
216 (self.main_win, self.main_panel) = panel_create(mlines, mcols)
217 self.menu_obj = Menu(self)
218 self.help_obj = Help(self)
222 def update_panels(self):
223 """Update panel and window contents"""
224 curses.panel.update_panels()
228 """Pickle stack before quitting"""
229 if self.obj.index + 1 == len(self.stack):
232 progress.dump(self.stack, runner.get_orig()[1])
236 """Get total number of starred cards"""
237 return [card for card in self.stack if card.starred]
241 Display the statusbar at the bottom of the screen with progress, star
242 status, and card side.
244 (mlines, mcols) = self.win.getmaxyx()
245 self.win.hline(mlines - 2, 0, 0, mcols)
247 # Calculate percent done
248 if len(self.stack) <= 1:
252 round(self.obj.index / (len(self.stack) - 1) * 100)
255 # Print yellow if starred
256 if self.current_card().starred:
257 star_color = curses.color_pair(3)
259 star_color = curses.color_pair(1)
261 # Create bar component
263 bar_middle = self.current_card().printStar()
265 f"] [{len(self.nstarred())}/{str(len(self.stack))} starred] "
267 f"{str(self.obj.index).zfill(len(str(len(self.stack))))}"
268 f"/{str(len(self.stack))})] ["
269 f"{self.headers[self.current_card().side]} ("
270 f"{str(int(self.current_card().side) + 1)})]"
273 # Put it all togethor
274 self.win.addstr(mlines - 1, 0, bar_start, curses.color_pair(1))
275 self.win.addstr(mlines - 1, len(bar_start), bar_middle, star_color)
278 len(bar_start + bar_middle),
279 textwrap.shorten(bar_end, width=mcols - 20, placeholder="…"),
280 curses.color_pair(1),
283 def wrap_width(self):
284 """Calculate the width at which the body text should wrap"""
285 (_, mcols) = self.win.getmaxyx()
286 wrap_width = mcols - 20
293 Display the contents of the card.
294 Shows a header, a horizontal line, and the contents of the current
297 (mlines, mcols) = self.win.getmaxyx()
298 self.main_panel.bottom()
299 self.main_win.clear()
300 # If on the back of the card, show the content of the front side in
302 num_done = str(self.obj.index + 1).zfill(len(str(len(self.stack))))
303 if self.current_card().side == 0:
304 top = num_done + " | " + self.headers[self.current_card().side]
309 + self.headers[self.current_card().side]
311 + str(self.current_card().front)
318 self.main_win.addstr(
321 textwrap.shorten(top, width=header_width, placeholder="…"),
325 # Add horizontal line
326 self.main_win.hline(1, 0, curses.ACS_HLINE, mcols)
329 self.main_win.addstr(
333 self.current_card().get(),
334 width=self.wrap_width(),
341 def current_card(self):
342 """Get current card object"""
343 return self.stack[self.obj.index]
347 Display a card and wait for the input.
348 Used as a general way of getting back into the card flow from a menu
352 key = self.win.getkey()
355 elif key in ["h", "KEY_LEFT"]:
357 self.current_card().side = 0
359 elif key in ["l", "KEY_RIGHT"]:
360 if self.obj.index + 1 == len(self.stack):
363 self.obj.forward(self.stack)
364 self.current_card().side = 0
366 elif key in ["j", "k", "KEY_UP", "KEY_DOWN"]:
367 self.current_card().flip()
369 elif key in ["i", "/"]:
370 self.current_card().toggleStar()
372 elif key in ["0", "^", "KEY_HOME"]:
374 self.current_card().side = 0
376 elif key in ["$", "KEY_END"]:
377 self.obj.index = len(self.stack) - 1
378 self.current_card().side = 0
380 elif key in ["H", "?"]:
385 (self.headers, self.stack) = runner.reparse()
388 def disp_sidebar(self):
389 """Display a sidebar with the starred terms"""
390 (mlines, mcols) = self.win.getmaxyx()
397 curses.color_pair(3) + curses.A_BOLD,
399 self.win.vline(0, mcols - 20, 0, mlines - 2)
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):
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")