Skip to content

Instantly share code, notes, and snippets.

@blambi
Created December 4, 2014 15:00
Show Gist options
  • Save blambi/cf05d232148609b8c10c to your computer and use it in GitHub Desktop.
Save blambi/cf05d232148609b8c10c to your computer and use it in GitHub Desktop.
Perm ED
"""A minimalist try at a text editor"""
class App( Applet ):
caption = u"Ed"
def __init__( self, term ):
Applet.__init__( self, term )
self.text = list()
self.filename = u'unnamed'
self.input = u''
self.cur_row = 0
self.term.write( u"Welcome to ed\n" )
#self.term.write( u"ed 0> " )
def show( self ):
if self.args != []: # read first as file
self.read_file( self.args[0] )
self.term.write( u'ed %d> %s' %( self.cur_row, self.input ) )
def event( self, event ):
if event.type == events.EVENT_KEYDOWN:
if event.keycode == 8:
if self.input != u'':
self.input = self.input[:-1]
self.term.backspace()
elif event.key == u'\n':
if self.term.mode != terminals.STUPID:
self.term.write( u'\n' )
self.handle() # do stuff!
# reset the input
self.term.write( u'ed %d> ' % self.cur_row )
self.input = u''
elif len( event.key ) != 1: # special keys
self.term.write( self.text[ self.cur_row ] + u'\n' )
if event.key == u'KEYDOWN':
if self.cur_row + 1 < len( self.text ):
self.cur_row += 1
elif event.key == u'KEYUP':
if self.cur_row - 1 >= 0:
self.cur_row -= 1
self.term.write( u'ed %d> ' % self.cur_row )
elif event.key != u'' and event.key != u'\n':
self.input += event.key
if self.term.mode != terminals.STUPID:
self.term.write( event.key )
def handle( self ):
cmd = self.input.split( u' ' )
if ( cmd[0] == u'h' or cmd[0] == u'help' ):
# help, writes a simple command list
docs = ( u'h, help - this text',
u'o, open file - opens/reads a file',
u'j, jump row - jump to row',
u'p, print - prints current row and incr row pointer',
u'c, cat - prints the current buffer',
u's, save file - save buffer to file (if unnamed current)',
u'i, insert - inserts a blank row after row pointer',
u'quit - exits ed' )
self.term.write( u'Available commands:\n' )
for c in docs:
self.term.write( c + u'\n' )
elif ( cmd[0] == u'o' or cmd[0] == u'open' ) and len( cmd ) == 2:
self.read_file( cmd[1] )
elif ( cmd[0] == u's' or cmd[0] == u'save' ):
# Save
if len( cmd ) == 2:
targ = cmd[1]
else:
targ = self.filename
self.term.write( u'Saving %s...\n' % targ )
if targ.count( u'/' ):
filename = targ
else:
filename = self.workdir + u'/' + targ
result = kernel.fs.write_file( filename, self.text )
if not result:
self.term.write( u"Error: " + kernel.log.tail( 1 )[0] +\
u'\n' )
elif ( cmd[0] == u'j' or cmd[0] == u'jump' ) and len( cmd ) == 2:
# jump
if not cmd[1].isdigit():
self.term.write( u"what???\n" )
return
row = int( cmd[1] )
if len( self.text ) <= row or row < 0:
self.term.write( u"Outside of the file, sorry\n" )
return
self.cur_row = row
elif cmd[0] == u'p' or cmd[0] == u'print':
# print (and increse pointer)
self.term.write( self.text[ self.cur_row ] + u'\n' )
if self.cur_row + 1 < len( self.text ):
self.cur_row += 1
elif cmd[0] == u'c' or cmd[0] == u'cat':
# print all but keep pointer
c = 0
for row in self.text:
self.term.write( u"%d: %s\n" %( c, row ) )
c += 1
elif cmd[0] == u'i' or cmd[0] == u'insert':
# inserts a row after current row
self.cur_row += 1
self.text.insert( self.cur_row, u'' )
elif cmd[0] == u'quit':
self.term.write( u"Bye Bye\n" )
kernel.remove_applet( self.app_id )
return
elif self.input.startswith( u'.' ):
# new line entered to replace current row
if len( self.text ) == 0:
self.text.append( u'' )
self.text[self.cur_row] = self.input[1:]
if self.cur_row +1 >= len( self.text ):
self.text.append( u'' )
self.cur_row += 1
elif cmd[0] != u'':
self.term.write( u"Unknown command..\n" )
def read_file( self, filename ):
self.term.write( u'Opening %s...\n' % filename )
if filename[0] != u'/':
filename = self.workdir + filename
#path, wfilename = filename.rsplit( u'/', 1 )
#else:
# wfilename = filename
# path = self.workdir
#self.filename = path + u'/' + wfilename
self.text = kernel.fs.read_file( filename )
if self.text == None:
self.term.write( u"Error: " + kernel.log.tail( 1 )[0] + u'\n' )
self.text = []
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment