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)
35 Display the statusbar at the bottom of the screen with progress, star
36 status, and card side.
38 (mlines, mcols) = self.win.getmaxyx()
40 # Calculate percent done
41 if len(self.stack) <= 1:
44 percent = str(round(self.obj.getIdx() /
45 len(self.stack) * 100)).zfill(2)
47 # Print yellow if starred
48 if self.stack[self.obj.getIdx()].getStar():
49 star_color = curses.color_pair(3)
51 star_color = curses.color_pair(1)
53 # Create bar component
55 bar_middle = self.stack[self.obj.getIdx()].printStar()
56 bar_end = "] [" + percent + "% (" + \
57 str(self.obj.getIdx() + 1).zfill(len(str(len(self.stack)))) + \
58 "/" + str(len(self.stack)) + ")] [" + \
59 self.headers[self.obj.getSide()] + " (" + \
60 str(self.obj.getSide() + 1) + ")]"
63 self.win.hline(mlines - 2, 0, 0, mcols)
64 self.win.addstr(mlines - 1, 0, bar_start, curses.color_pair(1))
65 self.win.addstr(bar_middle, star_color)
66 self.win.insstr(bar_end, curses.color_pair(1))
68 def menu_print(self, string, err=False):
71 color = curses.color_pair(2)
73 color = curses.color_pair(1)
74 self.disp_menu(keygrab=False)
75 self.win.addstr("\n\n" + string, color)
80 key = self.win.getkey()
82 if len(self.stack) == self.obj.getIdx():
84 elif len(self.stack) < self.obj.getIdx():
88 self.stack = lightcards.get_orig()[1]
89 self.menu_print("Stack reset!")
91 [x.unStar() for x in self.stack]
92 self.menu_print("All unstarred!")
94 [x.star() for x in self.stack]
95 self.menu_print("All starred!")
99 "self.stack reversed!")
102 self.menu_print("Stack shuffled!")
105 x[0], x[1] = x[1], x[0]
106 (self.headers[0], self.headers[1]) = (self.headers[1],
108 self.menu_print("Cards flipped!")
110 # Check if there are any starred cards before proceeding, and
111 # if not, don't allow to proceed and show an error message
119 self.stack = [x for x in self.stack if x.getStar()]
120 self.menu_print("Stars only!")
122 self.menu_print("ERR: None are starred!", err=True)
123 elif key in ["h", "KEY_LEFT"]:
124 self.obj.setIdx(len(self.stack) - 1)
130 def disp_menu(self, keygrab=True, quit=False):
132 Display a menu once the end of the deck has been reached, offering
133 multiple options on how to continue.
136 quit_text = "[q]: back"
138 quit_text = "[q]: quit"
140 self.win.addstr("LIGHTCARDS MENU", curses.color_pair(1) +
142 self.win.hline(1, 0, curses.ACS_HLINE, 15)
143 self.win.addstr(2, 0, "[y]: reset stack to original state\n" +
144 "[z]: shuffle stack\n" +
145 "[f]: flip all cards in stack\n" +
146 "[t]: reverse stack order\n" +
147 "[u]: unstar all\n" +
149 "[s]: update stack to include starred only\n\n" +
156 def wrap_width(self):
157 (_, mcols) = self.win.getmaxyx()
158 wrap_width = mcols - 20
165 Display the contents of the card
166 Shows a header, a horizontal line, and the contents of the current
170 (_, mcols) = self.win.getmaxyx()
171 if self.obj.getIdx() == len(self.stack):
172 self.disp_menu(quit=True)
174 # If on the back of the card, show the content of the front side in
176 num_done = str(self.obj.getIdx() +
177 1).zfill(len(str(len(self.stack))))
178 if self.obj.getSide() == 0:
179 top = num_done + " | " + self.headers[self.obj.getSide()]
181 top = num_done + " | " + self.headers[self.obj.getSide()] + \
182 " | \"" + str(self.stack[self.obj.getIdx()][0]) + "\""
187 self.win.addstr(textwrap.shorten(top, width=header_width,
188 placeholder="…"), curses.A_BOLD)
190 # Add horizontal line
191 lin_width = header_width
192 if len(top) < header_width:
194 self.win.hline(1, 0, curses.ACS_HLINE, lin_width)
197 self.win.addstr(2, 0, textwrap.fill(
198 self.stack[self.obj.getIdx()][self.obj.getSide()],
199 width=self.wrap_width()))
204 """Display help screen"""
206 self.win.addstr("LIGHTCARDS HELP", curses.color_pair(1) +
208 self.win.hline(1, 0, curses.ACS_HLINE, 15)
209 self.win.addstr(2, 0, textwrap.fill(
210 "Welcome to lightcards. Here are some keybindings to get you " +
211 "started:", width=self.wrap_width()) +
212 "\n\nh, left previous card\n" +
213 "l, right next card\n" +
214 "j, k, up, down flip card\n" +
216 "0, ^, home go to the start of the deck\n" +
217 "$, end go to the end of the deck\n" +
218 "H, ? open this screen\n" +
219 "e open the input file in $EDITOR\n" +
220 "m open the control menu\n\n" +
222 "More information can be found in the man page, " +
223 "or by running `lightcards --help`.",
224 width=self.wrap_width()) +
225 "\n\nPress [q], [H], or [?] to go back.")
227 key = self.win.getkey()
228 if key in ["q", "H", "?"]:
233 Display a card and wait for the input.
234 Used as a general way of getting back into the card flow from a menu
239 key = self.win.getkey()
242 elif key in ["l", "KEY_RIGHT"]:
243 self.obj.forward(self.stack)
246 elif key in ["h", "KEY_LEFT"]:
250 elif key in ["j", "k", "KEY_UP", "KEY_DOWN"]:
253 elif key in ["i", "/"]:
254 self.stack[self.obj.getIdx()].toggleStar()
256 elif key in ["0", "^", "KEY_HOME"]:
260 elif key in ["$", "KEY_END"]:
261 self.obj.setIdx(len(self.stack) - 1)
264 elif key in ["H", "?"]:
270 (self.headers, self.stack) = lightcards.reparse()
273 def disp_sidebar(self):
274 (mlines, mcols) = self.win.getmaxyx()
275 self.win.addstr(0, mcols - 16, "STARRED CARDS",
276 curses.color_pair(3) + curses.A_BOLD)
277 self.win.vline(0, mcols - 20, 0, mlines)
278 self.win.hline(1, mcols - 19, 0, mlines)
280 total = [card for card in self.stack if card.getStar()]
284 for card in self.stack:
286 self.win.addstr(2 + i, mcols - 19, f"... ({ntotal - i} more)")
290 if len(card[0]) > 18:
291 term = card[0][:18] + "…"
292 self.win.addstr(2 + i, mcols - 19, term)
297 self.win.addstr(2, mcols - 19, "None starred")
299 self.win.addstr(mlines - 1, mcols - 19, f"{ntotal}/{str(len(self.stack))}")