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.stack, lightcards.get_orig())
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!")
97 self.menu_print("Stack alphabetized!")
99 [x.unStar() for x in self.stack]
100 self.menu_print("All unstarred!")
102 [x.star() for x in self.stack]
103 self.menu_print("All starred!")
107 "self.stack reversed!")
110 self.menu_print("Stack shuffled!")
113 x[0], x[1] = x[1], x[0]
114 (self.headers[0], self.headers[1]) = (self.headers[1],
116 self.menu_print("Cards flipped!")
118 # Check if there are any starred cards before proceeding, and
119 # if not, don't allow to proceed and show an error message
127 self.stack = [x for x in self.stack if x.getStar()]
128 self.menu_print("Stars only!")
130 self.menu_print("ERR: None are starred!", err=True)
131 elif key in ["h", "KEY_LEFT"]:
132 self.obj.setIdx(len(self.stack) - 1)
138 def disp_menu(self, keygrab=True, quit=False):
140 Display a menu once the end of the deck has been reached, offering
141 multiple options on how to continue.
144 quit_text = "[q]: back"
146 quit_text = "[q]: quit"
148 self.win.addstr("LIGHTCARDS MENU", curses.color_pair(1) +
150 self.win.hline(1, 0, curses.ACS_HLINE, 15)
151 self.win.addstr(2, 0, "[y]: reset stack to original state\n" +
152 "[a]: alphabetize stack\n" +
153 "[z]: shuffle stack\n" +
154 "[f]: flip all cards in stack\n" +
155 "[t]: reverse stack order\n" +
156 "[u]: unstar all\n" +
158 "[s]: update stack to include starred only\n\n" +
165 def wrap_width(self):
166 (_, mcols) = self.win.getmaxyx()
167 wrap_width = mcols - 20
174 Display the contents of the card
175 Shows a header, a horizontal line, and the contents of the current
179 (_, mcols) = self.win.getmaxyx()
180 if self.obj.getIdx() == len(self.stack):
181 self.disp_menu(quit=True)
183 # If on the back of the card, show the content of the front side in
185 num_done = str(self.obj.getIdx() +
186 1).zfill(len(str(len(self.stack))))
187 if self.obj.getSide() == 0:
188 top = num_done + " | " + self.headers[self.obj.getSide()]
190 top = num_done + " | " + self.headers[self.obj.getSide()] + \
191 " | \"" + str(self.stack[self.obj.getIdx()][0]) + "\""
196 self.win.addstr(textwrap.shorten(top, width=header_width,
197 placeholder="…"), curses.A_BOLD)
199 # Add horizontal line
200 lin_width = header_width
201 if len(top) < header_width:
203 self.win.hline(1, 0, curses.ACS_HLINE, lin_width)
206 self.win.addstr(2, 0, textwrap.fill(
207 self.stack[self.obj.getIdx()][self.obj.getSide()],
208 width=self.wrap_width()))
213 """Display help screen"""
215 self.win.addstr("LIGHTCARDS HELP", curses.color_pair(1) +
217 self.win.hline(1, 0, curses.ACS_HLINE, 15)
218 self.win.addstr(2, 0, textwrap.fill(
219 "Welcome to lightcards. Here are some keybindings to get you " +
220 "started:", width=self.wrap_width()) +
221 "\n\nh, left previous card\n" +
222 "l, right next card\n" +
223 "j, k, up, down flip card\n" +
225 "0, ^, home go to the start of the deck\n" +
226 "$, end go to the end of the deck\n" +
227 "H, ? open this screen\n" +
228 "e open the input file in $EDITOR\n" +
229 "m open the control menu\n\n" +
231 "More information can be found in the man page, " +
232 "or by running `lightcards --help`.",
233 width=self.wrap_width()) +
234 "\n\nPress [q], [H], or [?] to go back.")
236 key = self.win.getkey()
237 if key in ["q", "H", "?"]:
242 Display a card and wait for the input.
243 Used as a general way of getting back into the card flow from a menu
248 key = self.win.getkey()
251 elif key in ["l", "KEY_RIGHT"]:
252 self.obj.forward(self.stack)
255 elif key in ["h", "KEY_LEFT"]:
259 elif key in ["j", "k", "KEY_UP", "KEY_DOWN"]:
262 elif key in ["i", "/"]:
263 self.stack[self.obj.getIdx()].toggleStar()
265 elif key in ["0", "^", "KEY_HOME"]:
269 elif key in ["$", "KEY_END"]:
270 self.obj.setIdx(len(self.stack) - 1)
273 elif key in ["H", "?"]:
279 (self.headers, self.stack) = lightcards.reparse()
282 def disp_sidebar(self):
283 (mlines, mcols) = self.win.getmaxyx()
286 self.win.addstr(0, mcols - 16, "STARRED CARDS",
287 curses.color_pair(3) + curses.A_BOLD)
288 self.win.vline(0, mcols - 20, 0, mlines - 2)
289 self.win.hline(1, left, 0, mlines)
292 for card in self.stack:
294 self.win.addstr(2 + i, left, f"... ({self.ntotal() - i} more)")
298 if len(card[0]) > 18:
299 term = card[0][:18] + "…"
300 self.win.addstr(2 + i, left, term)
305 self.win.addstr(2, left, "None starred")