]> git.armaanb.net Git - lightcards.git/blob - lightcards/display.py
Fix calculation of progress
[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         side_title = headers[obj.getSide()]
67         stdscr.addstr(side_title + "\n")
68         for i in range(len(side_title)):
69             stdscr.addch("=")
70         stdscr.addstr("\n" + str(stack[obj.getIdx()][obj.getSide()]))
71     disp_bar(stdscr, stack, headers, obj)
72
73
74 def get_key(stdscr, stack, headers, idx):
75     curses.curs_set(0)
76     disp_card(stdscr, stack, headers, idx)
77
78     while True:
79         key = stdscr.getkey()
80         if key == "q":
81             exit(0)
82         elif key in ["l", "KEY_LEFT"]:
83             idx.forward(stack)
84             idx.setSide(0)
85             disp_card(stdscr, stack, headers, idx)
86         elif key in ["h", "KEY_RIGHT"]:
87             idx.back()
88             idx.setSide(0)
89             disp_card(stdscr, stack, headers,  idx)
90         elif key in ["j", "k", "KEY_UP", "KEY_DOWN"]:
91             idx.flip()
92             disp_card(stdscr, stack, headers, idx)
93         elif key in ["i", "/"]:
94             stack[idx.getIdx()].toggleStar()
95             disp_card(stdscr, stack, headers, idx)