import curses
import sys
import os
from dataclasses import dataclass, field
from Core import save_file, load_file, Cursor, visual_cursor_renderer
from UI import StatusWindow, ContentBufferWindow, CommandBox, WindowManager
# Shortcut Dictionary
shortcutKeys={}
#SETUP
#NOTE Will go in its own file eventually.
def screen_setup(stdscr):
curses.raw()
curses.noecho()
stdscr.keypad(1)
def screen_initialiser(stdscr):
term_row, term_column = stdscr.getmaxyx()
buffer = []
src =sys.argv[1] if len(sys.argv)==2 else 'noname.txt'
cursor = Cursor(buffer=buffer)
load_file(buffer= buffer, filepath= src)
return term_row, term_column, buffer, src, cursor
#MAIN
def main(stdscr):
# Initialize the screen
mainscreen = stdscr
screen_setup(stdscr=mainscreen)
term_row, term_column, buffer, src, cursor =screen_initialiser(stdscr=mainscreen)
window_manager = WindowManager(buffer= buffer, cursor= cursor, width= term_column, height = term_row, src= src)
while True:
mainscreen.move(0,0) #positions the cursor to top left
#SCROLLING
cursor.cursor_scrolling(term_column=term_column, visible_rows= window_manager.content.height)
#SCREEN RENDERING
window_manager.render()
#CURSOR RENDERING
visual_cursor_renderer(screen=mainscreen, cursor = cursor)
#input manager
ch =mainscreen.getch()
#Text insert
if ch != ((ch) & 0x1f) and ch < 128:
buffer[cursor.row].insert(cursor.column, ch)
cursor.column +=1
#enter handling
if chr(ch) in "\n\r":
line = buffer[cursor.row][cursor.column:]
buffer[cursor.row] = buffer[cursor.row][:cursor.column]
cursor.row+=1
cursor.column = 0
buffer.insert(cursor.row, line)
#Backspace handling
if ch in [8,263]:
if cursor.column:
try:
cursor.column -=1
del buffer[cursor.row][cursor.column]
except IndexError:
cursor.column = len(cursor.buffer[cursor.row])
elif cursor.row>0:
line = buffer[cursor.row][cursor.column:]
del buffer[cursor.row]
cursor.row -=1
cursor.column = len(buffer[cursor.row])
buffer[cursor.row] += line
#CURSOR ACTIONS
cursor.cursor_actions(ch=ch)
#save and quit
if ch == (ord("s") & 0x1f): # Ctrl+S
save_file(buffer=buffer, filepath=src)
if ch == (ord("q") & 0x1f): # Ctrl+Q
sys.exit()
curses.wrapper(main) #this protects the terminal