]> git.armaanb.net Git - lightcards.git/blob - lightcards/display.py
Clean up lightcards.py
[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(3)
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         self.win.clear()
62         if err:
63             self.win.addstr(string + "\n\n", curses.color_pair(2))
64         else:
65             self.win.addstr(string + "\n\n", curses.color_pair(1))
66             self.disp_menu()
67
68     def disp_menu(self):
69         """
70         Display a menu once the end of the deck has been reached, offering
71         multiple options on how to continue.
72         """
73
74         self.win.addstr("Choose one of the following options:\n\n" +
75                         "[y]: reset stack to original state\n" +
76                         "[z]: shuffle stack\n" +
77                         "[f]: flip all cards in stack\n" +
78                         "[t]: reverse stack order\n" +
79                         "[u]: unstar all\n" +
80                         "[s]: update stack to include starred only\n\n" +
81                         "[r]: restart\n" +
82                         "[q]: quit")
83         self.obj.setIdx(0)
84         while True:
85             key = self.win.getkey()
86             if key == "q":
87                 sys.exit(0)
88             elif key == "y":
89                 self.stack = lightcards.get_orig()[1]
90                 self.menu_print("Stack reset!")
91             elif key == "u":
92                 [x.unStar() for x in self.stack]
93                 self.menu_print("All unstarred!")
94             elif key == "t":
95                 self.stack.reverse()
96                 self.menu_print(
97                     "self.stack reversed!")
98             elif key == "z":
99                 shuffle(self.stack)
100                 self.menu_print("self.stack shuffled!")
101             elif key == "f":
102                 for x in self.stack:
103                     x[0], x[1] = x[1], x[0]
104                 (self.headers[0], self.headers[1]) = (self.headers[1],
105                                                       self.headers[0])
106                 self.menu_print("Cards flipped!")
107             elif key == "s":
108                 # Check if there are any starred cards before proceeding, and
109                 # if not, don't allow to proceed and show an error message
110                 cont = False
111                 for x in self.stack:
112                     if x.getStar():
113                         cont = True
114                         break
115
116                 if cont:
117                     self.stack = [x for x in self.stack if x.getStar()]
118                     self.menu_print("Stars only!")
119                 else:
120                     self.menu_print("ERR: None are starred!", err=True)
121             elif key in ["h", "KEY_LEFT"]:
122                 self.obj.setIdx(len(self.stack) - 1)
123                 self.get_key()
124             elif key == "r":
125                 self.get_key()
126
127     def wrap_width(self):
128         (mlines, mcols) = self.win.getmaxyx()
129         wrap_width = mcols
130         if mcols > 80:
131             wrap_width = 80
132         return wrap_width
133
134     def disp_card(self):
135         """
136         Display the contents of the card
137         Shows a header, a horizontal line, and the contents of the current
138         side.
139         """
140         self.win.clear()
141         (mlines, mcols) = self.win.getmaxyx()
142         if self.obj.getIdx() == len(self.stack):
143             self.disp_menu()
144         else:
145             # If on the back of the card, show the content of the front side in
146             # the header
147             num_done = str(self.obj.getIdx() +
148                            1).zfill(len(str(len(self.stack))))
149             if self.obj.getSide() == 0:
150                 top = num_done + " | " + self.headers[self.obj.getSide()]
151             else:
152                 top = num_done + " | " + self.headers[self.obj.getSide()] + \
153                     " | \"" + str(self.stack[self.obj.getIdx()][0]) + "\""
154             header_width = mcols
155             if mcols > 80:
156                 header_width = 80
157
158             self.win.addstr(textwrap.shorten(top, width=header_width,
159                                              placeholder="…"), curses.A_BOLD)
160
161             # Add horizontal line
162             lin_width = header_width
163             if len(top) < header_width:
164                 lin_width = len(top)
165             self.win.hline(1, 0, curses.ACS_HLINE, lin_width)
166
167             # Show current side
168             self.win.addstr(2, 0, textwrap.fill(
169                 self.stack[self.obj.getIdx()][self.obj.getSide()],
170                 width=self.wrap_width()))
171         self.disp_bar()
172
173     def disp_help(self):
174         """Display help screen"""
175         self.win.clear()
176         self.win.addstr("LIGHTCARDS HELP", curses.color_pair(1) +
177                         curses.A_BOLD)
178         self.win.hline(1, 0, curses.ACS_HLINE, 15)
179         self.win.addstr(2, 0, textwrap.fill(
180             "Welcome to lightcards. Here are some keybindings to get you " +
181             "started", width=self.wrap_width()) +
182                         "\n\nh, left          previous card\n" +
183                         "l, right         next card\n" +
184                         "j, k, up, down   flip card\n" +
185                         "i, /             star card\n" +
186                         "0, ^, home       go to the start of the deck\n" +
187                         "$, end           go to the end of the deck\n" +
188                         "H, ?             open this screen\n" +
189                         "e                open the input file in $EDITOR\n\n" +
190                         textwrap.fill(
191                             "More information can be found in the man page, " +
192                             "or by running `lightcards --help`.",
193                             width=self.wrap_width()) +
194                         "\n\nPress [q], [H], or [?] to go back.")
195         while True:
196             key = self.win.getkey()
197             if key in ["q", "H", "?"]:
198                 self.get_key()
199
200     def get_key(self):
201         """
202         Display a card and wait for the input.
203         Used as a general way of getting back into the card flow from a menu
204         """
205
206         self.disp_card()
207         while True:
208             key = self.win.getkey()
209             if key == "q":
210                 sys.exit(0)
211             elif key in ["l", "KEY_RIGHT"]:
212                 self.obj.forward(self.stack)
213                 self.obj.setSide(0)
214                 self.disp_card()
215             elif key in ["h", "KEY_LEFT"]:
216                 self.obj.back()
217                 self.obj.setSide(0)
218                 self.disp_card()
219             elif key in ["j", "k", "KEY_UP", "KEY_DOWN"]:
220                 self.obj.flip()
221                 self.disp_card()
222             elif key in ["i", "/"]:
223                 self.stack[self.obj.getIdx()].toggleStar()
224                 self.disp_card()
225             elif key in ["0", "^", "KEY_HOME"]:
226                 self.obj.setIdx(0)
227                 self.obj.setSide(0)
228                 self.disp_card()
229             elif key in ["$", "KEY_END"]:
230                 self.obj.setIdx(len(self.stack) - 1)
231                 self.obj.setSide(0)
232                 self.disp_card()
233             elif key in ["H", "?"]:
234                 self.disp_help()
235             elif key == "e":
236                 (self.headers, self.stack) = lightcards.reparse()
237                 self.get_key()