From 021181cfb0702cc2e8dd8fc2a1b461555511c21f Mon Sep 17 00:00:00 2001 From: Robert HH Date: Sun, 1 Nov 2015 12:05:13 +0100 Subject: [PATCH] Show available memory in REDRAW; try to recover from Memory Error --- pe.py | 79 ++++++++++++++++++++++-------------------- pemin.py | 74 ++++++++++++++++++++------------------- pye.py | 103 ++++++++++++++++++++++++++++++------------------------- wipye.py | 74 ++++++++++++++++++++------------------- 4 files changed, 175 insertions(+), 155 deletions(-) diff --git a/pe.py b/pe.py index c7e4263..eb51ea9 100644 --- a/pe.py +++ b/pe.py @@ -76,7 +76,8 @@ 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: @@ -84,7 +85,8 @@ def init_tty(self, 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 @@ -179,13 +181,13 @@ 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() @@ -193,13 +195,12 @@ def display_window(self): 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: @@ -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 @@ -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() @@ -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): @@ -560,8 +565,7 @@ 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() @@ -569,7 +573,7 @@ def get_file(fname): 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) @@ -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 diff --git a/pemin.py b/pemin.py index c506c6c..e574c14 100644 --- a/pemin.py +++ b/pemin.py @@ -74,7 +74,8 @@ 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: @@ -82,7 +83,8 @@ def init_tty(self, 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 @@ -161,13 +163,13 @@ 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() @@ -175,13 +177,12 @@ def display_window(self): 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: @@ -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 @@ -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() @@ -447,8 +453,7 @@ 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() @@ -456,7 +461,7 @@ def get_file(fname): 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) @@ -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 diff --git a/pye.py b/pye.py index 02c1652..aea1035 100644 --- a/pye.py +++ b/pye.py @@ -174,16 +174,18 @@ def rd(): Editor.winch = False return b'\x05' - def init_tty(self, device, baud): - self.org_termios = termios.tcgetattr(device) + @staticmethod + def init_tty(device, baud): + Editor.org_termios = termios.tcgetattr(device) tty.setraw(device) Editor.sdev = device if sys.implementation.name == "cpython": signal.signal(signal.SIGWINCH, Editor.signal_handler) - def deinit_tty(self): + @staticmethod + def deinit_tty(): import termios - termios.tcsetattr(Editor.sdev, termios.TCSANOW, self.org_termios) + termios.tcsetattr(Editor.sdev, termios.TCSANOW, Editor.org_termios) @staticmethod def signal_handler(sig, frame): @@ -207,7 +209,8 @@ def rd(): 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: @@ -216,7 +219,8 @@ def init_tty(self, device, baud): 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) #endif @@ -244,7 +248,8 @@ def rd(): return ch.encode() except: pass - def init_tty(self, device, baud): + @staticmethod + def init_tty(device, baud): import machine if device: Editor.serialcomm = machine.UART(device - 1, baud) @@ -252,7 +257,8 @@ def init_tty(self, device, baud): Editor.serialcomm = sys.stdout Editor.sdev = device - def deinit_tty(self): + @staticmethod + def deinit_tty(): pass #endif @staticmethod @@ -365,13 +371,13 @@ def display_window(self): ## Update window and status line for c in range(self.height): if i == self.total_lines: ## at empty bottom screen part 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]: ## line changed, print it - Editor.goto(c, 0) + self.goto(c, 0) self.wr(l) if len(l) < self.width: self.clear_to_eol() @@ -380,14 +386,13 @@ def display_window(self): ## Update window and status line ## display Status-Line self.goto(self.height, 0) self.hilite(1) - self.clear_to_eol() ## moved up for mate/xfce4-terminal issue with scroll region 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() ## once moved up for mate/xfce4-terminal issue with scroll region self.goto(self.row, self.col - self.margin) + self.cursor(True) - @staticmethod - def spaces(line, pos = None): ## count spaces + def spaces(self, line, pos = None): ## count spaces if pos == None: ## at line start return len(line) - len(line.lstrip(" ")) else: ## left to the cursor @@ -463,9 +468,9 @@ def handle_cursor_keys(self, key): ## keys which move, sanity checks later 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 == KEY_RIGHT: if self.col < len(self.content[self.cur_line]): self.col += 1 @@ -726,36 +731,43 @@ def edit_loop(self): ## main editing loop key = self.get_input() ## Get Char of Fct-key code self.message = '' ## clear message - if key == KEY_QUIT: - if self.changed != ' ': - res = self.line_edit("Content changed! Quit without saving (y/N)? ", "N") - if not res or res[0].upper() != 'Y': - continue + try: + if key == KEY_QUIT: + if self.changed != ' ': + res = self.line_edit("Content changed! Quit without saving (y/N)? ", "N") + if not res or res[0].upper() != 'Y': + continue ## Do not leave cursor in the middle of screen #ifndef BASIC - self.mouse_reporting(False) ## disable mouse reporting, enable scrolling + self.mouse_reporting(False) ## disable mouse reporting, enable scrolling #endif - self.scroll_region(0) - self.goto(self.height, 0) - self.clear_to_eol() - return None - elif key == KEY_REDRAW: - self.set_screen_parms() - self.row = min(self.height - 1, self.row) + self.scroll_region(0) + self.goto(self.height, 0) + self.clear_to_eol() + return None + elif key == KEY_REDRAW: + self.set_screen_parms() + self.row = min(self.height - 1, self.row) #ifdef LINUX - if sys.platform in ("linux", "darwin") and sys.implementation.name == "cpython": - signal.signal(signal.SIGWINCH, Editor.signal_handler) + if sys.platform in ("linux", "darwin") and sys.implementation.name == "cpython": + signal.signal(signal.SIGWINCH, Editor.signal_handler) #endif - 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 + 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!" + + ## expandtabs: hopefully sometimes replaced by the built-in function - @staticmethod - def expandtabs(s): + def expandtabs(self, s): from _io import StringIO if '\t' in s: sb = StringIO() @@ -772,8 +784,7 @@ def expandtabs(s): return s ## packtabs: replace sequence of space by tab #ifndef BASIC - @staticmethod - def packtabs(s): + def packtabs(self, s): from _io import StringIO sb = StringIO() for i in range(0, len(s), 8): @@ -785,8 +796,7 @@ def packtabs(s): sb.write(c) return sb.getvalue() #endif - @staticmethod - def get_file(fname): + def get_file(self, fname): try: #ifdef LINUX if sys.implementation.name == "cpython": @@ -800,7 +810,7 @@ def get_file(fname): message = 'Could not load %s, Error: %s' % (fname, err) return (None, message) for i in range(len(content)): ## strip and convert - 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): @@ -819,9 +829,8 @@ def pye(content = None, tab_size = 4, undo = 50, device = 0, baud = 115200): e.init_tty(device, baud) e.edit_loop() e.deinit_tty() -## clean-up - content = e.content if (e.fname == None) else e.fname - return content +## close + return e.content if (e.fname == None) else e.fname #ifdef LINUX if __name__ == "__main__": diff --git a/wipye.py b/wipye.py index a6bbd8a..6323e39 100644 --- a/wipye.py +++ b/wipye.py @@ -82,14 +82,16 @@ def rd(): if ch != "\x00": return ch.encode() except: pass - def init_tty(self, device, baud): + @staticmethod + def init_tty(device, baud): import machine if device: Editor.serialcomm = machine.UART(device - 1, baud) else: Editor.serialcomm = sys.stdout Editor.sdev = device - def deinit_tty(self): + @staticmethod + def deinit_tty(): pass @staticmethod def goto(row, col): @@ -167,13 +169,13 @@ 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() @@ -181,13 +183,12 @@ def display_window(self): 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: @@ -255,9 +256,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 @@ -418,27 +419,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() @@ -453,8 +459,7 @@ 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() @@ -462,7 +467,7 @@ def get_file(fname): 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) @@ -478,5 +483,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