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, "status", self.stack)
28 progress.dump(self.stack, "stack", self.stack)
29 progress.dump(self.headers, "headers", self.stack)
34 Display the statusbar at the bottom of the screen with progress, star
35 status, and card side.
37 (mlines, mcols) = self.win.getmaxyx()
39 # Calculate percent done
40 if len(self.stack) <= 1:
43 percent = str(round(self.obj.getIdx() /
44 len(self.stack) * 100)).zfill(2)
46 # Print yellow if starred
47 if self.stack[self.obj.getIdx()].getStar():
48 star_color = curses.color_pair(3)
50 star_color = curses.color_pair(1)
52 # Create bar component
54 bar_middle = self.stack[self.obj.getIdx()].printStar()
55 bar_end = "] [" + percent + "% (" + \
56 str(self.obj.getIdx() + 1).zfill(len(str(len(self.stack)))) + \
57 "/" + str(len(self.stack)) + ")] [" + \
58 self.headers[self.obj.getSide()] + " (" + \
59 str(self.obj.getSide() + 1) + ")]"
62 self.win.addstr(mlines - 1, 0, bar_start, curses.color_pair(1))
63 self.win.addstr(bar_middle, star_color)
64 self.win.insstr(bar_end, curses.color_pair(1))
66 def menu_print(self, string, err=False):
67 (mlines, mcols) = self.win.getmaxyx()
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):
131 Display a menu once the end of the deck has been reached, offering
132 multiple options on how to continue.
135 self.win.addstr("LIGHTCARDS MENU", curses.color_pair(1) +
137 self.win.hline(1, 0, curses.ACS_HLINE, 15)
138 self.win.addstr(2, 0, "[y]: reset stack to original state\n" +
139 "[z]: shuffle stack\n" +
140 "[f]: flip all cards in stack\n" +
141 "[t]: reverse stack order\n" +
142 "[u]: unstar all\n" +
144 "[s]: update stack to include starred only\n\n" +
151 def wrap_width(self):
152 (mlines, mcols) = self.win.getmaxyx()
160 Display the contents of the card
161 Shows a header, a horizontal line, and the contents of the current
165 (mlines, mcols) = self.win.getmaxyx()
166 if self.obj.getIdx() == len(self.stack):
169 # If on the back of the card, show the content of the front side in
171 num_done = str(self.obj.getIdx() +
172 1).zfill(len(str(len(self.stack))))
173 if self.obj.getSide() == 0:
174 top = num_done + " | " + self.headers[self.obj.getSide()]
176 top = num_done + " | " + self.headers[self.obj.getSide()] + \
177 " | \"" + str(self.stack[self.obj.getIdx()][0]) + "\""
182 self.win.addstr(textwrap.shorten(top, width=header_width,
183 placeholder="…"), curses.A_BOLD)
185 # Add horizontal line
186 lin_width = header_width
187 if len(top) < header_width:
189 self.win.hline(1, 0, curses.ACS_HLINE, lin_width)
192 self.win.addstr(2, 0, textwrap.fill(
193 self.stack[self.obj.getIdx()][self.obj.getSide()],
194 width=self.wrap_width()))
198 """Display help screen"""
200 self.win.addstr("LIGHTCARDS HELP", curses.color_pair(1) +
202 self.win.hline(1, 0, curses.ACS_HLINE, 15)
203 self.win.addstr(2, 0, textwrap.fill(
204 "Welcome to lightcards. Here are some keybindings to get you " +
205 "started:", width=self.wrap_width()) +
206 "\n\nh, left previous card\n" +
207 "l, right next card\n" +
208 "j, k, up, down flip card\n" +
210 "0, ^, home go to the start of the deck\n" +
211 "$, end go to the end of the deck\n" +
212 "H, ? open this screen\n" +
213 "e open the input file in $EDITOR\n" +
214 "m open the control menu\n\n" +
216 "More information can be found in the man page, " +
217 "or by running `lightcards --help`.",
218 width=self.wrap_width()) +
219 "\n\nPress [q], [H], or [?] to go back.")
221 key = self.win.getkey()
222 if key in ["q", "H", "?"]:
227 Display a card and wait for the input.
228 Used as a general way of getting back into the card flow from a menu
233 key = self.win.getkey()
236 elif key in ["l", "KEY_RIGHT"]:
237 self.obj.forward(self.stack)
240 elif key in ["h", "KEY_LEFT"]:
244 elif key in ["j", "k", "KEY_UP", "KEY_DOWN"]:
247 elif key in ["i", "/"]:
248 self.stack[self.obj.getIdx()].toggleStar()
250 elif key in ["0", "^", "KEY_HOME"]:
254 elif key in ["$", "KEY_END"]:
255 self.obj.setIdx(len(self.stack) - 1)
258 elif key in ["H", "?"]:
264 (self.headers, self.stack) = lightcards.reparse()