]> git.armaanb.net Git - lightcards.git/blob - lightcards/display.py
Remove clear screen before displaying menu
[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     curses.init_pair(1, curses.COLOR_CYAN, 0)
18     stdscr.insstr(mlines - 1, 0,
19                   "[" +
20                   stack[obj.getIdx()].printStar() +
21                   "] [" +
22                   percent +
23                   "% (" +
24                   str(obj.getIdx() + 1) +
25                   "/" +
26                   str(len(stack)) +
27                   ")]" +
28                   " [" +
29                   headers[obj.getSide()] +
30                   "]", curses.color_pair(1))
31
32
33 def disp_menu(stdscr, stack, headers, idx):
34     curses.init_pair(1, curses.COLOR_CYAN, 0)
35     curses.init_pair(2, curses.COLOR_RED, 0)
36     stdscr.addstr("Good job, you've completed a round!\n\n",
37                   curses.color_pair(1))
38     stdscr.addstr("Choose one of the following options:\n" +
39                   "[r]: restart\n" +
40                   "[s]: restart with starred only\n" +
41                   "[u]: restart and unstar all\n" +
42                   "[z]: restart and shuffle cards\n" +
43                   "[q]: quit")
44     while True:
45         key = stdscr.getkey()
46         if key == "q":
47             sys.exit(0)
48         elif key == "r":
49             idx.setIdx(0)
50             get_key(stdscr, stack, headers, idx)
51         elif key == "u":
52             idx.setIdx(0)
53             for x in stack:
54                 x.unStar()
55             get_key(stdscr, stack, headers, idx)
56         elif key == "s":
57             cont = False
58             for x in stack:
59                 if x.getStar():
60                     cont = True
61
62             if cont:
63                 idx.setIdx(0)
64                 stack = [x for x in stack if x.getStar()]
65                 get_key(stdscr, stack, headers, idx)
66             else:
67                 stdscr.clear()
68                 stdscr.addstr("ERR: Stack empty. Choose another option\n\n",
69                               curses.color_pair(2))
70                 disp_menu(stdscr, stack, headers, idx)
71         elif key == "z":
72             idx.setIdx(0)
73             shuffle(stack)
74             get_key(stdscr, stack, headers, idx)
75
76
77 def disp_card(stdscr, stack, headers, obj):
78     stdscr.clear()
79     (mlines, mcols) = stdscr.getmaxyx()
80     if obj.getIdx() == len(stack):
81         disp_menu(stdscr, stack, headers, obj)
82     else:
83         if obj.getSide() == 0:
84             top = headers[obj.getSide()] + "; " + str(obj.getIdx() + 1)
85         else:
86             top = headers[obj.getSide()] + "; " + str(obj.getIdx() + 1) + \
87                 "; " + str(stack[obj.getIdx()][0])
88
89         header_width = mcols
90         if mcols > 80:
91             header_width = 80
92
93         stdscr.addstr(textwrap.shorten(top, width=header_width,
94                                        placeholder="…"), curses.A_BOLD)
95
96         # Add horizontal line
97         lin_width = mcols
98         if len(top) < mcols:
99             lin_width = len(top)
100         stdscr.hline(1, 0, curses.ACS_HLINE, lin_width)
101
102         # Show current side
103         wrap_width = mcols
104         if mcols > 80:
105             wrap_width = 80
106         stdscr.hline(curses.ACS_HLINE, lin_width)
107         stdscr.addstr(2, 0, textwrap.fill(stack[obj.getIdx()][obj.getSide()],
108                                           width=wrap_width))
109     disp_bar(stdscr, stack, headers, obj)
110
111
112 def get_key(stdscr, stack, headers, idx):
113     curses.curs_set(0)
114     disp_card(stdscr, stack, headers, idx)
115
116     while True:
117         key = stdscr.getkey()
118         if key == "q":
119             sys.exit(0)
120         elif key in ["l", "KEY_LEFT"]:
121             idx.forward(stack)
122             idx.setSide(0)
123             disp_card(stdscr, stack, headers, idx)
124         elif key in ["h", "KEY_RIGHT"]:
125             idx.back()
126             idx.setSide(0)
127             disp_card(stdscr, stack, headers,  idx)
128         elif key in ["j", "k", "KEY_UP", "KEY_DOWN"]:
129             idx.flip()
130             disp_card(stdscr, stack, headers, idx)
131         elif key in ["i", "/"]:
132             stack[idx.getIdx()].toggleStar()
133             disp_card(stdscr, stack, headers, idx)
134         elif key in ["0", "^", "KEY_HOME"]:
135             idx.setIdx(0)
136             disp_card(stdscr, stack, headers, idx)
137         elif key in ["$", "KEY_END"]:
138             idx.setIdx(len(stack) - 1)
139             disp_card(stdscr, stack, headers, idx)