This commit is contained in:
Minecon724 2024-07-03 16:12:05 +02:00
parent 3a08831141
commit 547a8fcab2
Signed by: Minecon724
GPG key ID: 3CCC4D267742C8E8
3 changed files with 84 additions and 18 deletions

View file

@ -1 +1 @@
hello world
asdasdhello worlasnigasaafaaafaasddsfasddsadsasdsdaasdsasdadasdadsasdasdddadakasdfsfd

26
test.py Normal file
View 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)

View file

@ -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)
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]):
terminal_size = None
@ -118,12 +150,12 @@ def main(lines: list[str]):
y = 0
x = 0
cur_line = 0
line_pos = 0
cur_line = len(lines) - 1
line_pos = len(lines[-1])
while True:
new_terminal_size = get_terminal_size()
if new_terminal_size != terminal_size:
new_terminal_size = get_terminal_size() # TODO make this not needed
if terminal_size != new_terminal_size:
terminal_size = new_terminal_size
y, x = refresh(line_pos, cur_line, lines, terminal_size.columns)
@ -132,19 +164,26 @@ def main(lines: list[str]):
if key == '':
continue
elif key == KEY_ESCAPE:
next = stdin.read(1)
if next == '[':
next = stdin.read(1)
if next == 'A':
cprint('A')
elif next == 'B':
cprint('B')
elif next == 'C':
y, x, line_pos, cur_line = move_forward(y, x, line_pos, cur_line, lines, terminal_size.columns)
move(y, x)
elif next == 'D':
y, x, line_pos, cur_line = move_back(y, x, line_pos, cur_line, lines, terminal_size.columns)
move(y, x)
key = stdin.read(1)
if key == '[':
key = stdin.read(1)
keycode = ord(key)
if 64 < keycode < 69:
mv = move_delta(keycode - 65, y, x, line_pos, cur_line, lines, terminal_size.columns)
if mv is None:
rprint(KEY_BELL)
else:
y, x, line_pos, cur_line = mv
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:
"""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
if __name__ == "__main__":
exists = True