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