1 # Display card output and retreive input
5 from random import shuffle
9 from . import lightcards
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)
28 Display the statusbar at the bottom of the screen with progress, star
29 status, and card side.
31 (mlines, mcols) = self.win.getmaxyx()
33 # Calculate percent done
34 if len(self.stack) <= 1:
37 percent = str(round(self.obj.getIdx() /
38 len(self.stack) * 100)).zfill(3)
40 # Print yellow if starred
41 if self.stack[self.obj.getIdx()].getStar():
42 star_color = curses.color_pair(3)
44 star_color = curses.color_pair(1)
46 # Create bar component
48 bar_middle = self.stack[self.obj.getIdx()].printStar()
49 bar_end= "] [" + percent + "% (" + \
50 str(self.obj.getIdx() + 1).zfill(len(str(len(self.stack)))) + \
51 "/" + str(len(self.stack)) + ")] [" + \
52 self.headers[self.obj.getSide()] + " (" + \
53 str(self.obj.getSide() + 1) + ")]"
56 self.win.addstr(mlines - 1, 0, bar_start, curses.color_pair(1))
57 self.win.addstr(bar_middle, star_color)
58 self.win.insstr(bar_end, curses.color_pair(1))
60 def menu_print(self, string, err=False):
63 self.win.addstr(string + "\n\n", curses.color_pair(2))
65 self.win.addstr(string + "\n\n", curses.color_pair(1))
70 Display a menu once the end of the deck has been reached, offering
71 multiple options on how to continue.
74 self.win.addstr("Choose one of the following options:\n\n" +
75 "[y]: reset stack to original state\n" +
76 "[z]: shuffle stack\n" +
77 "[f]: flip all cards in stack\n" +
78 "[t]: reverse stack order\n" +
80 "[s]: update stack to include starred only\n\n" +
85 key = self.win.getkey()
89 self.stack = lightcards.get_orig()[1]
90 self.menu_print("Stack reset!")
92 [x.unStar() for x in self.stack]
93 self.menu_print("All unstarred!")
97 "self.stack reversed!")
100 self.menu_print("self.stack shuffled!")
103 x[0], x[1] = x[1], x[0]
104 (self.headers[0], self.headers[1]) = (self.headers[1],
106 self.menu_print("Cards flipped!")
108 # Check if there are any starred cards before proceeding, and
109 # if not, don't allow to proceed and show an error message
117 self.stack = [x for x in self.stack if x.getStar()]
118 self.menu_print("Stars only!")
120 self.menu_print("ERR: None are starred!", err=True)
121 elif key in ["h", "KEY_LEFT"]:
122 self.obj.setIdx(len(self.stack) - 1)
127 def wrap_width(self):
128 (mlines, mcols) = self.win.getmaxyx()
136 Display the contents of the card
137 Shows a header, a horizontal line, and the contents of the current
141 (mlines, mcols) = self.win.getmaxyx()
142 if self.obj.getIdx() == len(self.stack):
145 # If on the back of the card, show the content of the front side in
147 num_done = str(self.obj.getIdx() +
148 1).zfill(len(str(len(self.stack))))
149 if self.obj.getSide() == 0:
150 top = num_done + " | " + self.headers[self.obj.getSide()]
152 top = num_done + " | " + self.headers[self.obj.getSide()] + \
153 " | \"" + str(self.stack[self.obj.getIdx()][0]) + "\""
158 self.win.addstr(textwrap.shorten(top, width=header_width,
159 placeholder="…"), curses.A_BOLD)
161 # Add horizontal line
162 lin_width = header_width
163 if len(top) < header_width:
165 self.win.hline(1, 0, curses.ACS_HLINE, lin_width)
168 self.win.addstr(2, 0, textwrap.fill(
169 self.stack[self.obj.getIdx()][self.obj.getSide()],
170 width=self.wrap_width()))
174 """Display help screen"""
176 self.win.addstr("LIGHTCARDS HELP", curses.color_pair(1) +
178 self.win.hline(1, 0, curses.ACS_HLINE, 15)
179 self.win.addstr(2, 0, textwrap.fill(
180 "Welcome to lightcards. Here are some keybindings to get you " +
181 "started", width=self.wrap_width()) +
182 "\n\nh, left previous card\n" +
183 "l, right next card\n" +
184 "j, k, up, down flip card\n" +
186 "0, ^, home go to the start of the deck\n" +
187 "$, end go to the end of the deck\n" +
188 "H, ? open this screen\n" +
189 "e open the input file in $EDITOR\n\n" +
191 "More information can be found in the man page, " +
192 "or by running `lightcards --help`.",
193 width=self.wrap_width()) +
194 "\n\nPress [q], [H], or [?] to go back.")
196 key = self.win.getkey()
197 if key in ["q", "H", "?"]:
202 Display a card and wait for the input.
203 Used as a general way of getting back into the card flow from a menu
208 key = self.win.getkey()
211 elif key in ["l", "KEY_RIGHT"]:
212 self.obj.forward(self.stack)
215 elif key in ["h", "KEY_LEFT"]:
219 elif key in ["j", "k", "KEY_UP", "KEY_DOWN"]:
222 elif key in ["i", "/"]:
223 self.stack[self.obj.getIdx()].toggleStar()
225 elif key in ["0", "^", "KEY_HOME"]:
229 elif key in ["$", "KEY_END"]:
230 self.obj.setIdx(len(self.stack) - 1)
233 elif key in ["H", "?"]:
236 (self.headers, self.stack) = lightcards.reparse()