1 # Display card output and retreive input
7 from random import shuffle
12 from . import runner, progress, parse
15 def panel_create(x, y):
16 """Create popup panels to a certain scale"""
17 win = curses.newwin(x, y)
18 panel = curses.panel.new_panel(win)
23 class CursesError(BaseException):
24 def __init__(self, message="lightcards: Curses error!"):
25 self.message = message
31 def __init__(self, outer, mlines=20, mcols=52):
32 """Initialize help screen"""
34 (self.win, self.panel) = panel_create(mlines, mcols)
39 "Welcome to runner. Here are some keybindings",
40 "to get you started:",
42 "h, left previous card",
44 "j, k, up, down flip card",
46 "0, ^, home go to the start of the deck",
47 "$, end go to the end of the deck",
48 "H, ? open this screen",
49 "m open the control menu",
50 "1, 2, 3 switch views",
52 "More information can be found in the man page, or",
53 "by running `lightcards --help`.",
55 "Press [H], or [?] to go back.",
62 curses.color_pair(1) + curses.A_BOLD,
64 self.win.hline(2, 1, curses.ACS_HLINE, mcols)
66 for t in enumerate(text):
67 self.win.addstr(t[0] + 3, 1, t[1])
72 """Display help screen"""
73 (mlines, mcols) = self.outer.win.getmaxyx()
74 self.win.mvwin(int(mlines / 2) - 10, int(mcols / 2) - 25)
78 key = self.win.getkey()
81 elif key in ["H", "?"]:
87 def __init__(self, outer, mlines=17, mcols=44):
88 """Initialize the menu with content"""
90 (self.win, self.panel) = panel_create(mlines, mcols)
98 curses.color_pair(1) + curses.A_BOLD,
100 self.win.hline(2, 1, curses.ACS_HLINE, mcols)
102 "[y]: reset stack to original state",
103 "[a]: alphabetize stack",
104 "[z]: shuffle stack",
105 "[t]: reverse stack order",
108 "[s]: update stack to include starred only",
109 "[e]: open the input file in $EDITOR",
115 for t in enumerate(text):
116 self.win.addstr(t[0] + 3, 1, t[1])
120 def menu_print(self, string, err=False):
121 """Print messages on the menu screen"""
123 color = curses.color_pair(2)
125 color = curses.color_pair(1)
127 self.win.addstr(15, 1, string, color)
131 """Grab keypresses on the menu screen"""
133 key = self.win.getkey()
134 if key in ["r", "m"]:
140 self.outer.stack = runner.get_orig()[1]
141 self.menu_print("Stack reset!")
143 self.outer.stack.sort(key=lambda x: x.front)
144 self.menu_print("Stack alphabetized!")
146 [x.unStar() for x in self.outer.stack]
147 self.menu_print("All unstarred!")
149 [x.star() for x in self.outer.stack]
150 self.menu_print("All starred!")
152 self.outer.stack.reverse()
153 self.menu_print("Stack reversed!")
155 shuffle(self.outer.stack)
156 self.menu_print("Stack shuffled!")
159 os.system(f"$EDITOR {self.outer.input_file}"),
160 (self.outer.headers, self.outer.stack) = parse.parse_html(
161 parse.md2html(self.outer.input_file)
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)
181 self.outer.obj.index = 0
186 Display a menu offering multiple options on how to manipulate the deck
190 self.win.addch(14, i + 1, " ")
192 (mlines, mcols) = self.outer.win.getmaxyx()
193 self.win.mvwin(int(mlines / 2) - 8, int(mcols / 2) - 22)
200 def __init__(self, stack, headers, obj, view, input_file):
202 self.headers = headers
205 self.input_file = input_file
207 def run(self, stdscr):
208 """Set important options that require stdscr before starting"""
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_panel = curses.panel.new_panel(self.win)
217 self.menu_obj = Menu(self)
218 self.help_obj = Help(self)
222 def check_size(self):
223 (mlines, mcols) = self.win.getmaxyx()
225 while mlines < 24 or mcols < 60:
231 "Terminal too small! Min size 60x24", width=mcols
235 (mlines, mcols) = self.win.getmaxyx()
241 """Pickle stack before quitting"""
242 if self.obj.index + 1 == len(self.stack):
245 progress.dump(self.stack, runner.get_orig()[1])
249 """Get total number of starred cards"""
250 return [card for card in self.stack if card.starred]
254 Display the statusbar at the bottom of the screen with progress, star
255 status, and card side.
257 (mlines, mcols) = self.win.getmaxyx()
258 self.win.hline(mlines - 2, 0, 0, mcols)
260 # Calculate percent done
261 if len(self.stack) <= 1:
265 round(self.obj.index / (len(self.stack) - 1) * 100)
268 # Print yellow if starred
269 if self.current_card().starred:
270 star_color = curses.color_pair(3)
272 star_color = curses.color_pair(1)
276 bar_middle = self.current_card().printStar()
278 f"] [{len(self.nstarred())}/{str(len(self.stack))} starred] "
280 f"{str(self.obj.index).zfill(len(str(len(self.stack))))}"
281 f"/{str(len(self.stack))})]"
285 f" [{self.get_side()} ("
286 f"{str(int(self.current_card().side) + 1)})]"
288 bar_end += f" [View {str(self.view)}]"
290 # Put it all togethor
291 self.win.addstr(mlines - 1, 0, bar_start, curses.color_pair(1))
292 self.win.addstr(mlines - 1, len(bar_start), bar_middle, star_color)
295 len(bar_start + bar_middle),
296 textwrap.shorten(bar_end, width=mcols - 20, placeholder="…"),
297 curses.color_pair(1),
300 def wrap_width(self):
301 """Calculate the width at which the body text should wrap"""
302 (_, mcols) = self.win.getmaxyx()
303 wrap_width = mcols - 20
309 if self.obj.side == 0:
310 return self.headers[self.current_card().side]
312 return self.headers[self.current_card().get_reverse()]
315 (_, mcols) = self.win.getmaxyx()
316 self.main_panel.bottom()
318 num_done = str(self.obj.index + 1).zfill(len(str(len(self.stack))))
320 if self.view in [1, 2, 4]:
322 Display the contents of the card.
323 Shows a header, a horizontal line, and the contents of the current
326 # If on the back of the card, show the content of the front side in
333 if self.current_card().side == 0:
334 top = num_done + " | " + self.get_side()
341 + str(self.current_card().get()[self.obj.get_reverse()])
348 textwrap.shorten(top, width=mcols - 20, placeholder="…"),
357 self.current_card().get()[self.obj.side],
358 width=self.wrap_width(),
364 Display the contents of the card with both the front and back sides.
366 (_, mcols) = self.win.getmaxyx()
367 self.main_panel.bottom()
386 self.headers[0] + ": " + self.current_card().front,
387 width=self.wrap_width(),
391 self.headers[1] + ": " + self.current_card().back,
392 width=self.wrap_width(),
396 self.win.hline(1, 0, curses.ACS_HLINE, mcols)
400 def current_card(self):
401 """Get current card object"""
402 return self.stack[self.obj.index]
406 Display a card and wait for the input.
407 Used as a general way of getting back into the card flow from a menu
411 key = self.win.getkey()
414 elif key in ["h", "KEY_LEFT"]:
416 self.current_card().side = 0
418 elif key in ["l", "KEY_RIGHT"]:
419 if self.obj.index + 1 == len(self.stack):
422 self.obj.forward(self.stack)
423 self.current_card().side = 0
425 elif key in ["j", "k", "KEY_UP", "KEY_DOWN"] and self.view != 3:
426 self.current_card().flip()
428 elif key in ["i", "/"]:
429 self.current_card().toggleStar()
431 elif key in ["0", "^", "KEY_HOME"]:
433 self.current_card().side = 0
435 elif key in ["$", "KEY_END"]:
436 self.obj.index = len(self.stack) - 1
437 self.current_card().side = 0
439 elif key in ["H", "?"]:
443 elif key in ["1", "2", "3", "4"]:
446 def disp_sidebar(self):
447 """Display a sidebar with the starred terms"""
448 (mlines, mcols) = self.win.getmaxyx()
455 curses.color_pair(3) + curses.A_BOLD,
457 self.win.vline(0, mcols - 20, 0, mlines - 2)
459 nstarred = self.nstarred()
460 for i, card in enumerate(nstarred):
461 term = card.get()[self.obj.side]
463 term = term[:18] + "…"
467 self.win.addch(mlines - 3, left + i, " ")
472 f"({len(nstarred) - i - 2} more)",
475 self.win.addstr(2 + i, left, term)
477 if len(self.nstarred()) == 0:
478 self.win.addstr(2, left, "None starred")