]> git.armaanb.net Git - lightcards.git/blob - lightcards/display.py
Don't print more header separators than can fit
[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
7
8 def disp_bar(stdscr, stack, headers, obj):
9     (mlines, mcols) = stdscr.getmaxyx()
10     if len(stack) <= 1:
11         percent = "100"
12     else:
13         percent = str(round(obj.getIdx() / (len(stack) - 1) * 100)).zfill(3)
14
15     stdscr.insstr(mlines - 1, 0,
16                   "[" +
17                   stack[obj.getIdx()].printStar() +
18                   "] [" +
19                   percent +
20                   "% (" +
21                   str(obj.getIdx() + 1) +
22                   "/" +
23                   str(len(stack)) +
24                   ")]" +
25                   " [" +
26                   headers[obj.getSide()] +
27                   "]")
28
29
30 def disp_menu(stdscr, stack, headers, idx):
31     stdscr.addstr("Good job, you've completed a round!\n\n" +
32                   "Choose one of the following options:\n" +
33                   "[r]: restart\n" +
34                   "[s]: restart with starred only\n" +
35                   "[u]: restart and unstar all\n" +
36                   "[z]: restart and shuffle cards\n" +
37                   "[q]: quit")
38     while True:
39         key = stdscr.getkey()
40         if key == "q":
41             exit(0)
42         elif key == "r":
43             idx.setIdx(0)
44             get_key(stdscr, stack, headers, idx)
45         elif key == "u":
46             idx.setIdx(0)
47             for x in stack:
48                 x.unStar()
49             get_key(stdscr, stack, headers, idx)
50         elif key == "s":
51             cont = False
52             for x in stack:
53                 if x.getStar():
54                     cont = True
55
56             if cont:
57                 idx.setIdx(0)
58                 stack = [x for x in stack if x.getStar()]
59                 get_key(stdscr, stack, headers, idx)
60             else:
61                 stdscr.clear()
62                 stdscr.addstr("ERR: Stack empty. Choose another option\n\n")
63                 disp_menu(stdscr, stack, headers, idx)
64         elif key == "z":
65             idx.setIdx(0)
66             shuffle(stack)
67             get_key(stdscr, stack, headers, idx)
68
69
70 def disp_card(stdscr, stack, headers, obj):
71     stdscr.clear()
72     if obj.getIdx() == len(stack):
73         disp_menu(stdscr, stack, headers, obj)
74     else:
75         if obj.getSide() == 0:
76             top = headers[obj.getSide()] + " " + str(obj.getIdx() + 1) + "\n"
77         else:
78             top = headers[obj.getSide()] + " " + str(obj.getIdx() + 1) + \
79                 "; " + str(stack[obj.getIdx()][0]) + "\n"
80
81         stdscr.addstr(top)
82         (mlines, mcols) = stdscr.getmaxyx()
83         if len(top) < mcols:
84             mcols = len(top)
85         for i in range(mcols):
86             stdscr.addch("=")
87
88         stdscr.addstr("\n" + str(stack[obj.getIdx()][obj.getSide()]))
89     disp_bar(stdscr, stack, headers, obj)
90
91
92 def get_key(stdscr, stack, headers, idx):
93     curses.curs_set(0)
94     disp_card(stdscr, stack, headers, idx)
95
96     while True:
97         key = stdscr.getkey()
98         if key == "q":
99             exit(0)
100         elif key in ["l", "KEY_LEFT"]:
101             idx.forward(stack)
102             idx.setSide(0)
103             disp_card(stdscr, stack, headers, idx)
104         elif key in ["h", "KEY_RIGHT"]:
105             idx.back()
106             idx.setSide(0)
107             disp_card(stdscr, stack, headers,  idx)
108         elif key in ["j", "k", "KEY_UP", "KEY_DOWN"]:
109             idx.flip()
110             disp_card(stdscr, stack, headers, idx)
111         elif key in ["i", "/"]:
112             stack[idx.getIdx()].toggleStar()
113             disp_card(stdscr, stack, headers, idx)
114         elif key in ["0", "^", "KEY_HOME"]:
115             idx.setIdx(0)
116             disp_card(stdscr, stack, headers, idx)
117         elif key in ["$", "KEY_END"]:
118             idx.setIdx(len(stack) - 1)
119             disp_card(stdscr, stack, headers, idx)