]> git.armaanb.net Git - lightcards.git/blob - lightcards/display.py
Add more options to restart menu at end of deck
[lightcards.git] / lightcards / display.py
1 # Display card output and retreive input
2 # Armaan Bhojwani 2021
3
4 import curses
5 from random import shuffle
6 import sys
7 import textwrap
8
9
10 def disp_bar(stdscr, stack, headers, obj):
11     """
12     Display the statusbar at the bottom of the screen with progress, star
13     status, and card side.
14     """
15     (mlines, mcols) = stdscr.getmaxyx()
16
17     # Calculate percent done
18     if len(stack) <= 1:
19         percent = "100"
20     else:
21         percent = str(round(obj.getIdx() / (len(stack) - 1) * 100)).zfill(3)
22
23     # Put all the info together
24     stdscr.insstr(mlines - 1, 0,
25                   "[" +
26                   stack[obj.getIdx()].printStar() +
27                   "] [" +
28                   percent +
29                   "% (" +
30                   str(obj.getIdx() + 1).zfill(len(str(len(stack)))) +
31                   "/" +
32                   str(len(stack)) +
33                   ")]" +
34                   " [" +
35                   headers[obj.getSide()] +
36                   " (" +
37                   str(obj.getSide() + 1) +
38                   ")]", curses.color_pair(1))
39
40
41 def disp_menu(stdscr, stack, headers, idx):
42     """
43     Display a menu once the end of the deck has been reached, offering
44     multiple options on how to continue.
45     """
46     stdscr.addstr("Good job, you've completed a round!\n\n",
47                   curses.color_pair(1))
48     stdscr.addstr("Choose one of the following options:\n" +
49                   "[r]: restart\n" +
50                   "[s]: restart with starred only\n" +
51                   "[u]: restart and unstar all\n" +
52                   "[z]: restart and shuffle cards\n" +
53                   "[f]: restart and show the other side first\n" +
54                   "[t]: restart in reverse order\n" +
55                   "[q]: quit")
56     while True:
57         key = stdscr.getkey()
58         if key == "q":
59             sys.exit(0)
60         elif key == "r":
61             idx.setIdx(0)
62             get_key(stdscr, stack, headers, idx)
63         elif key == "s":
64             # Check if there are any starred cards before proceeding, and if
65             # not, don't allow to proceed and show an error message
66             cont = False
67             for x in stack:
68                 if x.getStar():
69                     cont = True
70
71             if cont:
72                 idx.setIdx(0)
73                 stack = [x for x in stack if x.getStar()]
74                 get_key(stdscr, stack, headers, idx)
75             else:
76                 stdscr.clear()
77                 stdscr.addstr("ERR: Stack empty. Choose another option\n\n",
78                               curses.color_pair(2))
79                 disp_menu(stdscr, stack, headers, idx)
80         elif key == "u":
81             idx.setIdx(0)
82             for x in stack:
83                 x.unStar()
84             get_key(stdscr, stack, headers, idx)
85         elif key == "z":
86             idx.setIdx(0)
87             shuffle(stack)
88             get_key(stdscr, stack, headers, idx)
89         elif key == "f":
90             idx.setIdx(0)
91             for x in stack:
92                 x[0], x[1] = x[1], x[0]
93             headers[0], headers[1] = headers[1], headers[0]
94             get_key(stdscr, stack, headers, idx)
95         elif key == "t":
96             idx.setIdx(0)
97             stack.reverse()
98             get_key(stdscr, stack, headers, idx)
99
100
101 def disp_card(stdscr, stack, headers, obj):
102     """
103     Display the contents of the card
104     Shows a header, a horizontal line, and the contents of the current side.
105     """
106     stdscr.clear()
107     (mlines, mcols) = stdscr.getmaxyx()
108     if obj.getIdx() == len(stack):
109         disp_menu(stdscr, stack, headers, obj)
110     else:
111         # If on the back of the card, show the content of the front side in the
112         # header
113         num_done = str(obj.getIdx() + 1).zfill(len(str(len(stack))))
114         if obj.getSide() == 0:
115             top = num_done + " | " + headers[obj.getSide()]
116         else:
117             top = num_done + " | " + headers[obj.getSide()] + " | \"" + \
118                 str(stack[obj.getIdx()][0]) + "\""
119         header_width = mcols
120         if mcols > 80:
121             header_width = 80
122
123         stdscr.addstr(textwrap.shorten(top, width=header_width,
124                                        placeholder="…"), curses.A_BOLD)
125
126         # Add horizontal line
127         lin_width = header_width
128         if len(top) < header_width:
129             lin_width = len(top)
130         stdscr.hline(1, 0, curses.ACS_HLINE, lin_width)
131
132         # Show current side
133         wrap_width = mcols
134         if mcols > 80:
135             wrap_width = 80
136         stdscr.addstr(2, 0, textwrap.fill(stack[obj.getIdx()][obj.getSide()],
137                                           width=wrap_width))
138     disp_bar(stdscr, stack, headers, obj)
139
140
141 def disp_help(stdscr, stack, headers, idx):
142     """Display help screen"""
143     stdscr.clear()
144     stdscr.addstr("LIGHTCARDS HELP", curses.color_pair(1) + curses.A_BOLD)
145     stdscr.hline(1, 0, curses.ACS_HLINE, 15)
146     stdscr.addstr(2, 0,
147                   "Welcome to lightcards. Here are some keybindings to get\n" +
148                   "you started.\n\n" +
149                   "h, left            previous card\n" +
150                   "l, right           next card\n" +
151                   "j, k, up, down     flip card\n" +
152                   "i, /               star card\n" +
153                   "0, ^, home         go to the start of the deck\n" +
154                   "$, end             go to the end of the deck\n" +
155                   "H, ?               open this screen\n\n" +
156                   "More information can be found in the man page, or by\n" +
157                   "running `lightcards --help`.\n\n" +
158                   "Press [q], [H], or [?] to go back.")
159     while True:
160         key = stdscr.getkey()
161         if key in ["q", "H", "?"]:
162             get_key(stdscr, stack, headers, idx)
163
164
165 def init_disp(stdscr, stack, headers, idx):
166     """Initialize curses options. Entrypoint into the display functions."""
167     curses.curs_set(0)  # Hide cursor
168     curses.init_pair(1, curses.COLOR_CYAN, 0)
169     curses.init_pair(2, curses.COLOR_RED, 0)
170     get_key(stdscr, stack, headers, idx)
171
172
173 def get_key(stdscr, stack, headers, idx):
174     """
175     Display a card and wait for the input.
176     Used as a general way of getting back into the card flow from a menu
177     """
178
179     disp_card(stdscr, stack, headers, idx)
180     while True:
181         key = stdscr.getkey()
182         if key == "q":
183             sys.exit(0)
184         elif key in ["l", "KEY_LEFT"]:
185             idx.forward(stack)
186             idx.setSide(0)
187             disp_card(stdscr, stack, headers, idx)
188         elif key in ["h", "KEY_RIGHT"]:
189             idx.back()
190             idx.setSide(0)
191             disp_card(stdscr, stack, headers,  idx)
192         elif key in ["j", "k", "KEY_UP", "KEY_DOWN"]:
193             idx.flip()
194             disp_card(stdscr, stack, headers, idx)
195         elif key in ["i", "/"]:
196             stack[idx.getIdx()].toggleStar()
197             disp_card(stdscr, stack, headers, idx)
198         elif key in ["0", "^", "KEY_HOME"]:
199             idx.setIdx(0)
200             idx.setSide(0)
201             disp_card(stdscr, stack, headers, idx)
202         elif key in ["$", "KEY_END"]:
203             idx.setIdx(len(stack) - 1)
204             idx.setSide(0)
205             disp_card(stdscr, stack, headers, idx)
206         elif key in ["H", "?"]:
207             disp_help(stdscr, stack, headers, idx)