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