]> git.armaanb.net Git - lightcards.git/blob - lightcards/display.py
Move Status class into new file
[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_card(stdscr, stack, headers, obj):
11     stdscr.clear()
12     side_title = headers[obj.getSide()]
13     stdscr.addstr(side_title + "\n")
14     for i in range(len(side_title)):
15         stdscr.addstr("=")
16     stdscr.addstr("\n" + str(stack[obj.getIdx()][obj.getSide()]))
17
18     (mlines, mcols) = stdscr.getmaxyx()
19     mlines -= 1
20     mcols -= 5
21
22     try:
23         stdscr.insch(mlines, mcols, str(obj.getIdx() + 1))
24         stdscr.insch(mlines, mcols+1, '-')
25         if obj.getSide() == 0:
26             stdscr.insch(mlines, mcols+2, '1')
27         else:
28             stdscr.insch(mlines, mcols+2, '2')
29         stdscr.insch(mlines, mcols+3, '/')
30         stdscr.insch(mlines, mcols+4, str(len(stack)))
31     except Exception:
32         pass
33
34
35 def get_key(stdscr, stack, headers):
36     idx = Status()
37     curses.curs_set(0)
38     disp_card(stdscr, stack, headers, idx)
39
40     while True:
41         key = stdscr.getkey()
42         try:
43             if key == "q" or key == os.linesep:
44                 exit(0)
45             elif key in ["l", "KEY_LEFT"]:
46                 idx.forward(stack)
47                 idx.setSide(0)
48                 disp_card(stdscr, stack, headers, idx)
49             elif key in ["h", "KEY_RIGHT"]:
50                 idx.back()
51                 idx.setSide(0)
52                 disp_card(stdscr, stack, headers,  idx)
53             elif key in ["j", "k", "KEY_UP", "KEY_DOWN"]:
54                 idx.flip()
55                 disp_card(stdscr, stack, headers, idx)
56         except Exception:
57             pass