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 # Put all the info together
24 stdscr.insstr(mlines - 1, 0,
26 stack[obj.getIdx()].printStar() +
30 str(obj.getIdx() + 1) +
35 headers[obj.getSide()] +
37 str(obj.getSide() + 1) +
38 ")]", curses.color_pair(1))
41 def disp_menu(stdscr, stack, headers, idx):
43 Display a menu once the end of the deck has been reached, offering
44 multiple options on how to continue.
46 stdscr.addstr("Good job, you've completed a round!\n\n",
48 stdscr.addstr("Choose one of the following options:\n" +
50 "[s]: restart with starred only\n" +
51 "[u]: restart and unstar all\n" +
52 "[z]: restart and shuffle cards\n" +
60 get_key(stdscr, stack, headers, idx)
65 get_key(stdscr, stack, headers, idx)
67 # Check if there are any starred cards before proceeding, and if
68 # not, don't allow to proceed and show an error message
76 stack = [x for x in stack if x.getStar()]
77 get_key(stdscr, stack, headers, idx)
80 stdscr.addstr("ERR: Stack empty. Choose another option\n\n",
82 disp_menu(stdscr, stack, headers, idx)
86 get_key(stdscr, stack, headers, idx)
89 def disp_card(stdscr, stack, headers, obj):
91 Display the contents of the card
92 Shows a header, a horizontal line, and the contents of the current side.
95 (mlines, mcols) = stdscr.getmaxyx()
96 if obj.getIdx() == len(stack):
97 disp_menu(stdscr, stack, headers, obj)
99 # If on the back of the card, show the content of the front side in the
101 if obj.getSide() == 0:
102 top = str(obj.getIdx() + 1) + " | " + headers[obj.getSide()]
104 top = str(obj.getIdx() + 1) + " | " + headers[obj.getSide()] \
105 + " | \"" + str(stack[obj.getIdx()][0]) + "\""
110 stdscr.addstr(textwrap.shorten(top, width=header_width,
111 placeholder="…"), curses.A_BOLD)
113 # Add horizontal line
114 lin_width = header_width
115 if len(top) < header_width:
117 stdscr.hline(1, 0, curses.ACS_HLINE, lin_width)
123 stdscr.addstr(2, 0, textwrap.fill(stack[obj.getIdx()][obj.getSide()],
125 disp_bar(stdscr, stack, headers, obj)
128 def disp_help(stdscr, stack, headers, idx):
129 """Display help screen"""
131 stdscr.addstr("LIGHTCARDS HELP", curses.color_pair(1) + curses.A_BOLD)
132 stdscr.hline(1, 0, curses.ACS_HLINE, 15)
134 "Welcome to lightcards. Here are some keybindings to get\n" +
136 "h, left previous card\n" +
137 "l, right next card\n" +
138 "j, k, up, down flip card\n" +
140 "0, ^, home go to the start of the deck\n" +
141 "$, end go to the end of the deck\n" +
142 "H, ? open this screen\n\n" +
143 "More information can be found in the man page, or by\n" +
144 "running `lightcards --help`.\n\n" +
145 "Press [q], [H], or [?] to go back.")
147 key = stdscr.getkey()
148 if key in ["q", "H", "?"]:
149 get_key(stdscr, stack, headers, idx)
152 def init_disp(stdscr, stack, headers, idx):
153 """Initialize curses options. Entrypoint into the display functions."""
154 curses.curs_set(0) # Hide cursor
155 curses.init_pair(1, curses.COLOR_CYAN, 0)
156 curses.init_pair(2, curses.COLOR_RED, 0)
157 get_key(stdscr, stack, headers, idx)
160 def get_key(stdscr, stack, headers, idx):
162 Display a card and wait for the input.
163 Used as a general way of getting back into the card flow from a menu
166 disp_card(stdscr, stack, headers, idx)
168 key = stdscr.getkey()
171 elif key in ["l", "KEY_LEFT"]:
174 disp_card(stdscr, stack, headers, idx)
175 elif key in ["h", "KEY_RIGHT"]:
178 disp_card(stdscr, stack, headers, idx)
179 elif key in ["j", "k", "KEY_UP", "KEY_DOWN"]:
181 disp_card(stdscr, stack, headers, idx)
182 elif key in ["i", "/"]:
183 stack[idx.getIdx()].toggleStar()
184 disp_card(stdscr, stack, headers, idx)
185 elif key in ["0", "^", "KEY_HOME"]:
188 disp_card(stdscr, stack, headers, idx)
189 elif key in ["$", "KEY_END"]:
190 idx.setIdx(len(stack) - 1)
192 disp_card(stdscr, stack, headers, idx)
193 elif key in ["H", "?"]:
194 disp_help(stdscr, stack, headers, idx)