-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtui.py
124 lines (110 loc) · 4.22 KB
/
tui.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
from os import path
import time
from threading import Thread
import curses
from curses import ERR
from requests.exceptions import HTTPError
from pythonwars.pythonwars import CodeWars
class Tui:
'''TUI constructors and interaction class'''
def __init__(self):
self.code_wars = None
self.scr_size = None
def main(self, curse_main):
'''inits curses, sets some params, and begins
calling functions to get the show on the road.'''
self.scr_size = ((curse_main.getmaxyx()[0] // 2),
(curse_main.getmaxyx()[1] // 2))
curses.cbreak()
self.login_screen(curse_main)
self.auth_api(curse_main)
def login_screen(self, login_scr):
login_scr.addstr('-'*int(self.scr_size[1] - 11) +
'Welcome to CodeWars!' +
'-'*int(self.scr_size[1] - 10))
login_scr.refresh()
time.sleep(1)
login_scr.addstr(int(self.scr_size[0] + 5),
int(self.scr_size[1] - 14),
'Please enter your API token:')
login_scr.refresh()
curses.echo()
time.sleep(1)
if self.scr_size[0] > 28:
logo_thrd = Thread(self.logo_print(login_scr))
logo_thrd.start()
user_api = ''
getch_position = [0, 0]
input_scr = curses.newwin(1,21, int(self.scr_size[0] + 6),
int(self.scr_size[1] - 10))
input_scr.keypad(True)
input_scr.nodelay(True)
while True:
in_char = input_scr.getch(*getch_position)
try:
if int(in_char) == -1:
continue
except TypeError:
pass
else:
if in_char == curses.KEY_BACKSPACE:
if not user_api:
pass
else:
user_api = user_api[:-1]
getch_position[1] -= 1
input_scr.delch(*getch_position)
else:
user_api += chr(in_char)
getch_position[1] += 1
if len(user_api) == 20:
break
input_scr.nodelay(False)
login_scr.keypad(False)
login_scr.addstr(int(self.scr_size[0] + 8),
int(self.scr_size[1] - 12),
'Press enter to confirm.')
login_scr.getch()
login_scr.addstr(int(self.scr_size[0] + 10),
int(self.scr_size[1] - 9),
'Creating session.')
login_scr.refresh()
self.code_wars = CodeWars(user_api)
login_scr.addstr(int(self.scr_size[0] + 11),
int(self.scr_size[1] - 8),
'Session active.')
login_scr.refresh()
login_scr.clear()
login_scr.refresh()
def logo_print(self, scr):
with open(path.join('ascii_art', 'logo.txt')) as logo_file:
logo_count = int(self.scr_size[0] - 27)
for line in logo_file:
scr.addstr(logo_count, int(self.scr_size[1] - 30), line)
logo_count += 1
scr.refresh()
time.sleep(0.05)
def auth_api(self, auth_scr):
auth_scr.addstr(int(self.scr_size[0] - 8),
int(self.scr_size[1] - 15),
'Authenticating your API token.')
auth_scr.refresh()
try:
a = self.code_wars.train_next_code_challenge('python', peek=True)
except HTTPError:
auth_scr.addstr(int(self.scr_size[0] - 7),
int(self.scr_size[1] - 14),
'Authentication failed. Gtfo.')
auth_scr.addstr(int(self.scr_size[0] - 4),
int(self.scr_size[1] - 13),
'Press the Any key to exit.')
auth_scr.refresh()
auth_scr.getkey()
else:
auth_scr.addstr(int(self.scr_size[0] - 7),
int(self.scr_size[1] - 7),
'Authenticated.')
auth_scr.refresh()
time.sleep(3)
tui = Tui()
curses.wrapper(tui.main)