]> git.armaanb.net Git - lightcards.git/blob - lightcards/display.py
Add keybindings to go to start and end of stack
[lightcards.git] / lightcards / display.py
1 # Display card output and retreive input
2 # Armaan Bhojwani 2021
3
4 import curses
5
6
7 def disp_bar(stdscr, stack, headers, obj):
8     (mlines, mcols) = stdscr.getmaxyx()
9     if len(stack) <= 1:
10         percent = "100"
11     else:
12         percent = str(round(obj.getIdx() / (len(stack) - 1) * 100)).zfill(3)
13
14     stdscr.insstr(mlines - 1, 0,
15                   "[" +
16                   stack[obj.getIdx()].printStar() +
17                   "] [" +
18                   percent +
19                   "% (" +
20                   str(obj.getIdx() + 1) +
21                   "/" +
22                   str(len(stack)) +
23                   ")]" +
24                   " [" +
25                   headers[obj.getSide()] +
26                   "]")
27
28
29 def disp_menu(stdscr, stack, headers, idx):
30     stdscr.addstr("Good job, you've completed a round!\n\n" +
31                   "Choose one of the following options:\n" +
32                   "[r]estart, [s]tarred only, [u]nstar all and restart, [q]uit")
33     while True:
34         key = stdscr.getkey()
35         if key == "q":
36             exit(0)
37         elif key == "r":
38             idx.setIdx(0)
39             get_key(stdscr, stack, headers, idx)
40         elif key == "u":
41             idx.setIdx(0)
42             for x in stack:
43                 x.unStar()
44             get_key(stdscr, stack, headers, idx)
45         elif key == "s":
46             cont = False
47             for x in stack:
48                 if x.getStar():
49                     cont = True
50
51             if cont:
52                 idx.setIdx(0)
53                 stack = [x for x in stack if x.getStar()]
54                 get_key(stdscr, stack, headers, idx)
55             else:
56                 stdscr.clear()
57                 stdscr.addstr("ERR: Stack empty. Choose another option\n\n")
58                 disp_menu(stdscr, stack, headers, idx)
59
60
61 def disp_card(stdscr, stack, headers, obj):
62     stdscr.clear()
63     if obj.getIdx() == len(stack):
64         disp_menu(stdscr, stack, headers, obj)
65     else:
66         if obj.getSide() == 0:
67             top = headers[obj.getSide()] + " " + str(obj.getIdx() + 1) + "\n"
68         else:
69             top = headers[obj.getSide()] + " " + str(obj.getIdx() + 1) + \
70                 "; " + str(stack[obj.getIdx()][0]) + "\n"
71
72         stdscr.addstr(top)
73         for i in range(len(top)):
74             stdscr.addch("=")
75         stdscr.addstr("\n" + str(stack[obj.getIdx()][obj.getSide()]))
76     disp_bar(stdscr, stack, headers, obj)
77
78
79 def get_key(stdscr, stack, headers, idx):
80     curses.curs_set(0)
81     disp_card(stdscr, stack, headers, idx)
82
83     while True:
84         key = stdscr.getkey()
85         if key == "q":
86             exit(0)
87         elif key in ["l", "KEY_LEFT"]:
88             idx.forward(stack)
89             idx.setSide(0)
90             disp_card(stdscr, stack, headers, idx)
91         elif key in ["h", "KEY_RIGHT"]:
92             idx.back()
93             idx.setSide(0)
94             disp_card(stdscr, stack, headers,  idx)
95         elif key in ["j", "k", "KEY_UP", "KEY_DOWN"]:
96             idx.flip()
97             disp_card(stdscr, stack, headers, idx)
98         elif key in ["i", "/"]:
99             stack[idx.getIdx()].toggleStar()
100             disp_card(stdscr, stack, headers, idx)
101         elif key in ["0", "^", "KEY_HOME"]:
102             idx.setIdx(0)
103             disp_card(stdscr, stack, headers, idx)
104         elif key in ["$", "KEY_END"]:
105             idx.setIdx(len(stack) - 1)
106             disp_card(stdscr, stack, headers, idx)