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):
129 Display a menu once the end of the deck has been reached, offering
130 multiple options on how to continue.
133 self.win.addstr("LIGHTCARDS MENU", curses.color_pair(1) +
135 self.win.hline(1, 0, curses.ACS_HLINE, 15)
136 self.win.addstr(2, 0, "[y]: reset stack to original state\n" +
137 "[z]: shuffle stack\n" +
138 "[f]: flip all cards in stack\n" +
139 "[t]: reverse stack order\n" +
140 "[u]: unstar all\n" +
142 "[s]: update stack to include starred only\n\n" +
149 def wrap_width(self):
150 (mlines, mcols) = self.win.getmaxyx()
158 Display the contents of the card
159 Shows a header, a horizontal line, and the contents of the current
163 (mlines, mcols) = self.win.getmaxyx()
164 if self.obj.getIdx() == len(self.stack):
167 # If on the back of the card, show the content of the front side in
169 num_done = str(self.obj.getIdx() +
170 1).zfill(len(str(len(self.stack))))
171 if self.obj.getSide() == 0:
172 top = num_done + " | " + self.headers[self.obj.getSide()]
174 top = num_done + " | " + self.headers[self.obj.getSide()] + \
175 " | \"" + str(self.stack[self.obj.getIdx()][0]) + "\""
180 self.win.addstr(textwrap.shorten(top, width=header_width,
181 placeholder="…"), curses.A_BOLD)
183 # Add horizontal line
184 lin_width = header_width
185 if len(top) < header_width:
187 self.win.hline(1, 0, curses.ACS_HLINE, lin_width)
190 self.win.addstr(2, 0, textwrap.fill(
191 self.stack[self.obj.getIdx()][self.obj.getSide()],
192 width=self.wrap_width()))
196 """Display help screen"""
198 self.win.addstr("LIGHTCARDS HELP", curses.color_pair(1) +
200 self.win.hline(1, 0, curses.ACS_HLINE, 15)
201 self.win.addstr(2, 0, textwrap.fill(
202 "Welcome to lightcards. Here are some keybindings to get you " +
203 "started:", width=self.wrap_width()) +
204 "\n\nh, left previous card\n" +
205 "l, right next card\n" +
206 "j, k, up, down flip card\n" +
208 "0, ^, home go to the start of the deck\n" +
209 "$, end go to the end of the deck\n" +
210 "H, ? open this screen\n" +
211 "e open the input file in $EDITOR\n" +
212 "m open the control menu\n\n" +
214 "More information can be found in the man page, " +
215 "or by running `lightcards --help`.",
216 width=self.wrap_width()) +
217 "\n\nPress [q], [H], or [?] to go back.")
219 key = self.win.getkey()
220 if key in ["q", "H", "?"]:
225 Display a card and wait for the input.
226 Used as a general way of getting back into the card flow from a menu
231 key = self.win.getkey()
234 elif key in ["l", "KEY_RIGHT"]:
235 self.obj.forward(self.stack)
238 elif key in ["h", "KEY_LEFT"]:
242 elif key in ["j", "k", "KEY_UP", "KEY_DOWN"]:
245 elif key in ["i", "/"]:
246 self.stack[self.obj.getIdx()].toggleStar()
248 elif key in ["0", "^", "KEY_HOME"]:
252 elif key in ["$", "KEY_END"]:
253 self.obj.setIdx(len(self.stack) - 1)
256 elif key in ["H", "?"]:
262 (self.headers, self.stack) = lightcards.reparse()