This commit is contained in:
Minecon724 2024-06-30 18:08:02 +02:00
parent 5a0d446b43
commit 885fc45751
Signed by: Minecon724
GPG key ID: 3CCC4D267742C8E8
5 changed files with 226 additions and 19 deletions

View file

@ -3,4 +3,7 @@
1. `head -c 8 </dev/zero >myfile`
2. `head -c 16M </dev/random >>myfile`
You can repeat the second command to grow the file
You can repeat the second command to grow the file
### Run editor
`python3 writer`

13
editor.py Normal file
View file

@ -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')

18
main.py
View file

@ -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()

206
writer/__main__.py Normal file
View file

@ -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()

3
writer/keys.py Normal file
View file

@ -0,0 +1,3 @@
KEY_ESCAPE = '\x1b'
KEY_BACKSPACE = '\x7f'
KEY_ENTER = '\n'