From 885fc45751f05ad49fb99499b404afdf1c0582e1 Mon Sep 17 00:00:00 2001 From: Minecon724 Date: Sun, 30 Jun 2024 18:08:02 +0200 Subject: [PATCH] new --- README.md | 5 +- editor.py | 13 +++ main.py | 18 ---- writer/__main__.py | 206 +++++++++++++++++++++++++++++++++++++++++++++ writer/keys.py | 3 + 5 files changed, 226 insertions(+), 19 deletions(-) create mode 100644 editor.py delete mode 100644 main.py create mode 100644 writer/__main__.py create mode 100644 writer/keys.py diff --git a/README.md b/README.md index bdc44ab..603875e 100644 --- a/README.md +++ b/README.md @@ -3,4 +3,7 @@ 1. `head -c 8 myfile` 2. `head -c 16M >myfile` -You can repeat the second command to grow the file \ No newline at end of file +You can repeat the second command to grow the file + +### Run editor +`python3 writer` \ No newline at end of file diff --git a/editor.py b/editor.py new file mode 100644 index 0000000..0babb43 --- /dev/null +++ b/editor.py @@ -0,0 +1,13 @@ +class Editor: + content: str = '' + print_func: function + + def __init__(self, print_func: function=print): + self.print_func = print_func + pass + + def add_character(self, char: str): + self.print_func(char) + + def backspace(self): + self.print_func('^H') \ No newline at end of file diff --git a/main.py b/main.py deleted file mode 100644 index a2d3c4d..0000000 --- a/main.py +++ /dev/null @@ -1,18 +0,0 @@ -from notepad import Shard, Entry - -print("loading shart...") -shard = Shard.load_shard('myfile') -print("Size:", shard.size) - -print("now creating entry") -entry = Entry(shard) -entry.add_content("Lorm Ipsum dolor sit ammet, consectetuer adipiscng elit, We're no strangers to luv You know the rules and so do I A full commitment's wat I'm thinkin of You wouldn't get this from any other guy very good very nice".encode()) -print("entry created, its length:", len(entry.to_bytes()), "now writing it") - -try: - shard.write_entry(entry) - print("entry written") -except EOFError: - print("oops, looks like the file wont fit this") - -shard.close() \ No newline at end of file diff --git a/writer/__main__.py b/writer/__main__.py new file mode 100644 index 0000000..95a5ed0 --- /dev/null +++ b/writer/__main__.py @@ -0,0 +1,206 @@ +from os import get_terminal_size, O_NONBLOCK +from sys import stdin +from tty import setcbreak +from fcntl import fcntl, F_GETFL, F_SETFL + +from keys import * + +def wrapper(f: callable, *args: any): + try: + fd = stdin.fileno() + orig_fl = fcntl(fd, F_GETFL) + + setcbreak(fd) + fcntl(fd, F_SETFL, orig_fl | O_NONBLOCK) + + f(*args) + finally: + fcntl(fd, F_SETFL, orig_fl) + + +def rprint(*text: str): + print(*text, end='', flush=True) + +def cprint(*code: str): + rprint(*['\033[' + c for c in code]) + +def refresh(lines: list[str], ncols: int) -> tuple[int, int]: + cprint('2J', '0;0H') + y = 0 + cl = -1 + for l in lines: + _ = 0 + while _ == 0 or l != '': + cl = l[:ncols] + print(cl) + _ += 1 + l = l[ncols:] + y += _ + + x = len(cl) + 1 + if x - 1 == ncols: + x = 0 + y += 1 + cprint(f'{y};{x}H') + + return (y, x) + +def main(): + cprint('2J', '0;0H') + terminal_size = None + + y = 0 + x = 0 + + lines = [''] + cur_line = 0 + line_pos = 0 + + while True: + new_terminal_size = get_terminal_size() + if new_terminal_size != terminal_size: + terminal_size = new_terminal_size + + y, x = refresh(lines, terminal_size.columns) + + key = stdin.read(1) + if key == '': + continue + elif key == KEY_ESCAPE: + next = stdin.read(1) + pass + elif key == KEY_BACKSPACE: + pass + elif key == KEY_ENTER: + print('') + cur_line += 1 + if cur_line >= len(lines): + lines.append('') + elif 31 < ord(key) < 127: + lines[cur_line] += key + rprint(key) + x += 1 + #print(key.encode()) + +if __name__ == "__main__": + wrapper(main) + +exit() + +def main(screen: curses.window): + curses.use_default_colors() + for i in range(0, curses.COLORS): + curses.init_pair(i + 1, i, -1) + + nlines = 0 + ncols = 0 + + y = 0 + x = 0 + + content = "" + lines = [''] + cur_line = 0 + + color = curses.color_pair() + + while True: + nnlines, nncols = screen.getmaxyx() + if nnlines != nlines or nncols != ncols: + print('resized') + nlines, ncols = nnlines, nncols + screen.erase() + curses.resizeterm(nlines, ncols) + i = 0 + for l in lines: + if l == '': + i += 1 + continue + while l != '': + try: + screen.addstr(i, 0, l[:ncols]) + except curses.error: + continue # TODO don't loop + l = l[ncols:] + i += 1 + + screen.refresh() + key = screen.getch() + y, x = screen.getyx() + #print(key, chr(key), 31 < key < 256, end='', flush=True) + + if 31 < key < 256: + if key == 38: # ; + if color is None: + screen.addch(sy, sx, ' ') + screen.move(sy, sx) + else: + sy, sx = y, x + screen.addch('?', curses.color_pair(2)) + screen.move(sy, sx) + continue + elif color == None: + screen.addch(sy, sx, ' ') + screen.move(sy, sx) + + if 48 < key < 57: + # print(key - 49) + color = curses.color_pair(key - 49) + else: + color = -1 + continue + ch = chr(key) + + screen.addch(ch, color) + lines[cur_line] += ch + elif key == curses.KEY_ENTER or key == 10: + cur_line += 1 + if cur_line+1 >= len(lines): + lines.append('') + + screen.move(y+1, 0) + elif key == curses.KEY_BACKSPACE: + lines[cur_line] = lines[cur_line][:-1] + + x -= 1 + if x == -1: + if y == 0: + continue + y -= 1 + if len(lines[cur_line]) == 0: + x = len(lines[y]) + cur_line -= 1 + else: + x = ncols + + screen.addch(y, x, ' ') + screen.move(y, x) + elif key == curses.KEY_HOME: + screen.move(y, 0) + elif key == curses.KEY_END: + x = len(lines[y]) + screen.move(y, x) + +if __name__ == "__main__": + curses.wrapper(main) + +exit() + + + +print("loading shart...") +shard = Shard.load_shard('myfile') +print("Size:", shard.size) + +print("now creating entry") +entry = Entry(shard) +entry.add_content("Lorm Ipsum dolor sit ammet, consectetuer adipiscng elit, We're no strangers to luv You know the rules and so do I A full commitment's wat I'm thinkin of You wouldn't get this from any other guy very good very nice".encode()) +print("entry created, its length:", len(entry.to_bytes()), "now writing it") + +try: + shard.write_entry(entry) + print("entry written") +except EOFError: + print("oops, looks like the file wont fit this") + +shard.close() \ No newline at end of file diff --git a/writer/keys.py b/writer/keys.py new file mode 100644 index 0000000..1a5bd58 --- /dev/null +++ b/writer/keys.py @@ -0,0 +1,3 @@ +KEY_ESCAPE = '\x1b' +KEY_BACKSPACE = '\x7f' +KEY_ENTER = '\n' \ No newline at end of file