1 # Display card output and retreive input
5 from random import shuffle
10 def disp_bar(stdscr, stack, headers, obj):
12 Display the statusbar at the bottom of the screen with progress, star
13 status, and card side.
15 (mlines, mcols) = stdscr.getmaxyx()
17 # Calculate percent done
21 percent = str(round(obj.getIdx() / (len(stack) - 1) * 100)).zfill(3)
23 # Print yellow if starred
24 stdscr.addstr(mlines - 1, 0, "[", curses.color_pair(1))
25 if stack[obj.getIdx()].getStar():
26 stdscr.addstr(stack[obj.getIdx()].printStar(), curses.color_pair(3))
28 stdscr.addstr(stack[obj.getIdx()].printStar(), curses.color_pair(1))
30 # Put all the info together
34 str(obj.getIdx() + 1).zfill(len(str(len(stack)))) +
39 headers[obj.getSide()] +
41 str(obj.getSide() + 1) +
42 ")]", curses.color_pair(1))
45 def disp_menu(stdscr, stack, headers, idx):
47 Display a menu once the end of the deck has been reached, offering
48 multiple options on how to continue.
50 stdscr.addstr("Good job, you've completed a round!\n\n",
52 stdscr.addstr("Choose one of the following options:\n" +
54 "[s]: restart with starred only\n" +
55 "[u]: restart and unstar all\n" +
56 "[z]: restart and shuffle cards\n" +
57 "[f]: restart and show the other side first\n" +
58 "[t]: restart in reverse order\n" +
66 get_key(stdscr, stack, headers, idx)
68 # Check if there are any starred cards before proceeding, and if
69 # not, don't allow to proceed and show an error message
77 stack = [x for x in stack if x.getStar()]
78 get_key(stdscr, stack, headers, idx)
81 stdscr.addstr("ERR: Stack empty. Choose another option\n\n",
83 disp_menu(stdscr, stack, headers, idx)
88 get_key(stdscr, stack, headers, idx)
92 get_key(stdscr, stack, headers, idx)
96 x[0], x[1] = x[1], x[0]
97 headers[0], headers[1] = headers[1], headers[0]
98 get_key(stdscr, stack, headers, idx)
102 get_key(stdscr, stack, headers, idx)
105 def disp_card(stdscr, stack, headers, obj):
107 Display the contents of the card
108 Shows a header, a horizontal line, and the contents of the current side.
111 (mlines, mcols) = stdscr.getmaxyx()
112 if obj.getIdx() == len(stack):
113 disp_menu(stdscr, stack, headers, obj)
115 # If on the back of the card, show the content of the front side in the
117 num_done = str(obj.getIdx() + 1).zfill(len(str(len(stack))))
118 if obj.getSide() == 0:
119 top = num_done + " | " + headers[obj.getSide()]
121 top = num_done + " | " + headers[obj.getSide()] + " | \"" + \
122 str(stack[obj.getIdx()][0]) + "\""
127 stdscr.addstr(textwrap.shorten(top, width=header_width,
128 placeholder="…"), curses.A_BOLD)
130 # Add horizontal line
131 lin_width = header_width
132 if len(top) < header_width:
134 stdscr.hline(1, 0, curses.ACS_HLINE, lin_width)
140 stdscr.addstr(2, 0, textwrap.fill(stack[obj.getIdx()][obj.getSide()],
142 disp_bar(stdscr, stack, headers, obj)
145 def disp_help(stdscr, stack, headers, idx):
146 """Display help screen"""
148 stdscr.addstr("LIGHTCARDS HELP", curses.color_pair(1) + curses.A_BOLD)
149 stdscr.hline(1, 0, curses.ACS_HLINE, 15)
151 "Welcome to lightcards. Here are some keybindings to get\n" +
153 "h, left previous card\n" +
154 "l, right next card\n" +
155 "j, k, up, down flip card\n" +
157 "0, ^, home go to the start of the deck\n" +
158 "$, end go to the end of the deck\n" +
159 "H, ? open this screen\n\n" +
160 "More information can be found in the man page, or by\n" +
161 "running `lightcards --help`.\n\n" +
162 "Press [q], [H], or [?] to go back.")
164 key = stdscr.getkey()
165 if key in ["q", "H", "?"]:
166 get_key(stdscr, stack, headers, idx)
169 def init_disp(stdscr, stack, headers, idx):
170 """Initialize curses options. Entrypoint into the display functions."""
171 curses.curs_set(0) # Hide cursor
172 curses.init_pair(1, curses.COLOR_CYAN, 0)
173 curses.init_pair(2, curses.COLOR_RED, 0)
174 curses.init_pair(3, curses.COLOR_YELLOW, 0)
175 get_key(stdscr, stack, headers, idx)
178 def get_key(stdscr, stack, headers, idx):
180 Display a card and wait for the input.
181 Used as a general way of getting back into the card flow from a menu
184 disp_card(stdscr, stack, headers, idx)
186 key = stdscr.getkey()
189 elif key in ["l", "KEY_LEFT"]:
192 disp_card(stdscr, stack, headers, idx)
193 elif key in ["h", "KEY_RIGHT"]:
196 disp_card(stdscr, stack, headers, idx)
197 elif key in ["j", "k", "KEY_UP", "KEY_DOWN"]:
199 disp_card(stdscr, stack, headers, idx)
200 elif key in ["i", "/"]:
201 stack[idx.getIdx()].toggleStar()
202 disp_card(stdscr, stack, headers, idx)
203 elif key in ["0", "^", "KEY_HOME"]:
206 disp_card(stdscr, stack, headers, idx)
207 elif key in ["$", "KEY_END"]:
208 idx.setIdx(len(stack) - 1)
210 disp_card(stdscr, stack, headers, idx)
211 elif key in ["H", "?"]:
212 disp_help(stdscr, stack, headers, idx)