1 # Display card output and retreive input
5 from random import shuffle
9 from . import lightcards, progress
13 def __init__(self, stack, headers, obj):
15 self.headers = headers
18 def run(self, stdscr):
19 """Set important options before beginning"""
21 curses.curs_set(0) # Hide cursor
22 curses.init_pair(1, curses.COLOR_CYAN, 0)
23 curses.init_pair(2, curses.COLOR_RED, 0)
24 curses.init_pair(3, curses.COLOR_YELLOW, 0)
28 """Pickle stack before quitting"""
29 if self.obj.getIdx() == len(self.stack):
32 progress.dump(self.stack, lightcards.get_orig())
36 """Get toal number of starred cards"""
37 return(len([card for card in self.stack if card.getStar()]))
41 Display the statusbar at the bottom of the screen with progress, star
42 status, and card side.
44 (mlines, mcols) = self.win.getmaxyx()
46 # Calculate percent done
47 if len(self.stack) <= 1:
50 percent = str(round(self.obj.getIdx() /
51 len(self.stack) * 100)).zfill(2)
53 # Print yellow if starred
54 if self.stack[self.obj.getIdx()].getStar():
55 star_color = curses.color_pair(3)
57 star_color = curses.color_pair(1)
59 # Create bar component
61 bar_middle = self.stack[self.obj.getIdx()].printStar()
62 bar_end = f"] [{self.ntotal()}/{str(len(self.stack))} starred] " + \
64 str(self.obj.getIdx() + 1).zfill(len(str(len(self.stack)))) + \
65 f"/{str(len(self.stack))})] [" + \
66 f"{self.headers[self.obj.getSide()]} (" + \
67 f"{str(self.obj.getSide() + 1)})] "
70 self.win.hline(mlines - 2, 0, 0, mcols)
71 self.win.addstr(mlines - 1, 0, bar_start, curses.color_pair(1))
72 self.win.addstr(bar_middle, star_color)
73 self.win.insstr(bar_end, curses.color_pair(1))
75 def menu_print(self, string, err=False):
76 """Print messages on the menu screen"""
79 color = curses.color_pair(2)
81 color = curses.color_pair(1)
82 self.disp_menu(keygrab=False)
83 self.win.addstr("\n\n" + string, color)
87 """Grab keypresses for the menu screen"""
89 key = self.win.getkey()
91 if len(self.stack) == self.obj.getIdx():
93 elif len(self.stack) < self.obj.getIdx():
97 self.stack = lightcards.get_orig()[1]
98 self.menu_print("Stack reset!")
101 self.menu_print("Stack alphabetized!")
103 [x.unStar() for x in self.stack]
104 self.menu_print("All unstarred!")
106 [x.star() for x in self.stack]
107 self.menu_print("All starred!")
111 "self.stack reversed!")
114 self.menu_print("Stack shuffled!")
117 x[0], x[1] = x[1], x[0]
118 (self.headers[0], self.headers[1]) = (self.headers[1],
120 self.menu_print("Cards flipped!")
122 # Check if there are any starred cards before proceeding, and
123 # if not, don't allow to proceed and show an error message
131 self.stack = [x for x in self.stack if x.getStar()]
132 self.menu_print("Stars only!")
134 self.menu_print("ERR: None are starred!", err=True)
135 elif key in ["h", "KEY_LEFT"]:
136 self.obj.setIdx(len(self.stack) - 1)
142 def disp_menu(self, keygrab=True, quit=False):
144 Display a menu once the end of the deck has been reached, offering
145 multiple options on how to continue.
148 quit_text = "[q]: back"
150 quit_text = "[q]: quit"
152 self.win.addstr("LIGHTCARDS MENU", curses.color_pair(1) +
154 self.win.hline(1, 0, curses.ACS_HLINE, 15)
155 self.win.addstr(2, 0, "[y]: reset stack to original state\n" +
156 "[a]: alphabetize stack\n" +
157 "[z]: shuffle stack\n" +
158 "[f]: flip all cards in stack\n" +
159 "[t]: reverse stack order\n" +
160 "[u]: unstar all\n" +
162 "[s]: update stack to include starred only\n\n" +
169 def wrap_width(self):
170 """Calculate the width at which the body should wrap"""
171 (_, mcols) = self.win.getmaxyx()
172 wrap_width = mcols - 20
179 Display the contents of the card
180 Shows a header, a horizontal line, and the contents of the current
184 (_, mcols) = self.win.getmaxyx()
185 if self.obj.getIdx() == len(self.stack):
186 self.disp_menu(quit=True)
188 # If on the back of the card, show the content of the front side in
190 num_done = str(self.obj.getIdx() +
191 1).zfill(len(str(len(self.stack))))
192 if self.obj.getSide() == 0:
193 top = num_done + " | " + self.headers[self.obj.getSide()]
195 top = num_done + " | " + self.headers[self.obj.getSide()] + \
196 " | \"" + str(self.stack[self.obj.getIdx()][0]) + "\""
201 self.win.addstr(textwrap.shorten(top, width=header_width,
202 placeholder="…"), curses.A_BOLD)
204 # Add horizontal line
205 lin_width = header_width
206 if len(top) < header_width:
208 self.win.hline(1, 0, curses.ACS_HLINE, lin_width)
211 self.win.addstr(2, 0, textwrap.fill(
212 self.stack[self.obj.getIdx()][self.obj.getSide()],
213 width=self.wrap_width()))
218 """Display help screen"""
220 self.win.addstr("LIGHTCARDS HELP", curses.color_pair(1) +
222 self.win.hline(1, 0, curses.ACS_HLINE, 15)
223 self.win.addstr(2, 0, textwrap.fill(
224 "Welcome to lightcards. Here are some keybindings to get you " +
225 "started:", width=self.wrap_width()) +
226 "\n\nh, left previous card\n" +
227 "l, right next card\n" +
228 "j, k, up, down flip card\n" +
230 "0, ^, home go to the start of the deck\n" +
231 "$, end go to the end of the deck\n" +
232 "H, ? open this screen\n" +
233 "e open the input file in $EDITOR\n" +
234 "m open the control menu\n\n" +
236 "More information can be found in the man page, " +
237 "or by running `lightcards --help`.",
238 width=self.wrap_width()) +
239 "\n\nPress [q], [H], or [?] to go back.")
241 key = self.win.getkey()
242 if key in ["q", "H", "?"]:
247 Display a card and wait for the input.
248 Used as a general way of getting back into the card flow from a menu
253 key = self.win.getkey()
256 elif key in ["l", "KEY_RIGHT"]:
257 self.obj.forward(self.stack)
260 elif key in ["h", "KEY_LEFT"]:
264 elif key in ["j", "k", "KEY_UP", "KEY_DOWN"]:
267 elif key in ["i", "/"]:
268 self.stack[self.obj.getIdx()].toggleStar()
270 elif key in ["0", "^", "KEY_HOME"]:
274 elif key in ["$", "KEY_END"]:
275 self.obj.setIdx(len(self.stack) - 1)
278 elif key in ["H", "?"]:
284 (self.headers, self.stack) = lightcards.reparse()
287 def disp_sidebar(self):
288 """Display a sidebar with the starred terms"""
289 (mlines, mcols) = self.win.getmaxyx()
292 self.win.addstr(0, mcols - 16, "STARRED CARDS",
293 curses.color_pair(3) + curses.A_BOLD)
294 self.win.vline(0, mcols - 20, 0, mlines - 2)
295 self.win.hline(1, left, 0, mlines)
298 for card in self.stack:
300 self.win.addstr(2 + i, left, f"... ({self.ntotal() - i} more)")
304 if len(card[0]) > 18:
305 term = card[0][:18] + "…"
306 self.win.addstr(2 + i, left, term)
311 self.win.addstr(2, left, "None starred")