-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMegaEditor.py
184 lines (157 loc) · 5.97 KB
/
MegaEditor.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import curses
import curses.panel
import os.path
class MegaEditor:
def __init__(self, id, maxy, maxx, file):
self.id = id
self.maxx = maxx
self.maxy = maxy
self.window = curses.newwin(maxy, maxx, 3, 1)
self.window.refresh()
self.lines = [""]
self.file = file
self.start_y = 0
self.line_index = 0
self.init_lines()
self.start_y = 0
self.curr_y = 0
self.curr_x = 0
self.write_lines()
def init_lines(self):
if os.path.isfile(self.file):
self.lines = [line.rstrip('\n') for line in open(self.file)]
def focus(self):
self.window.move(self.curr_y, self.curr_x)
self.window.refresh()
def resize(self, y, x):
self.window.resize(y, x)
def write(self, c):
self.line_index = self.start_y + self.curr_y
if ord(c) == 10:
part_one = self.lines[self.line_index][:self.curr_x]
part_two = self.lines[self.line_index][self.curr_x:]
self.lines = self.lines[:self.line_index+1] + [part_two] + self.lines[self.line_index+1:]
self.lines[self.line_index] = part_one
self.curr_x = 0
if self.curr_y < self.maxy-1:
self.curr_y += 1
else:
self.start_y += 1
else:
if ord(c) == 9:
self.lines[self.line_index] = self.lines[self.line_index][:self.curr_x] + " " + self.lines[self.line_index][self.curr_x:]
self.curr_x += 4
else:
self.lines[self.line_index] = self.lines[self.line_index][:self.curr_x] + c + self.lines[self.line_index][self.curr_x:]
self.curr_x += 1
self.write_lines()
self.window.refresh()
def write_lines(self):
ty = 0
self.window.clear()
end_y = self.start_y + self.maxy
if end_y > len(self.lines):
end_y = len(self.lines)
lines = self.lines[self.start_y:end_y]
for line in lines:
self.window.addstr(ty, 0, line)
ty += 1
self.window.move(self.curr_y, self.curr_x)
self.window.refresh()
def backspace(self):
self.line_index = self.start_y + self.curr_y
if self.curr_x == 0 and self.line_index != 0:
part_one = self.lines[self.line_index-1]
part_two = self.lines[self.line_index]
self.lines = self.lines[:self.line_index] + self.lines[self.line_index+1:]
self.lines[self.line_index-1] = part_one + part_two
if self.curr_y > 0:
self.curr_y -= 1
else:
self.start_y -= 1
self.curr_x = len(part_one)
elif self.curr_x == 0 and self.line_index == 0:
return False
elif self.curr_x == len(self.lines[self.line_index]):
self.curr_x -= 1
self.lines[self.line_index] = self.lines[self.line_index][:self.curr_x]
else:
self.curr_x -= 1
temp = self.lines[self.line_index]
part_one = temp[:self.curr_x]
part_two = temp[self.curr_x+1:]
self.lines[self.line_index] = part_one + part_two
self.write_lines()
def delete(self):
self.line_index = self.start_y + self.curr_y
if self.line_index == len(self.lines[self.line_index]) and self.line_index != len(self.lines)-1:
part_one = self.lines[self.line_index]
part_two = self.lines[self.line_index+1]
self.lines = self.lines[:self.line_index] + self.lines[self.line_index+1:]
self.lines[self.line_index] = part_one + part_two
self.curr_x = len(part_one)
elif self.curr_x == len(self.lines[self.line_index]) and self.line_index == len(self.lines)-1:
return False
# elif self.curr_x == 0:
# self.lines[self.curr_y] = self.lines[self.curr_y][:self.curr_x]
else:
temp = self.lines[self.line_index]
part_one = temp[:self.curr_x]
part_two = temp[self.curr_x+1:]
self.lines[self.line_index] = part_one + part_two
self.write_lines()
def moveLeft(self):
self.line_index = self.start_y + self.curr_y
if self.curr_x == 0:
if self.line_index == 0:
return False
else:
if self.curr_y > 0:
self.curr_y -= 1
else:
self.start_y -= 1
self.curr_x = len(self.lines[self.line_index])
else:
self.curr_x -= 1
self.write_lines()
def moveRight(self):
self.line_index = self.start_y + self.curr_y
if self.curr_x == len(self.lines[self.line_index]):
if self.line_index == len(self.lines)-1:
return False
else:
if self.curr_y < self.maxy - 1:
self.curr_y += 1
else:
self.start_y += 1
self.curr_x = 0
else:
self.curr_x += 1
self.write_lines()
def moveUp(self):
self.line_index = self.start_y + self.curr_y
if self.line_index <= 0:
return False
if self.curr_y > 0:
self.curr_y -= 1
else:
self.start_y -= 1
if self.curr_x > len(self.lines[self.line_index-1]):
self.curr_x = len(self.lines[self.line_index-1])
self.write_lines()
def moveDown(self):
self.line_index = self.start_y + self.curr_y
if self.line_index == len(self.lines)-1:
return False
if self.curr_y < self.maxy-1:
self.curr_y += 1
else:
self.start_y += 1
if self.curr_x > len(self.lines[self.line_index]):
self.curr_x = len(self.lines[self.line_index])
self.write_lines()
def save(self):
file = open(self.file, "w+")
for line in self.lines:
file.write(line+"\r\n")
file.close()