]> git.armaanb.net Git - lightcards.git/blob - lightcards/display.py
45994917fd3d4d6e416647efcb556ab230e9a832
[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 == "r":
122                 self.get_key()
123
124     def wrap_width(self):
125         (mlines, mcols) = self.win.getmaxyx()
126         wrap_width = mcols
127         if mcols > 80:
128             wrap_width = 80
129         return wrap_width
130
131     def disp_card(self):
132         """
133         Display the contents of the card
134         Shows a header, a horizontal line, and the contents of the current
135         side.
136         """
137         self.win.clear()
138         (mlines, mcols) = self.win.getmaxyx()
139         if self.obj.getIdx() == len(self.stack):
140             self.disp_menu()
141         else:
142             # If on the back of the card, show the content of the front side in
143             # the header
144             num_done = str(self.obj.getIdx() +
145                            1).zfill(len(str(len(self.stack))))
146             if self.obj.getSide() == 0:
147                 top = num_done + " | " + self.headers[self.obj.getSide()]
148             else:
149                 top = num_done + " | " + self.headers[self.obj.getSide()] + \
150                     " | \"" + str(self.stack[self.obj.getIdx()][0]) + "\""
151             header_width = mcols
152             if mcols > 80:
153                 header_width = 80
154
155             self.win.addstr(textwrap.shorten(top, width=header_width,
156                                              placeholder="…"), curses.A_BOLD)
157
158             # Add horizontal line
159             lin_width = header_width
160             if len(top) < header_width:
161                 lin_width = len(top)
162             self.win.hline(1, 0, curses.ACS_HLINE, lin_width)
163
164             # Show current side
165             self.win.addstr(2, 0, textwrap.fill(
166                 self.stack[self.obj.getIdx()][self.obj.getSide()],
167                 width=self.wrap_width()))
168         self.disp_bar()
169
170     def disp_help(self):
171         """Display help screen"""
172         self.win.clear()
173         self.win.addstr("LIGHTCARDS HELP", curses.color_pair(1) +
174                         curses.A_BOLD)
175         self.win.hline(1, 0, curses.ACS_HLINE, 15)
176         self.win.addstr(2, 0, textwrap.fill(
177             "Welcome to lightcards. Here are some keybindings to get you " +
178             "started", width=self.wrap_width()) +
179                         "\n\nh, left          previous card\n" +
180                         "l, right         next card\n" +
181                         "j, k, up, down   flip card\n" +
182                         "i, /             star card\n" +
183                         "0, ^, home       go to the start of the deck\n" +
184                         "$, end           go to the end of the deck\n" +
185                         "H, ?             open this screen\n" +
186                         "e                open the input file in $EDITOR\n\n" +
187                         textwrap.fill(
188                             "More information can be found in the man page, " +
189                             "or by running `lightcards --help`.",
190                             width=self.wrap_width()) +
191                         "\n\nPress [q], [H], or [?] to go back.")
192         while True:
193             key = self.win.getkey()
194             if key in ["q", "H", "?"]:
195                 self.get_key()
196
197     def get_key(self):
198         """
199         Display a card and wait for the input.
200         Used as a general way of getting back into the card flow from a menu
201         """
202
203         self.disp_card()
204         while True:
205             key = self.win.getkey()
206             if key == "q":
207                 sys.exit(0)
208             elif key in ["l", "KEY_RIGHT"]:
209                 self.obj.forward(self.stack)
210                 self.obj.setSide(0)
211                 self.disp_card()
212             elif key in ["h", "KEY_LEFT"]:
213                 self.obj.back()
214                 self.obj.setSide(0)
215                 self.disp_card()
216             elif key in ["j", "k", "KEY_UP", "KEY_DOWN"]:
217                 self.obj.flip()
218                 self.disp_card()
219             elif key in ["i", "/"]:
220                 self.stack[self.obj.getIdx()].toggleStar()
221                 self.disp_card()
222             elif key in ["0", "^", "KEY_HOME"]:
223                 self.obj.setIdx(0)
224                 self.obj.setSide(0)
225                 self.disp_card()
226             elif key in ["$", "KEY_END"]:
227                 self.obj.setIdx(len(self.stack) - 1)
228                 self.obj.setSide(0)
229                 self.disp_card()
230             elif key in ["H", "?"]:
231                 self.disp_help()
232             elif key == "e":
233                 (self.headers, self.stack) = lightcards.reparse()
234                 self.get_key()