commit
This commit is contained in:
parent
3a08831141
commit
547a8fcab2
3 changed files with 84 additions and 18 deletions
2
file.txt
2
file.txt
|
@ -1 +1 @@
|
||||||
hello world
|
asdasdhello worlasnigasaafaaafaasddsfasddsadsasdsdaasdsasdadasdadsasdasdddadakasdfsfd
|
26
test.py
Normal file
26
test.py
Normal file
|
@ -0,0 +1,26 @@
|
||||||
|
from os import get_terminal_size, O_NONBLOCK
|
||||||
|
from sys import stdin
|
||||||
|
from termios import tcgetattr, tcsetattr, TCSADRAIN
|
||||||
|
from tty import setcbreak
|
||||||
|
|
||||||
|
def wrapper(f: callable, *args: any):
|
||||||
|
try:
|
||||||
|
fd = stdin.fileno()
|
||||||
|
old_tc = tcgetattr(fd)
|
||||||
|
|
||||||
|
setcbreak(fd)
|
||||||
|
|
||||||
|
f(*args)
|
||||||
|
except KeyboardInterrupt:
|
||||||
|
cprint('2J')
|
||||||
|
move(0, 0)
|
||||||
|
finally:
|
||||||
|
tcsetattr(fd, TCSADRAIN, old_tc)
|
||||||
|
|
||||||
|
def main():
|
||||||
|
while True:
|
||||||
|
key = stdin.read(1)
|
||||||
|
print(key.encode())
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
wrapper(main)
|
|
@ -111,6 +111,38 @@ def move_forward(y: int, x: int, line_pos: int, cur_line: int, lines: list[str],
|
||||||
|
|
||||||
return (y, x, line_pos, cur_line)
|
return (y, x, line_pos, cur_line)
|
||||||
|
|
||||||
|
def move_down(y: int, x: int, line_pos: int, cur_line: int, lines: list[str], ncols: int) -> tuple[int] | None:
|
||||||
|
lines_count = len(lines) - 1
|
||||||
|
|
||||||
|
if cur_line < lines_count:
|
||||||
|
cur_line += 1
|
||||||
|
line_len = len(lines[cur_line])
|
||||||
|
if line_pos > line_len:
|
||||||
|
line_pos = line_len
|
||||||
|
else:
|
||||||
|
return None
|
||||||
|
|
||||||
|
return (y, x, line_pos, cur_line)
|
||||||
|
|
||||||
|
def move_up(y: int, x: int, line_pos: int, cur_line: int, lines: list[str], ncols: int) -> tuple[int] | None:
|
||||||
|
lines_count = len(lines) - 1
|
||||||
|
|
||||||
|
if cur_line > 0:
|
||||||
|
cur_line -= 1
|
||||||
|
if line_pos > 0:
|
||||||
|
line_pos = 0
|
||||||
|
else:
|
||||||
|
return None
|
||||||
|
|
||||||
|
return (y, x, line_pos, cur_line)
|
||||||
|
|
||||||
|
def move_delta(movement: int, y: int, x: int, line_pos: int, cur_line: int, lines: list[str], ncols: int) -> tuple[int] | None:
|
||||||
|
match movement:
|
||||||
|
case 0: return move_up(y, x, line_pos, cur_line, lines, ncols)
|
||||||
|
case 1: return move_down(y, x, line_pos, cur_line, lines, ncols)
|
||||||
|
case 2: return move_forward(y, x, line_pos, cur_line, lines, ncols)
|
||||||
|
case 3: return move_back(y, x, line_pos, cur_line, lines, ncols)
|
||||||
|
|
||||||
|
|
||||||
def main(lines: list[str]):
|
def main(lines: list[str]):
|
||||||
terminal_size = None
|
terminal_size = None
|
||||||
|
@ -118,12 +150,12 @@ def main(lines: list[str]):
|
||||||
y = 0
|
y = 0
|
||||||
x = 0
|
x = 0
|
||||||
|
|
||||||
cur_line = 0
|
cur_line = len(lines) - 1
|
||||||
line_pos = 0
|
line_pos = len(lines[-1])
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
new_terminal_size = get_terminal_size()
|
new_terminal_size = get_terminal_size() # TODO make this not needed
|
||||||
if new_terminal_size != terminal_size:
|
if terminal_size != new_terminal_size:
|
||||||
terminal_size = new_terminal_size
|
terminal_size = new_terminal_size
|
||||||
y, x = refresh(line_pos, cur_line, lines, terminal_size.columns)
|
y, x = refresh(line_pos, cur_line, lines, terminal_size.columns)
|
||||||
|
|
||||||
|
@ -132,19 +164,26 @@ def main(lines: list[str]):
|
||||||
if key == '':
|
if key == '':
|
||||||
continue
|
continue
|
||||||
elif key == KEY_ESCAPE:
|
elif key == KEY_ESCAPE:
|
||||||
next = stdin.read(1)
|
key = stdin.read(1)
|
||||||
if next == '[':
|
if key == '[':
|
||||||
next = stdin.read(1)
|
key = stdin.read(1)
|
||||||
if next == 'A':
|
keycode = ord(key)
|
||||||
cprint('A')
|
|
||||||
elif next == 'B':
|
if 64 < keycode < 69:
|
||||||
cprint('B')
|
mv = move_delta(keycode - 65, y, x, line_pos, cur_line, lines, terminal_size.columns)
|
||||||
elif next == 'C':
|
if mv is None:
|
||||||
y, x, line_pos, cur_line = move_forward(y, x, line_pos, cur_line, lines, terminal_size.columns)
|
rprint(KEY_BELL)
|
||||||
move(y, x)
|
else:
|
||||||
elif next == 'D':
|
y, x, line_pos, cur_line = mv
|
||||||
y, x, line_pos, cur_line = move_back(y, x, line_pos, cur_line, lines, terminal_size.columns)
|
move(y, x)
|
||||||
move(y, x)
|
elif key == 'H': # home
|
||||||
|
if line_pos != 0:
|
||||||
|
line_pos = 0
|
||||||
|
elif key == 'F': # end
|
||||||
|
line_len = len(lines[cur_line])
|
||||||
|
if line_pos < line_len:
|
||||||
|
line_pos = line_len
|
||||||
|
|
||||||
|
|
||||||
elif key == KEY_BACKSPACE:
|
elif key == KEY_BACKSPACE:
|
||||||
"""if x != 0:
|
"""if x != 0:
|
||||||
|
@ -187,6 +226,7 @@ def main(lines: list[str]):
|
||||||
|
|
||||||
y, x = refresh(line_pos, cur_line, lines, terminal_size.columns) # TODO make this not needed
|
y, x = refresh(line_pos, cur_line, lines, terminal_size.columns) # TODO make this not needed
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
exists = True
|
exists = True
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue