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 self.win.addstr(mlines - 1, 0, "[", curses.color_pair(1))
42 if self.stack[self.obj.getIdx()].getStar():
43 self.win.addstr(self.stack[self.obj.getIdx()].printStar(),
46 self.win.addstr(self.stack[self.obj.getIdx()].printStar(),
49 # Put all the info together
50 self.win.addstr("] [" +
53 str(self.obj.getIdx() +
54 1).zfill(len(str(len(self.stack)))) +
56 str(len(self.stack)) +
59 self.headers[self.obj.getSide()] +
61 str(self.obj.getSide() + 1) +
62 ")]", curses.color_pair(1))
64 def menu_print(self, string, err=False):
67 self.win.addstr(string + "\n\n", curses.color_pair(2))
69 self.win.addstr(string + "\n\n", curses.color_pair(1))
74 Display a menu once the end of the deck has been reached, offering
75 multiple options on how to continue.
78 self.win.addstr("Choose one of the following options:\n\n" +
79 "[y]: reset stack to original state\n" +
80 "[z]: shuffle stack\n" +
81 "[f]: flip all cards in stack\n" +
82 "[t]: reverse stack order\n" +
84 "[s]: update stack to include starred only\n\n" +
89 key = self.win.getkey()
93 self.stack = lightcards.get_orig()[1]
94 self.menu_print("Stack reset!")
96 [x.unStar() for x in self.stack]
97 self.menu_print("All unstarred!")
101 "self.stack reversed!")
104 self.menu_print("self.stack shuffled!")
107 x[0], x[1] = x[1], x[0]
108 (self.headers[0], self.headers[1]) = (self.headers[1],
110 self.menu_print("Cards flipped!")
112 # Check if there are any starred cards before proceeding, and
113 # if not, don't allow to proceed and show an error message
121 self.stack = [x for x in self.stack if x.getStar()]
122 self.menu_print("Stars only!")
124 self.menu_print("ERR: None are starred!", err=True)
128 def wrap_width(self):
129 (mlines, mcols) = self.win.getmaxyx()
137 Display the contents of the card
138 Shows a header, a horizontal line, and the contents of the current
142 (mlines, mcols) = self.win.getmaxyx()
143 if self.obj.getIdx() == len(self.stack):
146 # If on the back of the card, show the content of the front side in
148 num_done = str(self.obj.getIdx() +
149 1).zfill(len(str(len(self.stack))))
150 if self.obj.getSide() == 0:
151 top = num_done + " | " + self.headers[self.obj.getSide()]
153 top = num_done + " | " + self.headers[self.obj.getSide()] + \
154 " | \"" + str(self.stack[self.obj.getIdx()][0]) + "\""
159 self.win.addstr(textwrap.shorten(top, width=header_width,
160 placeholder="…"), curses.A_BOLD)
162 # Add horizontal line
163 lin_width = header_width
164 if len(top) < header_width:
166 self.win.hline(1, 0, curses.ACS_HLINE, lin_width)
169 self.win.addstr(2, 0, textwrap.fill(
170 self.stack[self.obj.getIdx()][self.obj.getSide()],
171 width=self.wrap_width()))
175 """Display help screen"""
177 self.win.addstr("LIGHTCARDS HELP", curses.color_pair(1) +
179 self.win.hline(1, 0, curses.ACS_HLINE, 15)
180 self.win.addstr(2, 0, textwrap.fill(
181 "Welcome to lightcards. Here are some keybindings to get you " +
182 "started", width=self.wrap_width()) +
183 "\n\nh, left previous card\n" +
184 "l, right next card\n" +
185 "j, k, up, down flip card\n" +
187 "0, ^, home go to the start of the deck\n" +
188 "$, end go to the end of the deck\n" +
189 "H, ? open this screen\n" +
190 "e open the input file in $EDITOR\n\n" +
192 "More information can be found in the man page, " +
193 "or by running `lightcards --help`.",
194 width=self.wrap_width()) +
195 "\n\nPress [q], [H], or [?] to go back.")
197 key = self.win.getkey()
198 if key in ["q", "H", "?"]:
203 Display a card and wait for the input.
204 Used as a general way of getting back into the card flow from a menu
209 key = self.win.getkey()
212 elif key in ["l", "KEY_LEFT"]:
213 self.obj.forward(self.stack)
216 elif key in ["h", "KEY_RIGHT"]:
220 elif key in ["j", "k", "KEY_UP", "KEY_DOWN"]:
223 elif key in ["i", "/"]:
224 self.stack[self.obj.getIdx()].toggleStar()
226 elif key in ["0", "^", "KEY_HOME"]:
230 elif key in ["$", "KEY_END"]:
231 self.obj.setIdx(len(self.stack) - 1)
234 elif key in ["H", "?"]:
237 (self.headers, self.stack) = lightcards.reparse()