]> git.armaanb.net Git - lightcards.git/blob - lightcards/display.py
Clean up code
[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
8
9 def disp_bar(stdscr, stack, headers, obj):
10     (mlines, mcols) = stdscr.getmaxyx()
11     if len(stack) <= 1:
12         percent = "100"
13     else:
14         percent = str(round(obj.getIdx() / (len(stack) - 1) * 100)).zfill(3)
15
16     curses.init_pair(1, curses.COLOR_CYAN, 0)
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                   "]", curses.color_pair(1))
30
31
32 def disp_menu(stdscr, stack, headers, idx):
33     stdscr.clear()
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     if obj.getIdx() == len(stack):
80         disp_menu(stdscr, stack, headers, obj)
81     else:
82         if obj.getSide() == 0:
83             top = headers[obj.getSide()] + "; " + str(obj.getIdx() + 1) + "\n"
84         else:
85             top = headers[obj.getSide()] + "; " + str(obj.getIdx() + 1) + \
86                 "; " + str(stack[obj.getIdx()][0]) + "\n"
87
88         stdscr.addstr(top, curses.A_BOLD)
89         (mlines, mcols) = stdscr.getmaxyx()
90         if len(top) < mcols:
91             mcols = len(top)
92         for i in range(mcols):
93             stdscr.addch("=")
94
95         stdscr.addstr("\n" + str(stack[obj.getIdx()][obj.getSide()]))
96     disp_bar(stdscr, stack, headers, obj)
97
98
99 def get_key(stdscr, stack, headers, idx):
100     curses.curs_set(0)
101     disp_card(stdscr, stack, headers, idx)
102
103     while True:
104         key = stdscr.getkey()
105         if key == "q":
106             sys.exit(0)
107         elif key in ["l", "KEY_LEFT"]:
108             idx.forward(stack)
109             idx.setSide(0)
110             disp_card(stdscr, stack, headers, idx)
111         elif key in ["h", "KEY_RIGHT"]:
112             idx.back()
113             idx.setSide(0)
114             disp_card(stdscr, stack, headers,  idx)
115         elif key in ["j", "k", "KEY_UP", "KEY_DOWN"]:
116             idx.flip()
117             disp_card(stdscr, stack, headers, idx)
118         elif key in ["i", "/"]:
119             stack[idx.getIdx()].toggleStar()
120             disp_card(stdscr, stack, headers, idx)
121         elif key in ["0", "^", "KEY_HOME"]:
122             idx.setIdx(0)
123             disp_card(stdscr, stack, headers, idx)
124         elif key in ["$", "KEY_END"]:
125             idx.setIdx(len(stack) - 1)
126             disp_card(stdscr, stack, headers, idx)