]> git.armaanb.net Git - lightcards.git/blob - lightcards/display.py
bdad1d1e3c82acdef7dfec5da5167cfb9f9d0795
[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     (mlines, mcols) = stdscr.getmaxyx()
12     if len(stack) <= 1:
13         percent = "100"
14     else:
15         percent = str(round(obj.getIdx() / (len(stack) - 1) * 100)).zfill(3)
16
17     stdscr.insstr(mlines - 1, 0,
18                   "[" +
19                   stack[obj.getIdx()].printStar() +
20                   "] [" +
21                   percent +
22                   "% (" +
23                   str(obj.getIdx() + 1) +
24                   "/" +
25                   str(len(stack)) +
26                   ")]" +
27                   " [" +
28                   headers[obj.getSide()] +
29                   " (" +
30                   str(obj.getSide() + 1) +
31                   ")]", curses.color_pair(1))
32
33
34 def disp_menu(stdscr, stack, headers, idx):
35     stdscr.addstr("Good job, you've completed a round!\n\n",
36                   curses.color_pair(1))
37     stdscr.addstr("Choose one of the following options:\n" +
38                   "[r]: restart\n" +
39                   "[s]: restart with starred only\n" +
40                   "[u]: restart and unstar all\n" +
41                   "[z]: restart and shuffle cards\n" +
42                   "[q]: quit")
43     while True:
44         key = stdscr.getkey()
45         if key == "q":
46             sys.exit(0)
47         elif key == "r":
48             idx.setIdx(0)
49             get_key(stdscr, stack, headers, idx)
50         elif key == "u":
51             idx.setIdx(0)
52             for x in stack:
53                 x.unStar()
54             get_key(stdscr, stack, headers, idx)
55         elif key == "s":
56             cont = False
57             for x in stack:
58                 if x.getStar():
59                     cont = True
60
61             if cont:
62                 idx.setIdx(0)
63                 stack = [x for x in stack if x.getStar()]
64                 get_key(stdscr, stack, headers, idx)
65             else:
66                 stdscr.clear()
67                 stdscr.addstr("ERR: Stack empty. Choose another option\n\n",
68                               curses.color_pair(2))
69                 disp_menu(stdscr, stack, headers, idx)
70         elif key == "z":
71             idx.setIdx(0)
72             shuffle(stack)
73             get_key(stdscr, stack, headers, idx)
74
75
76 def disp_card(stdscr, stack, headers, obj):
77     stdscr.clear()
78     (mlines, mcols) = stdscr.getmaxyx()
79     if obj.getIdx() == len(stack):
80         disp_menu(stdscr, stack, headers, obj)
81     else:
82         if obj.getSide() == 0:
83             top = str(obj.getIdx() + 1) + " | " + headers[obj.getSide()]
84         else:
85             top = str(obj.getIdx() + 1) + " | " + headers[obj.getSide()] \
86                 + " | \"" + str(stack[obj.getIdx()][0]) + "\""
87         header_width = mcols
88         if mcols > 80:
89             header_width = 80
90
91         stdscr.addstr(textwrap.shorten(top, width=header_width,
92                                        placeholder="…"), curses.A_BOLD)
93
94         # Add horizontal line
95         lin_width = header_width
96         if len(top) < header_width:
97             lin_width = len(top)
98         stdscr.hline(1, 0, curses.ACS_HLINE, lin_width)
99
100         # Show current side
101         wrap_width = mcols
102         if mcols > 80:
103             wrap_width = 80
104         stdscr.addstr(2, 0, textwrap.fill(stack[obj.getIdx()][obj.getSide()],
105                                           width=wrap_width))
106     disp_bar(stdscr, stack, headers, obj)
107
108
109 def disp_help(stdscr, stack, headers, idx):
110     stdscr.clear()
111     stdscr.addstr("LIGHTCARDS HELP SCREEN", curses.color_pair(1))
112     stdscr.hline(1, 0, curses.ACS_HLINE, 23)
113     stdscr.addstr(2, 0,
114                   "Welcome to lightcards. Here are some keybindings to get\n" +
115                   "you started.\n\n" +
116                   "h, left            previous card\n" +
117                   "l, right           next card\n" +
118                   "j, k, up, down     flip card\n" +
119                   "i, /               star card\n" +
120                   "0, ^, home         go to start of deck\n" +
121                   "$, end             go to end of deck\n" +
122                   "H, ?               open this screen\n\n" +
123                   "More information can be found in the man page.\n" +
124                   "Press [q], [H], or [?] to go back.")
125     while True:
126         key = stdscr.getkey()
127         if key in ["q", "H", "?"]:
128             get_key(stdscr, stack, headers, idx)
129
130
131 def init_disp(stdscr, stack, headers, idx):
132     curses.curs_set(0)
133     curses.init_pair(1, curses.COLOR_CYAN, 0)
134     curses.init_pair(2, curses.COLOR_RED, 0)
135     get_key(stdscr, stack, headers, idx)
136
137 def get_key(stdscr, stack, headers, idx):
138     disp_card(stdscr, stack, headers, idx)
139     while True:
140         key = stdscr.getkey()
141         if key == "q":
142             sys.exit(0)
143         elif key in ["l", "KEY_LEFT"]:
144             idx.forward(stack)
145             idx.setSide(0)
146             disp_card(stdscr, stack, headers, idx)
147         elif key in ["h", "KEY_RIGHT"]:
148             idx.back()
149             idx.setSide(0)
150             disp_card(stdscr, stack, headers,  idx)
151         elif key in ["j", "k", "KEY_UP", "KEY_DOWN"]:
152             idx.flip()
153             disp_card(stdscr, stack, headers, idx)
154         elif key in ["i", "/"]:
155             stack[idx.getIdx()].toggleStar()
156             disp_card(stdscr, stack, headers, idx)
157         elif key in ["0", "^", "KEY_HOME"]:
158             idx.setIdx(0)
159             idx.setSide(0)
160             disp_card(stdscr, stack, headers, idx)
161         elif key in ["$", "KEY_END"]:
162             idx.setIdx(len(stack) - 1)
163             idx.setSide(0)
164             disp_card(stdscr, stack, headers, idx)
165         elif key in ["H", "?"]:
166             disp_help(stdscr, stack, headers, idx)