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 progress.dump((self.obj, self.stack, self.headers), self.stack)
32 Display the statusbar at the bottom of the screen with progress, star
33 status, and card side.
35 (mlines, _) = self.win.getmaxyx()
37 # Calculate percent done
38 if len(self.stack) <= 1:
41 percent = str(round(self.obj.getIdx() /
42 len(self.stack) * 100)).zfill(2)
44 # Print yellow if starred
45 if self.stack[self.obj.getIdx()].getStar():
46 star_color = curses.color_pair(3)
48 star_color = curses.color_pair(1)
50 # Create bar component
52 bar_middle = self.stack[self.obj.getIdx()].printStar()
53 bar_end = "] [" + percent + "% (" + \
54 str(self.obj.getIdx() + 1).zfill(len(str(len(self.stack)))) + \
55 "/" + str(len(self.stack)) + ")] [" + \
56 self.headers[self.obj.getSide()] + " (" + \
57 str(self.obj.getSide() + 1) + ")]"
60 self.win.addstr(mlines - 1, 0, bar_start, curses.color_pair(1))
61 self.win.addstr(bar_middle, star_color)
62 self.win.insstr(bar_end, curses.color_pair(1))
64 def menu_print(self, string, err=False):
67 color = curses.color_pair(2)
69 color = curses.color_pair(1)
70 self.disp_menu(keygrab=False)
71 self.win.addstr("\n\n" + string, color)
76 key = self.win.getkey()
78 if len(self.stack) == self.obj.getIdx():
80 elif len(self.stack) < self.obj.getIdx():
84 self.stack = lightcards.get_orig()[1]
85 self.menu_print("Stack reset!")
87 [x.unStar() for x in self.stack]
88 self.menu_print("All unstarred!")
90 [x.star() for x in self.stack]
91 self.menu_print("All starred!")
95 "self.stack reversed!")
98 self.menu_print("Stack shuffled!")
101 x[0], x[1] = x[1], x[0]
102 (self.headers[0], self.headers[1]) = (self.headers[1],
104 self.menu_print("Cards flipped!")
106 # Check if there are any starred cards before proceeding, and
107 # if not, don't allow to proceed and show an error message
115 self.stack = [x for x in self.stack if x.getStar()]
116 self.menu_print("Stars only!")
118 self.menu_print("ERR: None are starred!", err=True)
119 elif key in ["h", "KEY_LEFT"]:
120 self.obj.setIdx(len(self.stack) - 1)
126 def disp_menu(self, keygrab=True, quit=False):
128 Display a menu once the end of the deck has been reached, offering
129 multiple options on how to continue.
132 quit_text = "[q]: back"
134 quit_text = "[q]: quit"
136 self.win.addstr("LIGHTCARDS MENU", curses.color_pair(1) +
138 self.win.hline(1, 0, curses.ACS_HLINE, 15)
139 self.win.addstr(2, 0, "[y]: reset stack to original state\n" +
140 "[z]: shuffle stack\n" +
141 "[f]: flip all cards in stack\n" +
142 "[t]: reverse stack order\n" +
143 "[u]: unstar all\n" +
145 "[s]: update stack to include starred only\n\n" +
152 def wrap_width(self):
153 (_, mcols) = self.win.getmaxyx()
161 Display the contents of the card
162 Shows a header, a horizontal line, and the contents of the current
166 (_, mcols) = self.win.getmaxyx()
167 if self.obj.getIdx() == len(self.stack):
168 self.disp_menu(quit=True)
170 # If on the back of the card, show the content of the front side in
172 num_done = str(self.obj.getIdx() +
173 1).zfill(len(str(len(self.stack))))
174 if self.obj.getSide() == 0:
175 top = num_done + " | " + self.headers[self.obj.getSide()]
177 top = num_done + " | " + self.headers[self.obj.getSide()] + \
178 " | \"" + str(self.stack[self.obj.getIdx()][0]) + "\""
183 self.win.addstr(textwrap.shorten(top, width=header_width,
184 placeholder="…"), curses.A_BOLD)
186 # Add horizontal line
187 lin_width = header_width
188 if len(top) < header_width:
190 self.win.hline(1, 0, curses.ACS_HLINE, lin_width)
193 self.win.addstr(2, 0, textwrap.fill(
194 self.stack[self.obj.getIdx()][self.obj.getSide()],
195 width=self.wrap_width()))
199 """Display help screen"""
201 self.win.addstr("LIGHTCARDS HELP", curses.color_pair(1) +
203 self.win.hline(1, 0, curses.ACS_HLINE, 15)
204 self.win.addstr(2, 0, textwrap.fill(
205 "Welcome to lightcards. Here are some keybindings to get you " +
206 "started:", width=self.wrap_width()) +
207 "\n\nh, left previous card\n" +
208 "l, right next card\n" +
209 "j, k, up, down flip card\n" +
211 "0, ^, home go to the start of the deck\n" +
212 "$, end go to the end of the deck\n" +
213 "H, ? open this screen\n" +
214 "e open the input file in $EDITOR\n" +
215 "m open the control menu\n\n" +
217 "More information can be found in the man page, " +
218 "or by running `lightcards --help`.",
219 width=self.wrap_width()) +
220 "\n\nPress [q], [H], or [?] to go back.")
222 key = self.win.getkey()
223 if key in ["q", "H", "?"]:
228 Display a card and wait for the input.
229 Used as a general way of getting back into the card flow from a menu
234 key = self.win.getkey()
237 elif key in ["l", "KEY_RIGHT"]:
238 self.obj.forward(self.stack)
241 elif key in ["h", "KEY_LEFT"]:
245 elif key in ["j", "k", "KEY_UP", "KEY_DOWN"]:
248 elif key in ["i", "/"]:
249 self.stack[self.obj.getIdx()].toggleStar()
251 elif key in ["0", "^", "KEY_HOME"]:
255 elif key in ["$", "KEY_END"]:
256 self.obj.setIdx(len(self.stack) - 1)
259 elif key in ["H", "?"]:
265 (self.headers, self.stack) = lightcards.reparse()