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 stdscr.addstr("Good job, you've completed a round!\n\n",
54 stdscr.addstr("Choose one of the following options:\n" +
56 "[s]: restart with starred only\n" +
57 "[u]: restart and unstar all\n" +
58 "[z]: restart and shuffle cards\n" +
59 "[f]: restart and show the other side first\n" +
60 "[t]: restart in reverse order\n" +
68 get_key(stdscr, stack, headers, idx)
70 # Check if there are any starred cards before proceeding, and if
71 # not, don't allow to proceed and show an error message
79 stack = [x for x in stack if x.getStar()]
80 get_key(stdscr, stack, headers, idx)
83 stdscr.addstr("ERR: Stack empty. Choose another option\n\n",
85 disp_menu(stdscr, stack, headers, idx)
90 get_key(stdscr, stack, headers, idx)
94 get_key(stdscr, stack, headers, idx)
98 x[0], x[1] = x[1], x[0]
99 headers[0], headers[1] = headers[1], headers[0]
100 get_key(stdscr, stack, headers, idx)
104 get_key(stdscr, stack, headers, idx)
107 def disp_card(stdscr, stack, headers, obj):
109 Display the contents of the card
110 Shows a header, a horizontal line, and the contents of the current side.
113 (mlines, mcols) = stdscr.getmaxyx()
114 if obj.getIdx() == len(stack):
115 disp_menu(stdscr, stack, headers, obj)
117 # If on the back of the card, show the content of the front side in the
119 num_done = str(obj.getIdx() + 1).zfill(len(str(len(stack))))
120 if obj.getSide() == 0:
121 top = num_done + " | " + headers[obj.getSide()]
123 top = num_done + " | " + headers[obj.getSide()] + " | \"" + \
124 str(stack[obj.getIdx()][0]) + "\""
129 stdscr.addstr(textwrap.shorten(top, width=header_width,
130 placeholder="…"), curses.A_BOLD)
132 # Add horizontal line
133 lin_width = header_width
134 if len(top) < header_width:
136 stdscr.hline(1, 0, curses.ACS_HLINE, lin_width)
142 stdscr.addstr(2, 0, textwrap.fill(stack[obj.getIdx()][obj.getSide()],
144 disp_bar(stdscr, stack, headers, obj)
147 def disp_help(stdscr, stack, headers, idx):
148 """Display help screen"""
150 stdscr.addstr("LIGHTCARDS HELP", curses.color_pair(1) + curses.A_BOLD)
151 stdscr.hline(1, 0, curses.ACS_HLINE, 15)
153 "Welcome to lightcards. Here are some keybindings to get\n" +
155 "h, left previous card\n" +
156 "l, right next card\n" +
157 "j, k, up, down flip card\n" +
159 "0, ^, home go to the start of the deck\n" +
160 "$, end go to the end of the deck\n" +
161 "H, ? open this screen\n" +
162 "e open the input file in $EDITOR\n\n" +
163 "More information can be found in the man page, or by\n" +
164 "running `lightcards --help`.\n\n" +
165 "Press [q], [H], or [?] to go back.")
167 key = stdscr.getkey()
168 if key in ["q", "H", "?"]:
169 get_key(stdscr, stack, headers, idx)
172 def init_disp(stdscr, stack, headers, idx):
173 """Initialize curses options. Entrypoint into the display functions."""
174 curses.curs_set(0) # Hide cursor
175 curses.init_pair(1, curses.COLOR_CYAN, 0)
176 curses.init_pair(2, curses.COLOR_RED, 0)
177 curses.init_pair(3, curses.COLOR_YELLOW, 0)
178 get_key(stdscr, stack, headers, idx)
181 def get_key(stdscr, stack, headers, idx):
183 Display a card and wait for the input.
184 Used as a general way of getting back into the card flow from a menu
187 disp_card(stdscr, stack, headers, idx)
189 key = stdscr.getkey()
192 elif key in ["l", "KEY_LEFT"]:
195 disp_card(stdscr, stack, headers, idx)
196 elif key in ["h", "KEY_RIGHT"]:
199 disp_card(stdscr, stack, headers, idx)
200 elif key in ["j", "k", "KEY_UP", "KEY_DOWN"]:
202 disp_card(stdscr, stack, headers, idx)
203 elif key in ["i", "/"]:
204 stack[idx.getIdx()].toggleStar()
205 disp_card(stdscr, stack, headers, idx)
206 elif key in ["0", "^", "KEY_HOME"]:
209 disp_card(stdscr, stack, headers, idx)
210 elif key in ["$", "KEY_END"]:
211 idx.setIdx(len(stack) - 1)
213 disp_card(stdscr, stack, headers, idx)
214 elif key in ["H", "?"]:
215 disp_help(stdscr, stack, headers, idx)
217 (headers, stack) = lightcards.reparse()
218 get_key(stdscr, stack, headers, idx)