]> git.armaanb.net Git - lightcards.git/blob - lightcards/display.py
Reconfigure keybindings and add arrow key support
[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 class Status():
8     index = 0
9     side = 0
10
11     def forward(self, stack):
12         if not self.index == len(stack) - 1:
13             self.index += 1
14
15     def back(self):
16         if not self.index < 1:
17             self.index -= 1
18
19     def flip(self):
20         if self.side == 0:
21             self.side = 1
22         else:
23             self.side = 0
24
25     def setSide(self, inp):
26         self.side = inp
27
28     def getSide(self):
29         return self.side
30
31     def getIdx(self):
32         return self.index
33
34
35 def disp_card(stdscr, stack, headers, obj):
36     stdscr.clear()
37     side_title = headers[obj.getSide()]
38     stdscr.addstr(side_title + "\n")
39     for i in range(len(side_title)):
40         stdscr.addstr("=")
41     stdscr.addstr("\n" + str(stack[obj.getIdx()][obj.getSide()]))
42
43
44     (mlines, mcols) = stdscr.getmaxyx()
45     mlines -= 1
46     mcols -= 5
47
48     try:
49         stdscr.insch(mlines, mcols, str(obj.getIdx() + 1))
50         stdscr.insch(mlines, mcols+1, '-')
51         if obj.getSide() == 0:
52             stdscr.insch(mlines, mcols+2, '1')
53         else:
54             stdscr.insch(mlines, mcols+2, '2')
55         stdscr.insch(mlines, mcols+3, '/')
56         stdscr.insch(mlines, mcols+4, str(len(stack)))
57     except:
58         pass
59
60 def get_key(stdscr, stack, headers):
61     idx = Status()
62     curses.curs_set(0)
63     disp_card(stdscr, stack, headers, idx)
64
65     while True:
66         key = stdscr.getkey()
67         try:
68             if key == "q" or key == os.linesep:
69                 exit(0)
70             elif key in ["l", "KEY_LEFT"]:
71                 idx.forward(stack)
72                 idx.setSide(0)
73                 disp_card(stdscr, stack, headers, idx)
74             elif key in ["h", "KEY_RIGHT"]:
75                 idx.back()
76                 idx.setSide(0)
77                 disp_card(stdscr, stack, headers,  idx)
78             elif key in ["j", "k", "KEY_UP", "KEY_DOWN"]:
79                 idx.flip()
80                 disp_card(stdscr, stack, headers, idx)
81         except Exception:
82             pass