1 # Display card output and retreive input
5 from random import shuffle
9 from . import lightcards
12 def disp_bar(stdscr, stack, headers, obj):
14 Display the statusbar at the bottom of the screen with progress, star
15 status, and card side.
17 (mlines, mcols) = stdscr.getmaxyx()
19 # Calculate percent done
23 percent = str(round(obj.getIdx() / (len(stack) - 1) * 100)).zfill(3)
25 # Print yellow if starred
26 stdscr.addstr(mlines - 1, 0, "[", curses.color_pair(1))
27 if stack[obj.getIdx()].getStar():
28 stdscr.addstr(stack[obj.getIdx()].printStar(), curses.color_pair(3))
30 stdscr.addstr(stack[obj.getIdx()].printStar(), curses.color_pair(1))
32 # Put all the info together
36 str(obj.getIdx() + 1).zfill(len(str(len(stack)))) +
41 headers[obj.getSide()] +
43 str(obj.getSide() + 1) +
44 ")]", curses.color_pair(1))
47 def disp_menu(stdscr, stack, headers, idx):
49 Display a menu once the end of the deck has been reached, offering
50 multiple options on how to continue.
52 def menu_print(stdscr, stack, headers, idx, string, err=False):
55 stdscr.addstr(string + "\n\n", curses.color_pair(2))
57 stdscr.addstr(string + "\n\n", curses.color_pair(1))
58 disp_menu(stdscr, stack, headers, idx)
60 stdscr.addstr("Choose one of the following options:\n\n" +
61 "[y]: reset stack to original state\n" +
62 "[z]: shuffle stack\n" +
63 "[f]: flip all cards in stack\n" +
64 "[t]: reverse stack order\n" +
66 "[s]: update stack to include starred only\n\n" +
75 stack = lightcards.get_orig()[1]
76 menu_print(stdscr, stack, headers, idx,
79 [x.unStar() for x in stack]
80 menu_print(stdscr, stack, headers, idx,
84 menu_print(stdscr, stack, headers, idx,
88 menu_print(stdscr, stack, headers, idx,
92 x[0], x[1] = x[1], x[0]
93 headers[0], headers[1] = headers[1], headers[0]
94 menu_print(stdscr, stack, headers, idx,
97 # Check if there are any starred cards before proceeding, and if
98 # not, don't allow to proceed and show an error message
106 stack = [x for x in stack if x.getStar()]
107 menu_print(stdscr, stack, headers, idx,
110 menu_print(stdscr, stack, headers, idx,
111 "ERR: None are starred!",
114 get_key(stdscr, stack, headers, idx)
117 def disp_card(stdscr, stack, headers, obj):
119 Display the contents of the card
120 Shows a header, a horizontal line, and the contents of the current side.
123 (mlines, mcols) = stdscr.getmaxyx()
124 if obj.getIdx() == len(stack):
125 disp_menu(stdscr, stack, headers, obj)
127 # If on the back of the card, show the content of the front side in the
129 num_done = str(obj.getIdx() + 1).zfill(len(str(len(stack))))
130 if obj.getSide() == 0:
131 top = num_done + " | " + headers[obj.getSide()]
133 top = num_done + " | " + headers[obj.getSide()] + " | \"" + \
134 str(stack[obj.getIdx()][0]) + "\""
139 stdscr.addstr(textwrap.shorten(top, width=header_width,
140 placeholder="…"), curses.A_BOLD)
142 # Add horizontal line
143 lin_width = header_width
144 if len(top) < header_width:
146 stdscr.hline(1, 0, curses.ACS_HLINE, lin_width)
152 stdscr.addstr(2, 0, textwrap.fill(stack[obj.getIdx()][obj.getSide()],
154 disp_bar(stdscr, stack, headers, obj)
157 def disp_help(stdscr, stack, headers, idx):
158 """Display help screen"""
160 stdscr.addstr("LIGHTCARDS HELP", curses.color_pair(1) + curses.A_BOLD)
161 stdscr.hline(1, 0, curses.ACS_HLINE, 15)
163 "Welcome to lightcards. Here are some keybindings to get\n" +
165 "h, left previous card\n" +
166 "l, right next card\n" +
167 "j, k, up, down flip card\n" +
169 "0, ^, home go to the start of the deck\n" +
170 "$, end go to the end of the deck\n" +
171 "H, ? open this screen\n" +
172 "e open the input file in $EDITOR\n\n" +
173 "More information can be found in the man page, or by\n" +
174 "running `lightcards --help`.\n\n" +
175 "Press [q], [H], or [?] to go back.")
177 key = stdscr.getkey()
178 if key in ["q", "H", "?"]:
179 get_key(stdscr, stack, headers, idx)
182 def init_disp(stdscr, stack, headers, idx):
183 """Initialize curses options. Entrypoint into the display functions."""
184 curses.curs_set(0) # Hide cursor
185 curses.init_pair(1, curses.COLOR_CYAN, 0)
186 curses.init_pair(2, curses.COLOR_RED, 0)
187 curses.init_pair(3, curses.COLOR_YELLOW, 0)
188 get_key(stdscr, stack, headers, idx)
191 def get_key(stdscr, stack, headers, idx):
193 Display a card and wait for the input.
194 Used as a general way of getting back into the card flow from a menu
197 disp_card(stdscr, stack, headers, idx)
199 key = stdscr.getkey()
202 elif key in ["l", "KEY_LEFT"]:
205 disp_card(stdscr, stack, headers, idx)
206 elif key in ["h", "KEY_RIGHT"]:
209 disp_card(stdscr, stack, headers, idx)
210 elif key in ["j", "k", "KEY_UP", "KEY_DOWN"]:
212 disp_card(stdscr, stack, headers, idx)
213 elif key in ["i", "/"]:
214 stack[idx.getIdx()].toggleStar()
215 disp_card(stdscr, stack, headers, idx)
216 elif key in ["0", "^", "KEY_HOME"]:
219 disp_card(stdscr, stack, headers, idx)
220 elif key in ["$", "KEY_END"]:
221 idx.setIdx(len(stack) - 1)
223 disp_card(stdscr, stack, headers, idx)
224 elif key in ["H", "?"]:
225 disp_help(stdscr, stack, headers, idx)
227 (headers, stack) = lightcards.reparse()
228 get_key(stdscr, stack, headers, idx)