-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcmdline.py
72 lines (59 loc) · 1.15 KB
/
cmdline.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
import curses
def init():
global stdscr
stdscr = curses.initscr()
curses.cbreak()
curses.noecho()
size = stdscr.getmaxyx()
global outwin, cmdwin, cmdstr, lines
outwin = curses.newwin(size[0]-1, size[1], 0, 0)
cmdwin = curses.newwin(1, size[1], size[0]-1, 0)
cmdwin.keypad(1)
cmdwin.nodelay(1)
cmdstr = ""
lines = []
def deinit():
global cmdwin
cmdwin.nodelay(0)
cmdwin.keypad(0)
curses.echo()
curses.nocbreak()
curses.endwin()
def addline(text):
global outwin, lines
x = str(text).strip()
while len(x) > outwin.getmaxyx()[1]-1:
y = x
x = y[:outwin.getmaxyx()[1]-1]
lines.append(x)
x = y[len(x):]
if len(x) > 0:
lines.append(x)
while len(lines) > outwin.getmaxyx()[0]:
lines.pop(0)
try:
outwin.move(0, 0)
for i in lines:
outwin.clrtoeol()
i = i.strip()
outwin.addstr(i + "\n")
except curses.error:
pass
outwin.noutrefresh()
def handle():
global cmdwin, cmdstr
cmdwin.timeout(0)
c = cmdwin.getch()
if c > -1:
if c == 10:
addline(cmdstr)
cmdwin.clear()
cmdstr = ""
else:
try:
cmdwin.addch(c)
cmdstr += chr(c)
except curses.error:
pass
cmdwin.noutrefresh()
curses.doupdate()