105 lines
3.5 KiB
Python
105 lines
3.5 KiB
Python
import curses
|
|
|
|
insert_char_at_position = lambda string, char, position: string[:position] + char + string[position:]
|
|
remove_char_at_position = lambda string, position: string[:position] + string[position+1:]
|
|
split_list = lambda input_list, chunk_size: [input_list[i:i + chunk_size] for i in range(0, len(input_list), chunk_size)]
|
|
|
|
class Editor():
|
|
def __init__(self,stdscr,file=str) -> None:
|
|
self.stdscr = stdscr
|
|
self.x = 0
|
|
self.y = 0
|
|
self.tx,self.ty = curses.COLS ,curses.LINES
|
|
self.text_content = self.get_content(file)
|
|
self.lines = len(self.text_content)
|
|
self.pages = self.make_pages(self.text_content)
|
|
self.page = 0
|
|
|
|
def handle_keys(self):
|
|
key = self.stdscr.getch()
|
|
if key == curses.KEY_UP:
|
|
self.y = max(0, self.y - 1)
|
|
if self.y == 0 and self.page != 0:
|
|
self.page -= 1
|
|
self.y = self.ty-2
|
|
elif key == curses.KEY_DOWN:
|
|
self.y = min(self.ty - 1, self.y + 1)
|
|
if self.y == self.ty - 1 and self.page < len(self.pages) - 1:
|
|
self.page += 1
|
|
self.y = 0
|
|
elif key == curses.KEY_LEFT:
|
|
self.x = max(0, self.x - 1)
|
|
elif key == curses.KEY_RIGHT:
|
|
self.x = min(len(self.text_content[self.y]), self.x + 1)
|
|
elif key == curses.KEY_BACKSPACE:
|
|
if self.x > 0:
|
|
self.pages[self.page][self.y] = remove_char_at_position(
|
|
self.pages[self.page][self.y],
|
|
self.x - 1
|
|
)
|
|
self.x -= 1
|
|
elif key == curses.KEY_ENTER:
|
|
# Neue leere Zeile einfügen
|
|
self.new_line()
|
|
elif key != curses.ERR:
|
|
self.pages[self.page][self.y] = insert_char_at_position(
|
|
self.pages[self.page][self.y],
|
|
str(self.pages[self.page]),#chr(key),
|
|
self.x
|
|
)
|
|
self.x += 1
|
|
|
|
#def make_pages(self, content):
|
|
# return split_list(content, self.ty-1)
|
|
|
|
|
|
|
|
def get_content(self,file):
|
|
try:
|
|
with open(file) as content:
|
|
return content.readlines()
|
|
except FileNotFoundError:
|
|
return ["no data found","please check path"]
|
|
|
|
def make_pages(self,content):
|
|
#pages = []
|
|
#page = []
|
|
#for line in content:
|
|
# if len(page) >= self.ty - 1:
|
|
# pages.append(page)
|
|
# page = []
|
|
# page.append(line)
|
|
#if page:
|
|
# pages.append(page)
|
|
#return pages
|
|
|
|
return split_list(content, self.ty-1)
|
|
|
|
def print_data(self):
|
|
self.stdscr.clear()
|
|
for i, line in enumerate(self.pages[self.page]):
|
|
self.stdscr.addstr(i, 0, f"{line}")#{i+self.ty*self.page} -
|
|
self.stdscr.refresh()
|
|
|
|
def set_cursor_position(self):
|
|
self.stdscr.move(self.y, self.x)
|
|
def new_line(self):
|
|
self.pages[self.page].insert(self.y + 1, "")
|
|
self.pages = self.make_pages(sum(self.pages, [])) # Seiten neu erstellen
|
|
self.print_data()
|
|
self.y += 1 # Die Position nach unten verschieben, um die neu eingefügte Zeile anzuzeigen
|
|
def del_line(self):
|
|
pass
|
|
|
|
|
|
def main(stdscr):
|
|
file_name = "/home/megamichi/Schreibtisch/coding/terminal/test.txt"
|
|
editor = Editor(stdscr, file_name)
|
|
curses.curs_set(1)
|
|
while True:
|
|
editor.print_data()
|
|
editor.set_cursor_position()
|
|
editor.handle_keys()
|
|
|
|
if __name__ == "__main__":
|
|
curses.wrapper(main)
|