Skip to content

Commit

Permalink
Show available memory in REDRAW; try to recover from Memory Error
Browse files Browse the repository at this point in the history
  • Loading branch information
robert-hh committed Nov 1, 2015
1 parent b820e1f commit 021181c
Show file tree
Hide file tree
Showing 4 changed files with 175 additions and 155 deletions.
79 changes: 41 additions & 38 deletions pe.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,15 +76,17 @@ def rd():
while not Editor.serialcomm.any():
pass
return Editor.serialcomm.read(1)
def init_tty(self, device, baud):
@staticmethod
def init_tty(device, baud):
import pyb
Editor.sdev = device
if Editor.sdev:
Editor.serialcomm = pyb.UART(device, baud)
else:
Editor.serialcomm = pyb.USB_VCP()
Editor.serialcomm.setinterrupt(-1)
def deinit_tty(self):
@staticmethod
def deinit_tty():
if not Editor.sdev:
Editor.serialcomm.setinterrupt(3)
@staticmethod
Expand Down Expand Up @@ -179,27 +181,26 @@ def display_window(self):
for c in range(self.height):
if i == self.total_lines:
if self.scrbuf[c] != '':
Editor.goto(c, 0)
self.goto(c, 0)
self.clear_to_eol()
self.scrbuf[c] = ''
else:
l = self.content[i][self.margin:self.margin + self.width]
if l != self.scrbuf[c]:
Editor.goto(c, 0)
self.goto(c, 0)
self.wr(l)
if len(l) < self.width:
self.clear_to_eol()
self.scrbuf[c] = l
i += 1
self.goto(self.height, 0)
self.hilite(1)
self.clear_to_eol()
self.wr("[%d] %c Row: %d Col: %d %s" % (self.total_lines, self.changed, self.cur_line + 1, self.col + 1, self.message[:self.width - 25]))
self.hilite(0)
self.cursor(True)
self.clear_to_eol()
self.goto(self.row, self.col - self.margin)
@staticmethod
def spaces(line, pos = None):
self.cursor(True)
def spaces(self, line, pos = None):
if pos == None:
return len(line) - len(line.lstrip(" "))
else:
Expand Down Expand Up @@ -269,9 +270,9 @@ def handle_cursor_keys(self, key):
self.col -= 1
elif self.cur_line > 0:
self.cur_line -= 1
self.col = len(self.content[self.cur_line])
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
Expand Down Expand Up @@ -512,28 +513,33 @@ def edit_loop(self):
self.display_window()
key = self.get_input()
self.message = ''
if key == 0x03:
if self.changed != ' ':
res = self.line_edit("Content changed! Quit without saving (y/N)? ", "N")
if not res or res[0].upper() != 'Y':
continue
self.mouse_reporting(False)
self.scroll_region(0)
self.goto(self.height, 0)
self.clear_to_eol()
return None
elif key == 0x05:
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)
self.lastkey = key
@staticmethod
def expandtabs(s):
try:
if key == 0x03:
if self.changed != ' ':
res = self.line_edit("Content changed! Quit without saving (y/N)? ", "N")
if not res or res[0].upper() != 'Y':
continue
self.mouse_reporting(False)
self.scroll_region(0)
self.goto(self.height, 0)
self.clear_to_eol()
return None
elif key == 0x05:
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)
self.lastkey = key
except MemoryError:
del self.undo[:]
del self.yank_buffer[:]
gc.collect()
self.message ="Memory Error. Undo and Yank cleared!"
def expandtabs(self, s):
from _io import StringIO
if '\t' in s:
sb = StringIO()
Expand All @@ -548,8 +554,7 @@ def expandtabs(s):
return sb.getvalue()
else:
return s
@staticmethod
def packtabs(s):
def packtabs(self, s):
from _io import StringIO
sb = StringIO()
for i in range(0, len(s), 8):
Expand All @@ -560,16 +565,15 @@ def packtabs(s):
else:
sb.write(c)
return sb.getvalue()
@staticmethod
def get_file(fname):
def get_file(self, fname):
try:
with open(fname) as f:
content = f.readlines()
except Exception as err:
message = 'Could not load %s, Error: %s' % (fname, err)
return (None, message)
for i in range(len(content)):
content[i] = Editor.expandtabs(content[i].rstrip('\r\n\t '))
content[i] = self.expandtabs(content[i].rstrip('\r\n\t '))
return (content, "")
def pye(content = None, tab_size = 4, undo = 50, device = 0, baud = 115200):
e = Editor(tab_size, undo)
Expand All @@ -585,5 +589,4 @@ def pye(content = None, tab_size = 4, undo = 50, device = 0, baud = 115200):
e.init_tty(device, baud)
e.edit_loop()
e.deinit_tty()
content = e.content if (e.fname == None) else e.fname
return content
return e.content if (e.fname == None) else e.fname
74 changes: 39 additions & 35 deletions pemin.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,17 @@ def rd():
while not Editor.serialcomm.any():
pass
return Editor.serialcomm.read(1)
def init_tty(self, device, baud):
@staticmethod
def init_tty(device, baud):
import pyb
Editor.sdev = device
if Editor.sdev:
Editor.serialcomm = pyb.UART(device, baud)
else:
Editor.serialcomm = pyb.USB_VCP()
Editor.serialcomm.setinterrupt(-1)
def deinit_tty(self):
@staticmethod
def deinit_tty():
if not Editor.sdev:
Editor.serialcomm.setinterrupt(3)
@staticmethod
Expand Down Expand Up @@ -161,27 +163,26 @@ def display_window(self):
for c in range(self.height):
if i == self.total_lines:
if self.scrbuf[c] != '':
Editor.goto(c, 0)
self.goto(c, 0)
self.clear_to_eol()
self.scrbuf[c] = ''
else:
l = self.content[i][self.margin:self.margin + self.width]
if l != self.scrbuf[c]:
Editor.goto(c, 0)
self.goto(c, 0)
self.wr(l)
if len(l) < self.width:
self.clear_to_eol()
self.scrbuf[c] = l
i += 1
self.goto(self.height, 0)
self.hilite(1)
self.clear_to_eol()
self.wr("[%d] %c Row: %d Col: %d %s" % (self.total_lines, self.changed, self.cur_line + 1, self.col + 1, self.message[:self.width - 25]))
self.hilite(0)
self.cursor(True)
self.clear_to_eol()
self.goto(self.row, self.col - self.margin)
@staticmethod
def spaces(line, pos = None):
self.cursor(True)
def spaces(self, line, pos = None):
if pos == None:
return len(line) - len(line.lstrip(" "))
else:
Expand Down Expand Up @@ -249,9 +250,9 @@ def handle_cursor_keys(self, key):
self.col -= 1
elif self.cur_line > 0:
self.cur_line -= 1
self.col = len(self.content[self.cur_line])
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
Expand Down Expand Up @@ -412,27 +413,32 @@ def edit_loop(self):
self.display_window()
key = self.get_input()
self.message = ''
if key == 0x03:
if self.changed != ' ':
res = self.line_edit("Content changed! Quit without saving (y/N)? ", "N")
if not res or res[0].upper() != 'Y':
continue
self.scroll_region(0)
self.goto(self.height, 0)
self.clear_to_eol()
return None
elif key == 0x05:
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)
self.lastkey = key
@staticmethod
def expandtabs(s):
try:
if key == 0x03:
if self.changed != ' ':
res = self.line_edit("Content changed! Quit without saving (y/N)? ", "N")
if not res or res[0].upper() != 'Y':
continue
self.scroll_region(0)
self.goto(self.height, 0)
self.clear_to_eol()
return None
elif key == 0x05:
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)
self.lastkey = key
except MemoryError:
del self.undo[:]
del self.yank_buffer[:]
gc.collect()
self.message ="Memory Error. Undo and Yank cleared!"
def expandtabs(self, s):
from _io import StringIO
if '\t' in s:
sb = StringIO()
Expand All @@ -447,16 +453,15 @@ def expandtabs(s):
return sb.getvalue()
else:
return s
@staticmethod
def get_file(fname):
def get_file(self, fname):
try:
with open(fname) as f:
content = f.readlines()
except Exception as err:
message = 'Could not load %s, Error: %s' % (fname, err)
return (None, message)
for i in range(len(content)):
content[i] = Editor.expandtabs(content[i].rstrip('\r\n\t '))
content[i] = self.expandtabs(content[i].rstrip('\r\n\t '))
return (content, "")
def pye(content = None, tab_size = 4, undo = 50, device = 0, baud = 115200):
e = Editor(tab_size, undo)
Expand All @@ -472,5 +477,4 @@ def pye(content = None, tab_size = 4, undo = 50, device = 0, baud = 115200):
e.init_tty(device, baud)
e.edit_loop()
e.deinit_tty()
content = e.content if (e.fname == None) else e.fname
return content
return e.content if (e.fname == None) else e.fname
Loading

0 comments on commit 021181c

Please sign in to comment.