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