]> git.armaanb.net Git - lightcards.git/blob - lightcards/display.py
Fix style per the suggestions of Flake8
[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
8 class Status():
9     index = 0
10     side = 0
11
12     def forward(self, stack):
13         if not self.index == len(stack) - 1:
14             self.index += 1
15
16     def back(self):
17         if not self.index < 1:
18             self.index -= 1
19
20     def flip(self):
21         if self.side == 0:
22             self.side = 1
23         else:
24             self.side = 0
25
26     def setSide(self, inp):
27         self.side = inp
28
29     def getSide(self):
30         return self.side
31
32     def getIdx(self):
33         return self.index
34
35
36 def disp_card(stdscr, stack, headers, obj):
37     stdscr.clear()
38     side_title = headers[obj.getSide()]
39     stdscr.addstr(side_title + "\n")
40     for i in range(len(side_title)):
41         stdscr.addstr("=")
42     stdscr.addstr("\n" + str(stack[obj.getIdx()][obj.getSide()]))
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 Exception:
58         pass
59
60
61 def get_key(stdscr, stack, headers):
62     idx = Status()
63     curses.curs_set(0)
64     disp_card(stdscr, stack, headers, idx)
65
66     while True:
67         key = stdscr.getkey()
68         try:
69             if key == "q" or key == os.linesep:
70                 exit(0)
71             elif key in ["l", "KEY_LEFT"]:
72                 idx.forward(stack)
73                 idx.setSide(0)
74                 disp_card(stdscr, stack, headers, idx)
75             elif key in ["h", "KEY_RIGHT"]:
76                 idx.back()
77                 idx.setSide(0)
78                 disp_card(stdscr, stack, headers,  idx)
79             elif key in ["j", "k", "KEY_UP", "KEY_DOWN"]:
80                 idx.flip()
81                 disp_card(stdscr, stack, headers, idx)
82         except Exception:
83             pass