-
Notifications
You must be signed in to change notification settings - Fork 0
/
pcedit.py
128 lines (104 loc) · 3.53 KB
/
pcedit.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
import curses
import curses.textpad
import wrapper as my
QUIT_KEY = 'q'
HORIZONTAL_LINE_CHAR = '\u2500'
TOP_RIGHT_CORNER_CHAR = '\u256E'
VERTIACL_LINE_CHAR = '\u2502'
BOTTOM_RIGHT_CORNER_CHAR = '\u2518'
BOTTOM_LEFT_CORNER_CHAR = '\u2514'
TOP_LEF_CORNER_CHAR = '\u250C'
class Editor:
def __init__(self, stdscr):
self.k = 0
self.oy = 0
self.ox = 0
self.stdscr = stdscr
self.sub = None
self.help_box_dimensions = None
def refresh_hook(self):
self.draw_help_box(5, 120, 4, 10)
self.print_to_help_box('q - quit')
self.stdscr.original_refresh()
def restore_cursor_position(self):
self.move_cursor_absolute(self.oy, self.ox)
def save_cursor_position(self):
self.oy, self.ox = self.stdscr.getyx()
def print_to_help_box(self, msg):
if self.help_box_dimensions:
self.move_cursor_absolute(self.help_box_dimensions.get('y') + 1, self.help_box_dimensions.get('x') + 1)
self.stdscr.addstr(msg)
self.restore_cursor_position()
def move_cursor_absolute(self, y, x):
self.save_cursor_position()
self.stdscr.move(y, x)
def move_cursor_relative(self, y, x):
self.save_cursor_position()
self.stdscr.move(self.oy + y, self.ox + x)
def repeat_horizontal_char(self, ch, no):
for i in range(no):
self.print_char(ch)
def repeat_vertical_char(self, ch, no):
oy, ox = self.stdscr.getyx()
cy, cx = oy + 1, ox - 1
for i in range(no):
self.move_cursor_absolute(cy, cx)
self.print_char(ch)
cy += 1
def draw_help_box(self, y, x, h, w):
self.help_box_dimensions = {
'y': y,
'x': x,
'h': h,
'w': w,
}
self.move_cursor_absolute(y, x)
self.repeat_horizontal_char(HORIZONTAL_LINE_CHAR, w)
self.stdscr.addch(TOP_RIGHT_CORNER_CHAR)
self.repeat_vertical_char(VERTIACL_LINE_CHAR, h)
self.move_cursor_relative(1, -1)
self.stdscr.addch(BOTTOM_RIGHT_CORNER_CHAR)
self.move_cursor_absolute(y + h + 1, x)
self.repeat_horizontal_char(HORIZONTAL_LINE_CHAR, w)
self.move_cursor_relative(0, -w - 1)
self.stdscr.addch(BOTTOM_LEFT_CORNER_CHAR)
self.move_cursor_relative(-h - 1, 0)
self.repeat_vertical_char(VERTIACL_LINE_CHAR, h)
self.move_cursor_relative(-h, -1)
self.stdscr.addch(TOP_LEF_CORNER_CHAR)
self.move_cursor_absolute(0, 0)
def read_char(self):
return self.stdscr.getch()
def initialize_cursor(self):
curses.noecho()
curses.curs_set(1)
curses.cbreak()
self.stdscr.keypad(True)
def x_pressed(self):
return self.k == ord(QUIT_KEY)
def print_at_bottom(self, v):
self.save_cursor_position()
y, x = self.stdscr.getmaxyx()
self.move_cursor_absolute(y - 1, 0)
self.stdscr.addch(v)
self.restore_cursor_position()
def print_char(self, ch):
self.stdscr.addch(ch)
def main_loop(self):
self.draw_help_box(5, 120, 4, 10)
self.print_to_help_box('q - quit')
while not self.x_pressed():
self.k = self.read_char()
self.print_char(self.k)
def start(self):
self.initialize_cursor()
self.main_loop()
def main(stdscr):
try:
editor = Editor(stdscr)
editor.start()
except Exception as ex:
print(ex)
pass
if __name__ == '__main__':
my.wrapper(main)