-
Notifications
You must be signed in to change notification settings - Fork 4
/
nc.py
151 lines (117 loc) · 3.73 KB
/
nc.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import os
import struct
from curses import wrapper, newwin, resize_term, beep, flash
import curses
from time import sleep
import fcntl
import termios
import sys
def get_term_size():
# taken from http://dag.wieers.com/home-made/dstat/
try:
h, w = int(os.environ["LINES"]), int(os.environ["COLUMNS"])
except KeyError:
try:
s = struct.pack('HHHH', 0, 0, 0, 0)
x = fcntl.ioctl(sys.stdout.fileno(), termios.TIOCGWINSZ,
s)
h, w = struct.unpack('HHHH', x)[:2]
except:
h, w = 25, 80
return h, w
def check_size():
h, w = get_term_size()
while h < 40 or w < 162:
print(chr(27) + "[2J")
print("Resize the window until 50x162")
h, w = get_term_size()
print("Current window size: {}x{}".format(h,w))
sleep(0.2)
def init_ui():
check_size()
begin_x = 1
begin_y = 1
height = 40
width = 160
left_win_width = 28
middle_win_width = 80
right_win_width = width - left_win_width - middle_win_width - 4
win = newwin(height, width, begin_y, begin_x)
win.border(0, 0, 0, 0, 0, 0, 0, 0)
win.addstr("So You Have an Idea")
win.refresh()
left_win = win.subwin(height-5, left_win_width, 2, 2)
left_win.border(0, 0, 0, 0, 0, 0, 0, 0)
left_win.refresh()
left_win = left_win.derwin(height-7, left_win_width-2, 1, 1)
middle_win = win.subwin(height-5, middle_win_width, 2, left_win_width+2)
middle_win.border(0, 0, 0, 0, 0, 0, 0, 0)
for x in range(6, 20, 2):
middle_win.addstr(x, middle_win_width-1, ">")
middle_win.refresh()
middle_win = middle_win.derwin(height-7, middle_win_width-2, 1, 1)
right_win = win.subwin(height-5, right_win_width, 2, left_win_width + middle_win_width + 2)
right_win.border(0, 0, 0, 0, 0, 0, 0, 0)
for x in range(7, 21, 2):
right_win.addstr(x, 0, "<")
right_win.refresh()
right_win = right_win.derwin(height-7, right_win_width-2, 1, 1)
bottom_win = win.subwin(3, width-4, height-3, 2)
bottom_win.border(0, 0, 0, 0, 0, 0, 0, 0)
bottom_win.refresh()
bottom_win = bottom_win.derwin(1, 114, 1, 1)
left_win.refresh()
middle_win.refresh()
right_win.refresh()
bottom_win.refresh()
curses.init_pair(1, curses.COLOR_RED, curses.COLOR_BLACK)
curses.init_pair(2, curses.COLOR_GREEN, curses.COLOR_BLACK)
curses.init_pair(3, curses.COLOR_BLUE, curses.COLOR_BLACK)
curses.init_pair(4, curses.COLOR_CYAN, curses.COLOR_BLACK)
curses.init_pair(5, curses.COLOR_MAGENTA, curses.COLOR_BLACK)
return left_win, middle_win, right_win, bottom_win, win
def printw(window, text="", end="\n", color=0):
if not isinstance(text, str):
text = repr(text)
window.addstr(text + end, curses.color_pair(color))
window.refresh()
def alert(window, text, wait=True):
y, x = window.getmaxyx()
w, h = 50, 6
alertb = window.subwin(h, w, int(y/2-h/2), int(x/2-w/2))
alertb.border(0,0,0,0,0,0,0,0)
alertb.refresh()
alert = alertb.derwin(h-2, w-2, 1, 1)
alert.addstr(text)
alert.refresh()
beep()
flash()
if wait:
alert.getch()
alert.erase()
alertb.erase()
return alert, alertb
def getstr(window, empty_ok=False):
curses.curs_set(1)
curses.echo()
window.clear()
window.move(0, 0)
if empty_ok:
response = window.getstr()
else:
response = b''
while response == b'':
response = window.getstr()
window.clear()
window.refresh()
window.move(0, 0)
curses.curs_set(0)
return response
def clear(window):
window.clear()
window.refresh()
def main(stdscr):
stdscr.clear()
curses.start_color()
stdscr.refresh()
wrapper(main)