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