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