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, _) = 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.addstr(mlines - 1, 0, bar_start, curses.color_pair(1))
64 self.win.addstr(bar_middle, star_color)
65 self.win.insstr(bar_end, curses.color_pair(1))
67 def menu_print(self, string, err=False):
70 color = curses.color_pair(2)
72 color = curses.color_pair(1)
73 self.disp_menu(keygrab=False)
74 self.win.addstr("\n\n" + string, color)
79 key = self.win.getkey()
81 if len(self.stack) == self.obj.getIdx():
83 elif len(self.stack) < self.obj.getIdx():
87 self.stack = lightcards.get_orig()[1]
88 self.menu_print("Stack reset!")
90 [x.unStar() for x in self.stack]
91 self.menu_print("All unstarred!")
93 [x.star() for x in self.stack]
94 self.menu_print("All starred!")
98 "self.stack reversed!")
101 self.menu_print("Stack shuffled!")
104 x[0], x[1] = x[1], x[0]
105 (self.headers[0], self.headers[1]) = (self.headers[1],
107 self.menu_print("Cards flipped!")
109 # Check if there are any starred cards before proceeding, and
110 # if not, don't allow to proceed and show an error message
118 self.stack = [x for x in self.stack if x.getStar()]
119 self.menu_print("Stars only!")
121 self.menu_print("ERR: None are starred!", err=True)
122 elif key in ["h", "KEY_LEFT"]:
123 self.obj.setIdx(len(self.stack) - 1)
129 def disp_menu(self, keygrab=True, quit=False):
131 Display a menu once the end of the deck has been reached, offering
132 multiple options on how to continue.
135 quit_text = "[q]: back"
137 quit_text = "[q]: quit"
139 self.win.addstr("LIGHTCARDS MENU", curses.color_pair(1) +
141 self.win.hline(1, 0, curses.ACS_HLINE, 15)
142 self.win.addstr(2, 0, "[y]: reset stack to original state\n" +
143 "[z]: shuffle stack\n" +
144 "[f]: flip all cards in stack\n" +
145 "[t]: reverse stack order\n" +
146 "[u]: unstar all\n" +
148 "[s]: update stack to include starred only\n\n" +
155 def wrap_width(self):
156 (_, mcols) = self.win.getmaxyx()
164 Display the contents of the card
165 Shows a header, a horizontal line, and the contents of the current
169 (_, mcols) = self.win.getmaxyx()
170 if self.obj.getIdx() == len(self.stack):
171 self.disp_menu(quit=True)
173 # If on the back of the card, show the content of the front side in
175 num_done = str(self.obj.getIdx() +
176 1).zfill(len(str(len(self.stack))))
177 if self.obj.getSide() == 0:
178 top = num_done + " | " + self.headers[self.obj.getSide()]
180 top = num_done + " | " + self.headers[self.obj.getSide()] + \
181 " | \"" + str(self.stack[self.obj.getIdx()][0]) + "\""
186 self.win.addstr(textwrap.shorten(top, width=header_width,
187 placeholder="…"), curses.A_BOLD)
189 # Add horizontal line
190 lin_width = header_width
191 if len(top) < header_width:
193 self.win.hline(1, 0, curses.ACS_HLINE, lin_width)
196 self.win.addstr(2, 0, textwrap.fill(
197 self.stack[self.obj.getIdx()][self.obj.getSide()],
198 width=self.wrap_width()))
202 """Display help screen"""
204 self.win.addstr("LIGHTCARDS HELP", curses.color_pair(1) +
206 self.win.hline(1, 0, curses.ACS_HLINE, 15)
207 self.win.addstr(2, 0, textwrap.fill(
208 "Welcome to lightcards. Here are some keybindings to get you " +
209 "started:", width=self.wrap_width()) +
210 "\n\nh, left previous card\n" +
211 "l, right next card\n" +
212 "j, k, up, down flip card\n" +
214 "0, ^, home go to the start of the deck\n" +
215 "$, end go to the end of the deck\n" +
216 "H, ? open this screen\n" +
217 "e open the input file in $EDITOR\n" +
218 "m open the control menu\n\n" +
220 "More information can be found in the man page, " +
221 "or by running `lightcards --help`.",
222 width=self.wrap_width()) +
223 "\n\nPress [q], [H], or [?] to go back.")
225 key = self.win.getkey()
226 if key in ["q", "H", "?"]:
231 Display a card and wait for the input.
232 Used as a general way of getting back into the card flow from a menu
237 key = self.win.getkey()
240 elif key in ["l", "KEY_RIGHT"]:
241 self.obj.forward(self.stack)
244 elif key in ["h", "KEY_LEFT"]:
248 elif key in ["j", "k", "KEY_UP", "KEY_DOWN"]:
251 elif key in ["i", "/"]:
252 self.stack[self.obj.getIdx()].toggleStar()
254 elif key in ["0", "^", "KEY_HOME"]:
258 elif key in ["$", "KEY_END"]:
259 self.obj.setIdx(len(self.stack) - 1)
262 elif key in ["H", "?"]:
268 (self.headers, self.stack) = lightcards.reparse()