]> git.armaanb.net Git - lightcards.git/blob - lightcards/display.py
Make control menu always accessible with "m"
[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 from . import lightcards
10
11
12 class Display():
13     def __init__(self, stack, headers, obj):
14         self.stack = stack
15         self.headers = headers
16         self.obj = obj
17
18     def run(self, stdscr):
19         self.win = stdscr
20         curses.curs_set(0)  # Hide cursor
21         curses.init_pair(1, curses.COLOR_CYAN, 0)
22         curses.init_pair(2, curses.COLOR_RED, 0)
23         curses.init_pair(3, curses.COLOR_YELLOW, 0)
24         self.get_key()
25
26     def disp_bar(self):
27         """
28         Display the statusbar at the bottom of the screen with progress, star
29         status, and card side.
30         """
31         (mlines, mcols) = self.win.getmaxyx()
32
33         # Calculate percent done
34         if len(self.stack) <= 1:
35             percent = "100"
36         else:
37             percent = str(round(self.obj.getIdx() /
38                                 len(self.stack) * 100)).zfill(2)
39
40         # Print yellow if starred
41         if self.stack[self.obj.getIdx()].getStar():
42             star_color = curses.color_pair(3)
43         else:
44             star_color = curses.color_pair(1)
45
46         # Create bar component
47         bar_start = "["
48         bar_middle = self.stack[self.obj.getIdx()].printStar()
49         bar_end = "] [" + percent + "% (" + \
50             str(self.obj.getIdx() + 1).zfill(len(str(len(self.stack)))) + \
51             "/" + str(len(self.stack)) + ")] [" + \
52             self.headers[self.obj.getSide()] + " (" + \
53             str(self.obj.getSide() + 1) + ")]"
54
55         # Put it all togethor
56         self.win.addstr(mlines - 1, 0, bar_start, curses.color_pair(1))
57         self.win.addstr(bar_middle, star_color)
58         self.win.insstr(bar_end, curses.color_pair(1))
59
60     def menu_print(self, string, err=False):
61         (mlines, mcols) = self.win.getmaxyx()
62         self.win.clear()
63         if err:
64             color = curses.color_pair(2)
65         else:
66             color = curses.color_pair(1)
67         self.disp_menu(keygrab=False)
68         self.win.addstr("\n\n" + string, color)
69         self.menu_grab()
70
71     def menu_grab(self):
72         while True:
73             key = self.win.getkey()
74             if key == "q":
75                 if len(self.stack) == self.obj.getIdx():
76                     sys.exit(0)
77                 elif len(self.stack) < self.obj.getIdx():
78                     self.obj.setIdx(0)
79                 self.get_key()
80             elif key == "y":
81                 self.stack = lightcards.get_orig()[1]
82                 self.menu_print("Stack reset!")
83             elif key == "u":
84                 [x.unStar() for x in self.stack]
85                 self.menu_print("All unstarred!")
86             elif key == "d":
87                 [x.star() for x in self.stack]
88                 self.menu_print("All starred!")
89             elif key == "t":
90                 self.stack.reverse()
91                 self.menu_print(
92                     "self.stack reversed!")
93             elif key == "z":
94                 shuffle(self.stack)
95                 self.menu_print("Stack shuffled!")
96             elif key == "f":
97                 for x in self.stack:
98                     x[0], x[1] = x[1], x[0]
99                 (self.headers[0], self.headers[1]) = (self.headers[1],
100                                                       self.headers[0])
101                 self.menu_print("Cards flipped!")
102             elif key == "s":
103                 # Check if there are any starred cards before proceeding, and
104                 # if not, don't allow to proceed and show an error message
105                 cont = False
106                 for x in self.stack:
107                     if x.getStar():
108                         cont = True
109                         break
110
111                 if cont:
112                     self.stack = [x for x in self.stack if x.getStar()]
113                     self.menu_print("Stars only!")
114                 else:
115                     self.menu_print("ERR: None are starred!", err=True)
116             elif key in ["h", "KEY_LEFT"]:
117                 self.obj.setIdx(len(self.stack) - 1)
118                 self.get_key()
119             elif key == "r":
120                 self.obj.setIdx(0)
121                 self.get_key()
122
123     def disp_menu(self, keygrab=True):
124         """
125         Display a menu once the end of the deck has been reached, offering
126         multiple options on how to continue.
127         """
128
129         self.win.addstr("LIGHTCARDS MENU", curses.color_pair(1) +
130                         curses.A_BOLD)
131         self.win.hline(1, 0, curses.ACS_HLINE, 15)
132         self.win.addstr(2, 0, "[y]: reset stack to original state\n" +
133                         "[z]: shuffle stack\n" +
134                         "[f]: flip all cards in stack\n" +
135                         "[t]: reverse stack order\n" +
136                         "[u]: unstar all\n" +
137                         "[d]: star all\n" +
138                         "[s]: update stack to include starred only\n\n" +
139                         "[r]: restart\n" +
140                         "[q]: back")
141
142         if keygrab:
143             self.menu_grab()
144
145     def wrap_width(self):
146         (mlines, mcols) = self.win.getmaxyx()
147         wrap_width = mcols
148         if mcols > 80:
149             wrap_width = 80
150         return wrap_width
151
152     def disp_card(self):
153         """
154         Display the contents of the card
155         Shows a header, a horizontal line, and the contents of the current
156         side.
157         """
158         self.win.clear()
159         (mlines, mcols) = self.win.getmaxyx()
160         if self.obj.getIdx() == len(self.stack):
161             self.disp_menu()
162         else:
163             # If on the back of the card, show the content of the front side in
164             # the header
165             num_done = str(self.obj.getIdx() +
166                            1).zfill(len(str(len(self.stack))))
167             if self.obj.getSide() == 0:
168                 top = num_done + " | " + self.headers[self.obj.getSide()]
169             else:
170                 top = num_done + " | " + self.headers[self.obj.getSide()] + \
171                     " | \"" + str(self.stack[self.obj.getIdx()][0]) + "\""
172             header_width = mcols
173             if mcols > 80:
174                 header_width = 80
175
176             self.win.addstr(textwrap.shorten(top, width=header_width,
177                                              placeholder="…"), curses.A_BOLD)
178
179             # Add horizontal line
180             lin_width = header_width
181             if len(top) < header_width:
182                 lin_width = len(top)
183             self.win.hline(1, 0, curses.ACS_HLINE, lin_width)
184
185             # Show current side
186             self.win.addstr(2, 0, textwrap.fill(
187                 self.stack[self.obj.getIdx()][self.obj.getSide()],
188                 width=self.wrap_width()))
189         self.disp_bar()
190
191     def disp_help(self):
192         """Display help screen"""
193         self.win.clear()
194         self.win.addstr("LIGHTCARDS HELP", curses.color_pair(1) +
195                         curses.A_BOLD)
196         self.win.hline(1, 0, curses.ACS_HLINE, 15)
197         self.win.addstr(2, 0, textwrap.fill(
198             "Welcome to lightcards. Here are some keybindings to get you " +
199             "started:", width=self.wrap_width()) +
200                         "\n\nh, left          previous card\n" +
201                         "l, right         next card\n" +
202                         "j, k, up, down   flip card\n" +
203                         "i, /             star card\n" +
204                         "0, ^, home       go to the start of the deck\n" +
205                         "$, end           go to the end of the deck\n" +
206                         "H, ?             open this screen\n" +
207                         "e                open the input file in $EDITOR\n" +
208                         "m                open the control menu\n\n" +
209                         textwrap.fill(
210                             "More information can be found in the man page, " +
211                             "or by running `lightcards --help`.",
212                             width=self.wrap_width()) +
213                         "\n\nPress [q], [H], or [?] to go back.")
214         while True:
215             key = self.win.getkey()
216             if key in ["q", "H", "?"]:
217                 self.get_key()
218
219     def get_key(self):
220         """
221         Display a card and wait for the input.
222         Used as a general way of getting back into the card flow from a menu
223         """
224
225         self.disp_card()
226         while True:
227             key = self.win.getkey()
228             if key == "q":
229                 sys.exit(0)
230             elif key in ["l", "KEY_RIGHT"]:
231                 self.obj.forward(self.stack)
232                 self.obj.setSide(0)
233                 self.disp_card()
234             elif key in ["h", "KEY_LEFT"]:
235                 self.obj.back()
236                 self.obj.setSide(0)
237                 self.disp_card()
238             elif key in ["j", "k", "KEY_UP", "KEY_DOWN"]:
239                 self.obj.flip()
240                 self.disp_card()
241             elif key in ["i", "/"]:
242                 self.stack[self.obj.getIdx()].toggleStar()
243                 self.disp_card()
244             elif key in ["0", "^", "KEY_HOME"]:
245                 self.obj.setIdx(0)
246                 self.obj.setSide(0)
247                 self.disp_card()
248             elif key in ["$", "KEY_END"]:
249                 self.obj.setIdx(len(self.stack) - 1)
250                 self.obj.setSide(0)
251                 self.disp_card()
252             elif key in ["H", "?"]:
253                 self.disp_help()
254             elif key == "m":
255                 self.win.clear()
256                 self.disp_menu()
257             elif key == "e":
258                 (self.headers, self.stack) = lightcards.reparse()
259                 self.get_key()