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