Skip to content

Commit

Permalink
WiPy related fixes and minor changes
Browse files Browse the repository at this point in the history
  • Loading branch information
robert-hh committed Oct 29, 2015
1 parent c26cf3b commit 2ebd0be
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 96 deletions.
39 changes: 18 additions & 21 deletions pe.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import sys
import sys, gc
class Editor:
KEYMAP = {
b"\x1b[A" : 0x0b,
Expand Down Expand Up @@ -30,17 +30,17 @@ class Editor:
b"\x15" : 0x15,
b"\x1b[Z" : 0x15,
b"\x18" : 0x18,
b"\x1b[3;5~": 0x18,
b"\x16" : 0x16,
b"\x04" : 0x04,
b"\x12" : 0x12,
b"\x1b[M" : 0x1b,
b"\x01" : 0x01,
b"\x14" : 0x02,
b"\x1b[1;5H": 0x02,
b"\x02" : 0x14,
b"\x1b[1;5H": 0x02,
b"\x1b[1;5F": 0x14,
b"\x12" : 0x12,
b"\x0f" : 0x1e,
b"\x1b[3;5~": 0x18,
}
def __init__(self, tab_size, undo_limit):
self.top_line = 0
Expand Down Expand Up @@ -76,7 +76,7 @@ def rd():
while not Editor.serialcomm.any():
pass
return Editor.serialcomm.read(1)
def init_tty(self, device, baud, fd_tty):
def init_tty(self, device, baud):
import pyb
Editor.sdev = device
if Editor.sdev:
Expand Down Expand Up @@ -128,6 +128,7 @@ def scroll_down(self, scrolling):
self.goto(self.height - 1, 0)
Editor.wr("\x1bD " * scrolling)
def set_screen_parms(self):
self.cursor(False)
Editor.wr('\x1b[999;999H\x1b[6n')
pos = b''
char = Editor.rd()
Expand All @@ -136,7 +137,7 @@ def set_screen_parms(self):
char = Editor.rd()
(self.height, self.width) = [int(i, 10) for i in pos.split(b';')]
self.height -= 1
self.scrbuf = ["\x04"] * self.height
self.scrbuf = ["\x01"] * self.height
self.scroll_region(self.height)
def get_input(self):
while True:
Expand Down Expand Up @@ -268,12 +269,14 @@ def handle_cursor_keys(self, key):
self.col -= 1
elif self.cur_line > 0:
self.cur_line -= 1
if self.cur_line < self.top_line:
self.scroll_up(1)
self.col = len(self.content[self.cur_line])
elif key == 0x0f:
if self.col < len(self.content[self.cur_line]):
self.col += 1
elif self.cur_line < self.total_lines - 1:
self.cur_line += 1
self.cursor_down()
self.col = 0
elif key == 0x10:
ns = self.spaces(self.content[self.cur_line])
Expand Down Expand Up @@ -433,14 +436,12 @@ def handle_edit_key(self, key):
self.undo_add(self.cur_line, None, 0, -len(content))
self.content[self.cur_line:self.cur_line] = content
self.total_lines = len(self.content)
del content
self.changed = "*"
elif key == 0x18:
self.undo_add(self.cur_line, [l], 0, 0)
if key == self.lastkey:
self.yank_buffer.append(l)
else:
del self.yank_buffer
self.yank_buffer = [l]
if self.total_lines > 1:
del self.content[self.cur_line]
Expand All @@ -454,7 +455,6 @@ def handle_edit_key(self, key):
if key == self.lastkey:
self.yank_buffer.append(l)
else:
del self.yank_buffer
self.yank_buffer = [l]
self.cursor_down()
elif key == 0x16:
Expand Down Expand Up @@ -503,6 +503,8 @@ def handle_edit_key(self, key):
self.col += 1
self.changed = '*'
def edit_loop(self):
if len(self.content) == 0:
self.content = [""]
self.total_lines = len(self.content)
self.set_screen_parms()
self.mouse_reporting(True)
Expand All @@ -521,9 +523,11 @@ def edit_loop(self):
self.clear_to_eol()
return None
elif key == 0x05:
del self.scrbuf
self.set_screen_parms()
self.row = min(self.height - 1, self.row)
if sys.implementation.name == "micropython":
gc.collect()
self.message = "%d Bytes Memory available" % gc.mem_free()
elif self.handle_cursor_keys(key):
pass
else: self.handle_edit_key(key)
Expand Down Expand Up @@ -564,29 +568,22 @@ def get_file(fname):
except Exception as err:
message = 'Could not load %s, Error: %s' % (fname, err)
return (None, message)
else:
if not content:
content = [""]
for i in range(len(content)):
content[i] = Editor.expandtabs(content[i].rstrip('\r\n\t '))
return (content, "")
def pye(content = None, tab_size = 4, undo = 50, device = 0, baud = 115200, fd_tty = 0):
def pye(content = None, tab_size = 4, undo = 50, device = 0, baud = 115200):
e = Editor(tab_size, undo)
if type(content) == str and content:
e.fname = content
(e.content, e.message) = e.get_file(e.fname)
if not e.content:
if e.content == None:
print (e.message)
del e
return
elif type(content) == list and len(content) > 0 and type(content[0]) == str:

e.content = content
if fd_tty:
e.fname = ""
e.init_tty(device, baud, fd_tty)
e.init_tty(device, baud)
e.edit_loop()
e.deinit_tty()
content = e.content if (e.fname == None) else e.fname
del e
return content
43 changes: 26 additions & 17 deletions pemin.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import sys
import sys, gc
class Editor:
KEYMAP = {
b"\x1b[A" : 0x0b,
Expand Down Expand Up @@ -30,8 +30,17 @@ class Editor:
b"\x15" : 0x15,
b"\x1b[Z" : 0x15,
b"\x18" : 0x18,
b"\x1b[3;5~": 0x18,
b"\x16" : 0x16,
b"\x04" : 0x04,
b"\x12" : 0x12,
b"\x1b[M" : 0x1b,
b"\x01" : 0x01,
b"\x14" : 0x02,
b"\x02" : 0x14,
b"\x1b[1;5H": 0x02,
b"\x1b[1;5F": 0x14,
b"\x0f" : 0x1e,
}
def __init__(self, tab_size, undo_limit):
self.top_line = 0
Expand Down Expand Up @@ -65,7 +74,7 @@ def rd():
while not Editor.serialcomm.any():
pass
return Editor.serialcomm.read(1)
def init_tty(self, device, baud, fd_tty):
def init_tty(self, device, baud):
import pyb
Editor.sdev = device
if Editor.sdev:
Expand Down Expand Up @@ -111,6 +120,7 @@ def scroll_down(self, scrolling):
self.goto(self.height - 1, 0)
Editor.wr("\x1bD " * scrolling)
def set_screen_parms(self):
self.cursor(False)
Editor.wr('\x1b[999;999H\x1b[6n')
pos = b''
char = Editor.rd()
Expand All @@ -119,7 +129,7 @@ def set_screen_parms(self):
char = Editor.rd()
(self.height, self.width) = [int(i, 10) for i in pos.split(b';')]
self.height -= 1
self.scrbuf = ["\x04"] * self.height
self.scrbuf = ["\x01"] * self.height
self.scroll_region(self.height)
def get_input(self):
while True:
Expand Down Expand Up @@ -239,12 +249,14 @@ def handle_cursor_keys(self, key):
self.col -= 1
elif self.cur_line > 0:
self.cur_line -= 1
if self.cur_line < self.top_line:
self.scroll_up(1)
self.col = len(self.content[self.cur_line])
elif key == 0x0f:
if self.col < len(self.content[self.cur_line]):
self.col += 1
elif self.cur_line < self.total_lines - 1:
self.cur_line += 1
self.cursor_down()
self.col = 0
elif key == 0x10:
ns = self.spaces(self.content[self.cur_line])
Expand Down Expand Up @@ -332,7 +344,6 @@ def handle_edit_key(self, key):
if key == self.lastkey:
self.yank_buffer.append(l)
else:
del self.yank_buffer
self.yank_buffer = [l]
if self.total_lines > 1:
del self.content[self.cur_line]
Expand All @@ -346,7 +357,6 @@ def handle_edit_key(self, key):
if key == self.lastkey:
self.yank_buffer.append(l)
else:
del self.yank_buffer
self.yank_buffer = [l]
self.cursor_down()
elif key == 0x16:
Expand Down Expand Up @@ -386,12 +396,16 @@ def handle_edit_key(self, key):
self.total_lines = len(self.content)
if len(self.undo) == 0:
self.changed = self.sticky_c
elif key < 0x20:
self.message = "Sorry, command not supported"
elif key >= 0x20:
self.undo_add(self.cur_line, [l], 0x20 if key == 0x20 else 0x41)
self.content[self.cur_line] = l[:self.col] + chr(key) + l[self.col:]
self.col += 1
self.changed = '*'
def edit_loop(self):
if len(self.content) == 0:
self.content = [""]
self.total_lines = len(self.content)
self.set_screen_parms()
while True:
Expand All @@ -408,9 +422,11 @@ def edit_loop(self):
self.clear_to_eol()
return None
elif key == 0x05:
del self.scrbuf
self.set_screen_parms()
self.row = min(self.height - 1, self.row)
if sys.implementation.name == "micropython":
gc.collect()
self.message = "%d Bytes Memory available" % gc.mem_free()
elif self.handle_cursor_keys(key):
pass
else: self.handle_edit_key(key)
Expand Down Expand Up @@ -439,29 +455,22 @@ def get_file(fname):
except Exception as err:
message = 'Could not load %s, Error: %s' % (fname, err)
return (None, message)
else:
if not content:
content = [""]
for i in range(len(content)):
content[i] = Editor.expandtabs(content[i].rstrip('\r\n\t '))
return (content, "")
def pye(content = None, tab_size = 4, undo = 50, device = 0, baud = 115200, fd_tty = 0):
def pye(content = None, tab_size = 4, undo = 50, device = 0, baud = 115200):
e = Editor(tab_size, undo)
if type(content) == str and content:
e.fname = content
(e.content, e.message) = e.get_file(e.fname)
if not e.content:
if e.content == None:
print (e.message)
del e
return
elif type(content) == list and len(content) > 0 and type(content[0]) == str:

e.content = content
if fd_tty:
e.fname = ""
e.init_tty(device, baud, fd_tty)
e.init_tty(device, baud)
e.edit_loop()
e.deinit_tty()
content = e.content if (e.fname == None) else e.fname
del e
return content
Loading

0 comments on commit 2ebd0be

Please sign in to comment.