-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheditor.py
More file actions
executable file
·120 lines (106 loc) · 4.4 KB
/
Copy patheditor.py
File metadata and controls
executable file
·120 lines (106 loc) · 4.4 KB
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
import os
import time
import platform
from string import digits, ascii_letters, punctuation
from typing import Tuple
from sshkeyboard import listen_keyboard, stop_listening
keys = digits + ascii_letters + punctuation
class Editor:
def __init__(self):
self.text = ""
self.line = 1
self.cursor_position = (0, 0)
self._print()
listen_keyboard(on_press=self._on_press, on_release=None, delay_second_char=0.05, lower=False)
def _get_line_length(self, line: int) -> int:
lines = self.text.split("\n")
return len(lines[line])
def _get_end(self) -> Tuple[int, int]:
lines = self.text.split("\n")
return len(lines) - 1, self._get_line_length(len(lines) - 1) - 1
def _move_cursor(self, direction: str):
if direction == "left" and self.cursor_position != (0, 0):
if self.cursor_position[1] == 0:
newline = self.cursor_position[0] - 1
self.cursor_position = (newline, self._get_line_length(newline))
else:
self.cursor_position = (self.cursor_position[0], self.cursor_position[1] - 1)
end = self._get_end()
end = (end[0], end[1] + 1)
if direction == "right" and self.cursor_position != end:
if self.cursor_position[1] == self._get_line_length(self.cursor_position[0]):
self.cursor_position = (self.cursor_position[0] + 1, 0)
else:
self.cursor_position = (self.cursor_position[0], self.cursor_position[1] + 1)
if direction == "up" and self.cursor_position[0] > 0:
newline = self.cursor_position[0] - 1
newline_length = self._get_line_length(newline)
if newline_length < self.cursor_position[1]:
self.cursor_position = (self.cursor_position[0] - 1, newline_length)
else:
self.cursor_position = (self.cursor_position[0] - 1, self.cursor_position[1])
if direction == "down" and self.cursor_position[0] < self._get_end()[0]:
newline = self.cursor_position[0] + 1
newline_length = self._get_line_length(newline)
if newline_length < self.cursor_position[1]:
self.cursor_position = (self.cursor_position[0] + 1, newline_length)
else:
self.cursor_position = (self.cursor_position[0] + 1, self.cursor_position[1])
def _insert(self, key: str):
index = 0
for i in range(0, self.cursor_position[0]):
index += self._get_line_length(i) + 1 # + 1 because of \n
index += self.cursor_position[1]
self.text = self.text[:index] + key + self.text[index:]
def _remove(self):
index = 0
for i in range(0, self.cursor_position[0]):
index += self._get_line_length(i) + 1 # + 1 because of \n
index += self.cursor_position[1]
self.text = self.text[:index - 1] + self.text[index:]
def _on_press(self, key):
switch = {
"space": " "
}
if key == "enter":
self._insert("\n")
self._move_cursor("right")
elif key == "backspace":
self._remove()
self._move_cursor("left")
elif key == "left" or key == "right" or key == "up" or key == "down":
self._move_cursor(key)
elif key == "f10":
stop_listening()
time.sleep(0.1)
elif key == "tab":
for i in range(0, 3):
self._insert(" ")
self._move_cursor("right")
elif key in keys or key in switch.keys():
self._insert(switch[key] if key in switch.keys() else key)
self._move_cursor("right")
self._print()
@staticmethod
def _clear():
if platform.system() == "Windows":
os.system("cls")
else:
os.system("clear")
def _print(self):
self._clear()
print("F10 Compile code written in editor")
print("")
lines = self.text.split("\n")
for i in range(len(lines)):
print(f"{i + 1} ", end="")
line = lines[i]
for j in range(len(line)):
if self.cursor_position == (i, j):
print("|", end="")
print(line[j], end="")
if self.cursor_position == (i, len(line)):
print("|", end="")
print("")
if __name__ == "__main__":
e = Editor()