forked from joshdentremont/fallout-terminal
-
Notifications
You must be signed in to change notification settings - Fork 2
/
fallout_functions.py
69 lines (59 loc) · 1.75 KB
/
fallout_functions.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
import curses
import time
import sys
LETTER_PAUSE = 5
INPUT_PAUSE = 500 # ms
TYPE_DELAY = 40
HIDDEN_MASK = '*'
NEWLINE = 10
DELETE = 127
def slowWrite(window, text, pause = LETTER_PAUSE):
"""
wrapper for curses.addstr() which writes the text slowely
"""
for i in range(len(text)):
window.addstr(text[i])
window.refresh()
curses.napms(pause)
def upperInput(window, hidden = False, can_newline = True):
"""
Reads user input until enter key is pressed. Echoes the input in upper case
hidden - if true the output will be masked
can_newline - if true the input is followed by a newline and the screen is
scrolled if necessary
"""
inchar = 0
instr = ''
while inchar != NEWLINE:
inchar = window.getch()
# convert lower case to upper
if inchar > 96 and inchar < 123:
inchar -= 32
# deal with backspace
if inchar == DELETE:
if len(instr) > 0:
instr = instr[:-1]
cur = window.getyx()
window.move(cur[0], cur[1] - 1)
window.clrtobot()
else:
continue
elif inchar > 255:
continue
# output the character
elif inchar != NEWLINE:
instr += chr(inchar)
if hidden:
window.addch(HIDDEN_MASK)
else:
window.addch(inchar)
elif can_newline:
window.addch(NEWLINE)
return instr
def centeredWrite(window, text, pause = LETTER_PAUSE):
"""
Writes to the current line but centers the text
"""
width = window.getmaxyx()[1]
window.move(window.getyx()[0], width // 2 - len(text) // 2)
slowWrite(window, text, pause)