I needed a light and simple text editor.
Sometimes I need to write micro programs in python or batch, with this little script I solved my problem.
How to use:
py ste.py "filename I want to save"
import sys,os
import curses
#ste : simple(or stupid) text editor
def editor(stdscr):
k = 0
h = 0
cursor_x = 0
cursor_y = 0
stdscr.clear()
stdscr.refresh()
curses.start_color()
curses.init_pair(1, curses.COLOR_BLACK, curses.COLOR_WHITE)
curses.init_pair(2, curses.COLOR_GREEN, curses.COLOR_BLACK)
while (True):
height, width = stdscr.getmaxyx()
if k == curses.KEY_DOWN:
cursor_y += 1
elif k == curses.KEY_UP:
cursor_y -= 1
elif k == curses.KEY_RIGHT:
cursor_x += 1
elif k == curses.KEY_LEFT:
cursor_x -= 1
elif k == 10: #ENTER
cursor_x=1
cursor_y+=1
h=cursor_y
elif k == 9: #tab
cursor_x = cursor_x +3
elif k == 265: #F1
break
elif k == 266: #
stringa=''
outstr=''
f=open(sys.argv[1],"wb+")
for i in range(h+1):
outstr=stdscr.instr(i, 1) #Read the screen
f.write(outstr)
acapo='\n'
f.write(acapo.encode())
f.close()
else:
stdscr.attron(curses.color_pair(2))
curses.echo()
cursor_x = cursor_x+1
cursor_x = max(0, cursor_x)
cursor_x = min(width-1, cursor_x)
cursor_y = max(0, cursor_y)
cursor_y = min(height-1, cursor_y)
statusbarstr = "Press 'F1' to exit | 'F2' to save | https://danieleporcaripython.blogspot.com"
stdscr.attron(curses.color_pair(1))
stdscr.addstr(height-1, 0, statusbarstr)
stdscr.addstr(height-1, len(statusbarstr), " " * (width - len(statusbarstr) - 1))
stdscr.attroff(curses.color_pair(1))
stdscr.move(cursor_y, cursor_x)
stdscr.refresh()
stdscr.attron(curses.color_pair(2))
k = stdscr.getch()
def main():
curses.wrapper(editor)
if __name__ == "__main__":
main()