From 3a088311413fec80f30812f985ceda24874053f3 Mon Sep 17 00:00:00 2001 From: Minecon724 Date: Tue, 2 Jul 2024 17:36:28 +0200 Subject: [PATCH] comit tfw you have an issue only 1 person ever had thats what happende while i was trying to commit --- README.md | 2 +- file.txt | 1 + writer/__main__.py | 48 ++++++++++++++++++++++----------- notepad.py => writer/notepad.py | 2 +- 4 files changed, 36 insertions(+), 17 deletions(-) create mode 100644 file.txt rename notepad.py => writer/notepad.py (96%) diff --git a/README.md b/README.md index 603875e..2a6528c 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ ### Generating a file -1. `head -c 8 myfile` +1. `head -c 32 myfile` 2. `head -c 16M >myfile` You can repeat the second command to grow the file diff --git a/file.txt b/file.txt new file mode 100644 index 0000000..95d09f2 --- /dev/null +++ b/file.txt @@ -0,0 +1 @@ +hello world \ No newline at end of file diff --git a/writer/__main__.py b/writer/__main__.py index 96aa0bc..9b5d6d7 100644 --- a/writer/__main__.py +++ b/writer/__main__.py @@ -4,6 +4,7 @@ from termios import tcgetattr, tcsetattr, TCSADRAIN from tty import setcbreak from fcntl import fcntl, F_GETFL, F_SETFL +from notepad import Shard from keys import * def wrapper(f: callable, *args: any): @@ -19,7 +20,6 @@ def wrapper(f: callable, *args: any): except KeyboardInterrupt: cprint('2J') move(0, 0) - pass finally: fcntl(fd, F_SETFL, old_fl) tcsetattr(fd, TCSADRAIN, old_tc) @@ -76,7 +76,7 @@ def nl(y: int, x: int) -> tuple[int]: return (y + 1, 0) -def move_back(y: int, x: int, line_pos: int, cur_line: int, lines: list[str], ncols: int) -> tuple[int]: +def move_back(y: int, x: int, line_pos: int, cur_line: int, lines: list[str], ncols: int) -> tuple[int] | None: if x != 0: line_pos -= 1 x -= 1 @@ -87,7 +87,7 @@ def move_back(y: int, x: int, line_pos: int, cur_line: int, lines: list[str], nc x = line_pos % ncols y -= 1 else: - rprint(KEY_BELL) + return None return (y, x, line_pos, cur_line) @@ -112,13 +112,12 @@ def move_forward(y: int, x: int, line_pos: int, cur_line: int, lines: list[str], return (y, x, line_pos, cur_line) -def main(): +def main(lines: list[str]): terminal_size = None y = 0 x = 0 - lines = [''] cur_line = 0 line_pos = 0 @@ -160,11 +159,15 @@ def main(): line_pos = len(lines[cur_line]) x = line_pos % terminal_size.columns y -= 1""" - y, x, line_pos, cur_line = move_back(y, x, line_pos, cur_line, lines, terminal_size.columns) - move(y, x) - rprint(' ') - move(y, x) - lines[cur_line] = remove_str(lines[cur_line], line_pos) + mv = move_back(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) + rprint(' ') + move(y, x) + lines[cur_line] = remove_str(lines[cur_line], line_pos) elif key == KEY_ENTER: y, x = nl(y, x) @@ -173,21 +176,36 @@ def main(): if len(lines) == cur_line: lines.append('') elif 31 < ord(key) < 127: # keystroke + lines[cur_line] = insert_str(lines[cur_line], key, line_pos) line_pos += 1 x += 1 if x - 1 == terminal_size.columns: y, x = nl(y, x) - rprint(key) - lines[cur_line] = insert_str(lines[cur_line], key, line_pos) + #rprint(key) y, x = refresh(line_pos, cur_line, lines, terminal_size.columns) # TODO make this not needed if __name__ == "__main__": - wrapper(main) + exists = True + + try: + lines = open('file.txt', 'r').read().split('\n') + except FileNotFoundError: + lines = ['close this and create file.txt to save this'] + exists = False + + wrapper(main, lines) + + if exists: + with open('file.txt', 'w') as file: + file.writelines(lines) + + +"""OLD CODE + -exit() def main(screen: curses.window): curses.use_default_colors() @@ -305,4 +323,4 @@ try: except EOFError: print("oops, looks like the file wont fit this") -shard.close() \ No newline at end of file +shard.close()""" \ No newline at end of file diff --git a/notepad.py b/writer/notepad.py similarity index 96% rename from notepad.py rename to writer/notepad.py index 14d8315..a22199f 100644 --- a/notepad.py +++ b/writer/notepad.py @@ -5,7 +5,7 @@ from time import time @dataclass class Shard: - size: int + size: int # total size, excluding this 8 byte var and last entry file: BufferedRandom file_size: int