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):
20 curses.curs_set(0) # Hide cursor
21 curses.init_pair(1, curses.COLOR_CYAN, 0)
22 curses.init_pair(2, curses.COLOR_RED, 0)
23 curses.init_pair(3, curses.COLOR_YELLOW, 0)
27 if self.obj.getIdx() == len(self.stack):
30 progress.dump((self.obj, self.stack, self.headers), self.stack)
34 """Get toal number of starred cards"""
35 return(len([card for card in self.stack if card.getStar()]))
39 Display the statusbar at the bottom of the screen with progress, star
40 status, and card side.
42 (mlines, mcols) = self.win.getmaxyx()
44 # Calculate percent done
45 if len(self.stack) <= 1:
48 percent = str(round(self.obj.getIdx() /
49 len(self.stack) * 100)).zfill(2)
51 # Print yellow if starred
52 if self.stack[self.obj.getIdx()].getStar():
53 star_color = curses.color_pair(3)
55 star_color = curses.color_pair(1)
57 # Create bar component
59 bar_middle = self.stack[self.obj.getIdx()].printStar()
60 bar_end = f"] [{self.ntotal()}/{str(len(self.stack))} starred] " + \
62 str(self.obj.getIdx() + 1).zfill(len(str(len(self.stack)))) + \
63 f"/{str(len(self.stack))})] [" + \
64 f"{self.headers[self.obj.getSide()]} (" + \
65 f"{str(self.obj.getSide() + 1)})] "
68 self.win.hline(mlines - 2, 0, 0, mcols)
69 self.win.addstr(mlines - 1, 0, bar_start, curses.color_pair(1))
70 self.win.addstr(bar_middle, star_color)
71 self.win.insstr(bar_end, curses.color_pair(1))
73 def menu_print(self, string, err=False):
76 color = curses.color_pair(2)
78 color = curses.color_pair(1)
79 self.disp_menu(keygrab=False)
80 self.win.addstr("\n\n" + string, color)
85 key = self.win.getkey()
87 if len(self.stack) == self.obj.getIdx():
89 elif len(self.stack) < self.obj.getIdx():
93 self.stack = lightcards.get_orig()[1]
94 self.menu_print("Stack reset!")
96 [x.unStar() for x in self.stack]
97 self.menu_print("All unstarred!")
99 [x.star() for x in self.stack]
100 self.menu_print("All starred!")
104 "self.stack reversed!")
107 self.menu_print("Stack shuffled!")
110 x[0], x[1] = x[1], x[0]
111 (self.headers[0], self.headers[1]) = (self.headers[1],
113 self.menu_print("Cards flipped!")
115 # Check if there are any starred cards before proceeding, and
116 # if not, don't allow to proceed and show an error message
124 self.stack = [x for x in self.stack if x.getStar()]
125 self.menu_print("Stars only!")
127 self.menu_print("ERR: None are starred!", err=True)
128 elif key in ["h", "KEY_LEFT"]:
129 self.obj.setIdx(len(self.stack) - 1)
135 def disp_menu(self, keygrab=True, quit=False):
137 Display a menu once the end of the deck has been reached, offering
138 multiple options on how to continue.
141 quit_text = "[q]: back"
143 quit_text = "[q]: quit"
145 self.win.addstr("LIGHTCARDS MENU", curses.color_pair(1) +
147 self.win.hline(1, 0, curses.ACS_HLINE, 15)
148 self.win.addstr(2, 0, "[y]: reset stack to original state\n" +
149 "[z]: shuffle stack\n" +
150 "[f]: flip all cards in stack\n" +
151 "[t]: reverse stack order\n" +
152 "[u]: unstar all\n" +
154 "[s]: update stack to include starred only\n\n" +
161 def wrap_width(self):
162 (_, mcols) = self.win.getmaxyx()
163 wrap_width = mcols - 20
170 Display the contents of the card
171 Shows a header, a horizontal line, and the contents of the current
175 (_, mcols) = self.win.getmaxyx()
176 if self.obj.getIdx() == len(self.stack):
177 self.disp_menu(quit=True)
179 # If on the back of the card, show the content of the front side in
181 num_done = str(self.obj.getIdx() +
182 1).zfill(len(str(len(self.stack))))
183 if self.obj.getSide() == 0:
184 top = num_done + " | " + self.headers[self.obj.getSide()]
186 top = num_done + " | " + self.headers[self.obj.getSide()] + \
187 " | \"" + str(self.stack[self.obj.getIdx()][0]) + "\""
192 self.win.addstr(textwrap.shorten(top, width=header_width,
193 placeholder="…"), curses.A_BOLD)
195 # Add horizontal line
196 lin_width = header_width
197 if len(top) < header_width:
199 self.win.hline(1, 0, curses.ACS_HLINE, lin_width)
202 self.win.addstr(2, 0, textwrap.fill(
203 self.stack[self.obj.getIdx()][self.obj.getSide()],
204 width=self.wrap_width()))
209 """Display help screen"""
211 self.win.addstr("LIGHTCARDS HELP", curses.color_pair(1) +
213 self.win.hline(1, 0, curses.ACS_HLINE, 15)
214 self.win.addstr(2, 0, textwrap.fill(
215 "Welcome to lightcards. Here are some keybindings to get you " +
216 "started:", width=self.wrap_width()) +
217 "\n\nh, left previous card\n" +
218 "l, right next card\n" +
219 "j, k, up, down flip card\n" +
221 "0, ^, home go to the start of the deck\n" +
222 "$, end go to the end of the deck\n" +
223 "H, ? open this screen\n" +
224 "e open the input file in $EDITOR\n" +
225 "m open the control menu\n\n" +
227 "More information can be found in the man page, " +
228 "or by running `lightcards --help`.",
229 width=self.wrap_width()) +
230 "\n\nPress [q], [H], or [?] to go back.")
232 key = self.win.getkey()
233 if key in ["q", "H", "?"]:
238 Display a card and wait for the input.
239 Used as a general way of getting back into the card flow from a menu
244 key = self.win.getkey()
247 elif key in ["l", "KEY_RIGHT"]:
248 self.obj.forward(self.stack)
251 elif key in ["h", "KEY_LEFT"]:
255 elif key in ["j", "k", "KEY_UP", "KEY_DOWN"]:
258 elif key in ["i", "/"]:
259 self.stack[self.obj.getIdx()].toggleStar()
261 elif key in ["0", "^", "KEY_HOME"]:
265 elif key in ["$", "KEY_END"]:
266 self.obj.setIdx(len(self.stack) - 1)
269 elif key in ["H", "?"]:
275 (self.headers, self.stack) = lightcards.reparse()
278 def disp_sidebar(self):
279 (mlines, mcols) = self.win.getmaxyx()
282 self.win.addstr(0, mcols - 16, "STARRED CARDS",
283 curses.color_pair(3) + curses.A_BOLD)
284 self.win.vline(0, mcols - 20, 0, mlines - 2)
285 self.win.hline(1, left, 0, mlines)
288 for card in self.stack:
290 self.win.addstr(2 + i, left, f"... ({self.ntotal() - i} more)")
294 if len(card[0]) > 18:
295 term = card[0][:18] + "…"
296 self.win.addstr(2 + i, left, term)
301 self.win.addstr(2, left, "None starred")