1 # Display card output and retreive input
6 from random import shuffle
10 from . import lightcards, progress
14 def __init__(self, stack, headers, obj):
16 self.headers = headers
19 def run(self, stdscr):
20 """Set important options before beginning"""
22 (mlines, mcols) = self.win.getmaxyx()
23 curses.curs_set(0) # Hide cursor
24 curses.init_pair(1, curses.COLOR_CYAN, 0)
25 curses.init_pair(2, curses.COLOR_RED, 0)
26 curses.init_pair(3, curses.COLOR_YELLOW, 0)
28 (self.main_win, self.main_panel) = self.panel_create(mlines, mcols)
34 def panel_create(self, x, y):
35 """Create popup menus"""
36 win = curses.newwin(x, y)
37 panel = curses.panel.new_panel(win)
42 curses.panel.update_panels()
46 """Pickle stack before quitting"""
47 if self.obj.getIdx() == len(self.stack):
50 progress.dump(self.stack, lightcards.get_orig())
54 """Get toal number of starred cards"""
55 return([card for card in self.stack if card.getStar()])
59 Display the statusbar at the bottom of the screen with progress, star
60 status, and card side.
62 (mlines, mcols) = self.win.getmaxyx()
64 # Calculate percent done
65 if len(self.stack) <= 1:
68 percent = str(round(self.obj.getIdx() /
69 len(self.stack) * 100)).zfill(2)
71 # Print yellow if starred
72 if self.stack[self.obj.getIdx()].getStar():
73 star_color = curses.color_pair(3)
75 star_color = curses.color_pair(1)
77 # Create bar component
79 bar_middle = self.stack[self.obj.getIdx()].printStar()
80 bar_end = f"] [{len(self.ntotal())}/{str(len(self.stack))} starred] " + \
82 str(self.obj.getIdx() + 1).zfill(len(str(len(self.stack)))) + \
83 f"/{str(len(self.stack))})] [" + \
84 f"{self.headers[self.obj.getSide()]} (" + \
85 f"{str(self.obj.getSide() + 1)})] "
88 self.win.addstr(mlines - 1, 0, bar_start, curses.color_pair(1))
89 self.win.addstr(mlines - 1, len(bar_start), bar_middle, star_color)
90 self.win.addstr(mlines - 1, len(bar_start + bar_middle),
91 bar_end, curses.color_pair(1))
93 def menu_print(self, string, err=False):
94 """Print messages on the menu screen"""
96 color = curses.color_pair(2)
98 color = curses.color_pair(1)
101 self.menu_win.addch(15, i+1, " ")
103 self.menu_win.addstr(15, 1, string, color)
108 """Grab keypresses for the menu screen"""
110 key = self.win.getkey()
111 if key in ["r", "q", "m"]:
112 self.menu_panel.hide()
114 if key in ["q", "m"]:
115 if len(self.stack) == self.obj.getIdx():
117 elif len(self.stack) < self.obj.getIdx():
121 self.stack = lightcards.get_orig()[1]
122 self.menu_print("Stack reset!")
125 self.menu_print("Stack alphabetized!")
127 [x.unStar() for x in self.stack]
128 self.menu_print("All unstarred!")
130 [x.star() for x in self.stack]
131 self.menu_print("All starred!")
138 self.menu_print("Stack shuffled!")
141 x[0], x[1] = x[1], x[0]
142 (self.headers[0], self.headers[1]) = (self.headers[1],
144 self.menu_print("Cards flipped!")
146 # Check if there are any starred cards before proceeding, and
147 # if not, don't allow to proceed and show an error message
155 self.stack = [x for x in self.stack if x.getStar()]
156 self.menu_print("Stars only!")
158 self.menu_print("ERR: None are starred!", err=True)
159 elif key in ["h", "KEY_LEFT"]:
160 self.obj.setIdx(len(self.stack) - 1)
166 def menu_init(self, quit=True):
167 (self.menu_win, self.menu_panel) = self.panel_create(17, 44)
168 self.menu_panel.top()
169 self.menu_panel.hide()
171 quit_text = "[q]: back"
173 quit_text = "[q]: quit"
175 self.menu_win.addstr(1, 1, "LIGHTCARDS MENU", curses.color_pair(1) +
177 self.menu_win.hline(2, 1, curses.ACS_HLINE, 15)
178 text = ["[y]: reset stack to original state",
179 "[a]: alphabetize stack",
180 "[z]: shuffle stack",
181 "[f]: flip all cards in stack",
182 "[t]: reverse stack order",
185 "[s]: update stack to include starred only"]
187 for t in enumerate(text):
188 self.menu_win.addstr(t[0] + 3, 1, t[1])
189 self.menu_win.addstr(len(text) + 4, 1, "[r]: restart")
190 self.menu_win.addstr(len(text) + 5, 1, quit_text)
195 def disp_menu(self, keygrab=True):
197 Display a menu once the end of the deck has been reached, offering
198 multiple options on how to continue.
200 (mlines, mcols) = self.win.getmaxyx()
201 self.menu_win.mvwin(int(mlines/2) - 9, int(mcols/2) - 22)
202 self.menu_panel.show()
208 def wrap_width(self):
209 """Calculate the width at which the body should wrap"""
210 (_, mcols) = self.win.getmaxyx()
211 wrap_width = mcols - 20
218 Display the contents of the card
219 Shows a header, a horizontal line, and the contents of the current
222 (mlines, mcols) = self.win.getmaxyx()
223 if self.obj.getIdx() == len(self.stack):
226 self.main_panel.bottom()
227 self.main_win.clear()
228 # If on the back of the card, show the content of the front side in
230 num_done = str(self.obj.getIdx() +
231 1).zfill(len(str(len(self.stack))))
232 if self.obj.getSide() == 0:
233 top = num_done + " | " + self.headers[self.obj.getSide()]
235 top = num_done + " | " + self.headers[self.obj.getSide()] + \
236 " | \"" + str(self.stack[self.obj.getIdx()][0]) + "\""
241 self.main_win.addstr(0, 0, textwrap.shorten(top, width=header_width,
242 placeholder="…"), curses.A_BOLD)
244 # Add horizontal line
245 lin_width = header_width
246 if len(top) < header_width:
248 self.main_win.hline(1, 0, curses.ACS_HLINE, lin_width)
251 self.main_win.addstr(2, 0, textwrap.fill(
252 self.stack[self.obj.getIdx()][self.obj.getSide()],
253 width=self.wrap_width()))
257 self.win.hline(mlines - 2, 0, 0, mcols)
260 """Display help screen"""
261 (self.help_win, self.help_panel) = self.panel_create(20, 52)
262 self.help_panel.top()
263 self.help_panel.hide()
264 self.help_win.clear()
265 self.help_win.addstr(1, 1, "LIGHTCARDS HELP", curses.color_pair(1) +
267 self.help_win.hline(2, 1, curses.ACS_HLINE, 15)
268 text = ["Welcome to lightcards. Here are some keybindings",
269 "to get you started:",
271 "h, left previous card",
272 "l, right next card",
273 "j, k, up, down flip card",
275 "0, ^, home go to the start of the deck",
276 "$, end go to the end of the deck",
277 "H, ? open this screen",
278 "e open the input file in $EDITOR",
279 "m open the control menu",
281 "More information can be found in the man page, or",
282 "by running `lightcards --help`.",
284 "Press [q], [H], or [?] to go back."]
286 for t in enumerate(text):
287 self.help_win.addstr(t[0] + 3, 1, t[1])
292 (mlines, mcols) = self.win.getmaxyx()
293 self.help_win.mvwin(int(mlines/2) - 10, int(mcols/2) - 26)
295 self.help_panel.show()
297 key = self.help_win.getkey()
298 if key in ["q", "H", "?"]:
299 self.help_panel.hide()
304 Display a card and wait for the input.
305 Used as a general way of getting back into the card flow from a menu
310 key = self.win.getkey()
313 elif key in ["l", "KEY_RIGHT"]:
314 self.obj.forward(self.stack)
317 elif key in ["j", "k", "KEY_UP", "KEY_DOWN"]:
320 elif key in ["i", "/"]:
321 self.stack[self.obj.getIdx()].toggleStar()
323 elif key in ["0", "^", "KEY_HOME"]:
327 elif key in ["$", "KEY_END"]:
328 self.obj.setIdx(len(self.stack) - 1)
331 elif key in ["H", "?"]:
336 (self.headers, self.stack) = lightcards.reparse()
339 def disp_sidebar(self):
340 """Display a sidebar with the starred terms"""
341 (mlines, mcols) = self.win.getmaxyx()
344 self.win.addstr(0, mcols - 16, "STARRED CARDS",
345 curses.color_pair(3) + curses.A_BOLD)
346 self.win.vline(0, mcols - 20, 0, mlines - 2)
347 self.win.hline(1, left, 0, mlines)
350 # TODO: Fix this, some off by one error
351 newntotal = self.ntotal()
352 if mlines - 5 < len(self.ntotal()):
353 newntotal = self.ntotal()[:mlines - 4]
354 elif mlines - 5 == len(self.ntotal()):
355 newntotal = self.ntotal()[:mlines - 3]
357 for card in newntotal:
358 for i in enumerate(newntotal):
360 if len(i[1][0]) > 18:
361 term = i[1][0][:18] + "…"
362 self.win.addstr(2 + i[0], left, term)
363 if not newntotal == self.ntotal():
364 self.win.addstr(mlines - 3, left, f"({len(self.ntotal()) - len(newntotal)} more)")
367 if len(self.ntotal()) == 0:
368 self.win.addstr(2, left, "None starred")