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