1 # Display card output and retreive input
6 from random import shuffle
11 from . import runner, progress
14 def panel_create(x, y):
15 """Create popup panels to a certain scale"""
16 win = curses.newwin(x, y)
17 panel = curses.panel.new_panel(win)
23 def __init__(self, outer, mlines=20, mcols=52):
24 """Initialize help screen"""
26 (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) - 25)
70 key = self.win.getkey()
73 elif key in ["H", "?"]:
79 def __init__(self, outer, mlines=17, mcols=44):
80 """Initialize the menu with content"""
82 (self.win, self.panel) = panel_create(mlines, mcols)
90 curses.color_pair(1) + curses.A_BOLD,
92 self.win.hline(2, 1, curses.ACS_HLINE, mcols)
94 "[y]: reset stack to original state",
95 "[a]: alphabetize stack",
97 "[f]: flip all cards in stack",
98 "[t]: reverse stack order",
101 "[s]: update stack to include starred only",
107 for t in enumerate(text):
108 self.win.addstr(t[0] + 3, 1, t[1])
112 def menu_print(self, string, err=False):
113 """Print messages on the menu screen"""
115 color = curses.color_pair(2)
117 color = curses.color_pair(1)
120 self.win.addch(15, i + 1, " ")
122 self.win.addstr(15, 1, string, color)
126 """Grab keypresses on the menu screen"""
128 key = self.win.getkey()
129 if key in ["r", "m"]:
135 self.outer.stack = runner.get_orig()[1]
136 self.menu_print("Stack reset!")
138 self.outer.stack.sort(key=lambda x: x.front)
139 self.menu_print("Stack alphabetized!")
141 [x.unStar() for x in self.outer.stack]
142 self.menu_print("All unstarred!")
144 [x.star() for x in self.outer.stack]
145 self.menu_print("All starred!")
147 self.outer.stack.reverse()
148 self.menu_print("Stack reversed!")
150 shuffle(self.outer.stack)
151 self.menu_print("Stack shuffled!")
153 for x in self.outer.stack:
154 x.front, x.back = x.back, x.front
155 (self.outer.headers[0], self.outer.headers[1]) = (
156 self.outer.headers[1],
157 self.outer.headers[0],
159 self.menu_print("Cards flipped!")
161 # Check if there are any starred cards before proceeding, and
162 # if not, don't allow to proceed and show an error message
164 for x in self.outer.stack:
171 x for x in self.outer.stack if x.starred
173 self.menu_print("Stars only!")
175 self.menu_print("ERR: None are starred!", err=True)
177 self.outer.obj.index = 0
182 Display a menu offering multiple options on how to manipulate the deck
185 (mlines, mcols) = self.outer.win.getmaxyx()
186 self.win.mvwin(int(mlines / 2) - 9, int(mcols / 2) - 21)
193 def __init__(self, stack, headers, obj):
195 self.headers = headers
198 def run(self, stdscr):
199 """Set important options that require stdscr before starting"""
201 curses.curs_set(0) # Hide cursor
202 curses.use_default_colors() # Allow transparency
203 curses.init_pair(1, curses.COLOR_CYAN, -1)
204 curses.init_pair(2, curses.COLOR_RED, -1)
205 curses.init_pair(3, curses.COLOR_YELLOW, -1)
207 self.main_panel = curses.panel.new_panel(self.win)
208 self.menu_obj = Menu(self)
209 self.help_obj = Help(self)
213 def check_size(self):
214 (mlines, mcols) = self.win.getmaxyx()
216 while mlines < 24 or mcols < 60:
222 "Terminal too small! Min size 60x24", width=mcols
226 (mlines, mcols) = self.win.getmaxyx()
232 """Pickle stack before quitting"""
233 if self.obj.index + 1 == len(self.stack):
236 progress.dump(self.stack, runner.get_orig()[1])
240 """Get total number of starred cards"""
241 return [card for card in self.stack if card.starred]
245 Display the statusbar at the bottom of the screen with progress, star
246 status, and card side.
248 (mlines, mcols) = self.win.getmaxyx()
249 self.win.hline(mlines - 2, 0, 0, mcols)
251 # Calculate percent done
252 if len(self.stack) <= 1:
256 round(self.obj.index / (len(self.stack) - 1) * 100)
259 # Print yellow if starred
260 if self.current_card().starred:
261 star_color = curses.color_pair(3)
263 star_color = curses.color_pair(1)
265 # Create bar component
267 bar_middle = self.current_card().printStar()
269 f"] [{len(self.nstarred())}/{str(len(self.stack))} starred] "
271 f"{str(self.obj.index).zfill(len(str(len(self.stack))))}"
272 f"/{str(len(self.stack))})] ["
273 f"{self.headers[self.current_card().side]} ("
274 f"{str(int(self.current_card().side) + 1)})]"
277 # Put it all togethor
278 self.win.addstr(mlines - 1, 0, bar_start, curses.color_pair(1))
279 self.win.addstr(mlines - 1, len(bar_start), bar_middle, star_color)
282 len(bar_start + bar_middle),
283 textwrap.shorten(bar_end, width=mcols - 20, placeholder="…"),
284 curses.color_pair(1),
287 def wrap_width(self):
288 """Calculate the width at which the body text should wrap"""
289 (_, mcols) = self.win.getmaxyx()
290 wrap_width = mcols - 20
297 Display the contents of the card.
298 Shows a header, a horizontal line, and the contents of the current
301 (_, mcols) = self.win.getmaxyx()
302 self.main_panel.bottom()
305 # If on the back of the card, show the content of the front side in
307 num_done = str(self.obj.index + 1).zfill(len(str(len(self.stack))))
308 if self.current_card().side == 0:
309 top = num_done + " | " + self.headers[self.current_card().side]
314 + self.headers[self.current_card().side]
316 + str(self.current_card().front)
327 textwrap.shorten(top, width=header_width, placeholder="…"),
331 # Add horizontal line
332 self.win.hline(1, 0, curses.ACS_HLINE, mcols)
339 self.current_card().get(),
340 width=self.wrap_width(),
347 def current_card(self):
348 """Get current card object"""
349 return self.stack[self.obj.index]
353 Display a card and wait for the input.
354 Used as a general way of getting back into the card flow from a menu
358 key = self.win.getkey()
361 elif key in ["h", "KEY_LEFT"]:
363 self.current_card().side = 0
365 elif key in ["l", "KEY_RIGHT"]:
366 if self.obj.index + 1 == len(self.stack):
369 self.obj.forward(self.stack)
370 self.current_card().side = 0
372 elif key in ["j", "k", "KEY_UP", "KEY_DOWN"]:
373 self.current_card().flip()
375 elif key in ["i", "/"]:
376 self.current_card().toggleStar()
378 elif key in ["0", "^", "KEY_HOME"]:
380 self.current_card().side = 0
382 elif key in ["$", "KEY_END"]:
383 self.obj.index = len(self.stack) - 1
384 self.current_card().side = 0
386 elif key in ["H", "?"]:
391 (self.headers, self.stack) = runner.reparse()
394 def disp_sidebar(self):
395 """Display a sidebar with the starred terms"""
396 (mlines, mcols) = self.win.getmaxyx()
403 curses.color_pair(3) + curses.A_BOLD,
405 self.win.vline(0, mcols - 20, 0, mlines - 2)
407 nstarred = self.nstarred()
408 if mlines - 5 < len(self.nstarred()):
409 nstarred = self.nstarred()[: mlines - 4]
410 elif mlines - 5 == len(self.nstarred()):
411 nstarred = self.nstarred()[: mlines - 3]
414 for i, card in enumerate(nstarred):
418 self.win.addstr(2 + i, left, term)
419 if not nstarred == self.nstarred():
423 f"({len(self.nstarred()) - len(nstarred)} more)",
427 if len(self.nstarred()) == 0:
428 self.win.addstr(2, left, "None starred")