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, mcols) = 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):
65 (mlines, mcols) = self.win.getmaxyx()
68 color = curses.color_pair(2)
70 color = curses.color_pair(1)
71 self.disp_menu(keygrab=False)
72 self.win.addstr("\n\n" + string, color)
77 key = self.win.getkey()
79 if len(self.stack) == self.obj.getIdx():
81 elif len(self.stack) < self.obj.getIdx():
85 self.stack = lightcards.get_orig()[1]
86 self.menu_print("Stack reset!")
88 [x.unStar() for x in self.stack]
89 self.menu_print("All unstarred!")
91 [x.star() for x in self.stack]
92 self.menu_print("All starred!")
96 "self.stack reversed!")
99 self.menu_print("Stack shuffled!")
102 x[0], x[1] = x[1], x[0]
103 (self.headers[0], self.headers[1]) = (self.headers[1],
105 self.menu_print("Cards flipped!")
107 # Check if there are any starred cards before proceeding, and
108 # if not, don't allow to proceed and show an error message
116 self.stack = [x for x in self.stack if x.getStar()]
117 self.menu_print("Stars only!")
119 self.menu_print("ERR: None are starred!", err=True)
120 elif key in ["h", "KEY_LEFT"]:
121 self.obj.setIdx(len(self.stack) - 1)
127 def disp_menu(self, keygrab=True, quit=False):
129 Display a menu once the end of the deck has been reached, offering
130 multiple options on how to continue.
133 quit_text = "[q]: back"
135 quit_text = "[q]: quit"
137 self.win.addstr("LIGHTCARDS MENU", curses.color_pair(1) +
139 self.win.hline(1, 0, curses.ACS_HLINE, 15)
140 self.win.addstr(2, 0, "[y]: reset stack to original state\n" +
141 "[z]: shuffle stack\n" +
142 "[f]: flip all cards in stack\n" +
143 "[t]: reverse stack order\n" +
144 "[u]: unstar all\n" +
146 "[s]: update stack to include starred only\n\n" +
153 def wrap_width(self):
154 (mlines, mcols) = self.win.getmaxyx()
162 Display the contents of the card
163 Shows a header, a horizontal line, and the contents of the current
167 (mlines, mcols) = self.win.getmaxyx()
168 if self.obj.getIdx() == len(self.stack):
169 self.disp_menu(quit=True)
171 # If on the back of the card, show the content of the front side in
173 num_done = str(self.obj.getIdx() +
174 1).zfill(len(str(len(self.stack))))
175 if self.obj.getSide() == 0:
176 top = num_done + " | " + self.headers[self.obj.getSide()]
178 top = num_done + " | " + self.headers[self.obj.getSide()] + \
179 " | \"" + str(self.stack[self.obj.getIdx()][0]) + "\""
184 self.win.addstr(textwrap.shorten(top, width=header_width,
185 placeholder="…"), curses.A_BOLD)
187 # Add horizontal line
188 lin_width = header_width
189 if len(top) < header_width:
191 self.win.hline(1, 0, curses.ACS_HLINE, lin_width)
194 self.win.addstr(2, 0, textwrap.fill(
195 self.stack[self.obj.getIdx()][self.obj.getSide()],
196 width=self.wrap_width()))
200 """Display help screen"""
202 self.win.addstr("LIGHTCARDS HELP", curses.color_pair(1) +
204 self.win.hline(1, 0, curses.ACS_HLINE, 15)
205 self.win.addstr(2, 0, textwrap.fill(
206 "Welcome to lightcards. Here are some keybindings to get you " +
207 "started:", width=self.wrap_width()) +
208 "\n\nh, left previous card\n" +
209 "l, right next card\n" +
210 "j, k, up, down flip card\n" +
212 "0, ^, home go to the start of the deck\n" +
213 "$, end go to the end of the deck\n" +
214 "H, ? open this screen\n" +
215 "e open the input file in $EDITOR\n" +
216 "m open the control menu\n\n" +
218 "More information can be found in the man page, " +
219 "or by running `lightcards --help`.",
220 width=self.wrap_width()) +
221 "\n\nPress [q], [H], or [?] to go back.")
223 key = self.win.getkey()
224 if key in ["q", "H", "?"]:
229 Display a card and wait for the input.
230 Used as a general way of getting back into the card flow from a menu
235 key = self.win.getkey()
238 elif key in ["l", "KEY_RIGHT"]:
239 self.obj.forward(self.stack)
242 elif key in ["h", "KEY_LEFT"]:
246 elif key in ["j", "k", "KEY_UP", "KEY_DOWN"]:
249 elif key in ["i", "/"]:
250 self.stack[self.obj.getIdx()].toggleStar()
252 elif key in ["0", "^", "KEY_HOME"]:
256 elif key in ["$", "KEY_END"]:
257 self.obj.setIdx(len(self.stack) - 1)
260 elif key in ["H", "?"]:
266 (self.headers, self.stack) = lightcards.reparse()