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