Skip to content

Commit

Permalink
Further cleaning and some slight improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
robert-hh committed Jan 10, 2016
1 parent 0e0cfe3 commit 9bd3f09
Show file tree
Hide file tree
Showing 11 changed files with 394 additions and 385 deletions.
Binary file modified Pyboard Editor.doc
Binary file not shown.
Binary file modified Pyboard Editor.pdf
Binary file not shown.
11 changes: 9 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ a) find_in_file() supporting regular expressions,
b) line_edit() supporting the cursor left/right/home/end keys, and
c) expandtabs() and packtabs() with a second argument for tabsize (not for pye, but maybe useful)
- strip.sh: sample Shell script which creates the different variants out of pye.py using cpp, including variants of wipye.py with either speed up scrolling or support replace or support got bracket.
- pye_vt.py: a variant of pye.py, where all directly screen related functions are placed into a separate class. That's a better style, however it uses more memory.
- pye_vt.py: a variant of pye.py, where all directly screen related functions are placed into a separate class. That's a better style, however it uses more memory. This file is just given as exmaple and not maintained.

**Short Version History**

Expand Down Expand Up @@ -150,11 +150,18 @@ anyhow called one after the other, resulting in a enormous long function handlin
- Ctrl-O opens a new file/buffer.

**2.1** Some shrinking for WiPy
- Make Indent/Un-Indent optional in the WiPy version, to allow all variants to get compile w/o running out of memory.
- Make Indent/Un-Indent optional in the WiPy version, to allow all variants to get compiled w/o running out of memory.
The final code saving is just a few hundred bytes, so it's still not clear to me why these few extra lines dont't fit.
- Fixing a glitch which added an extra line when un-doing the delete of all lines
- Some shifting around of code lines
- Making the MOUSE support an extra option
- Removed the extra indent after ':' as the last char on the line. More confusing than helpful.
- Update of the doc file

**2.2** Further cleaning and some slight improvements
- Moved error catching one level up to the function pye(), catching load-file errors too.
- If open file names a directory, the list of files is loaded to the edit buffer.
- Ctrl-V in line edit mode gets the first line of the paste buffer.
- The WiPy version does not support undo for Indent/Un-indent, even if Indent is enabled. It is too memory consuming at runtime. It's questionable whether this is needed at all.
- And of course: update of the doc file

104 changes: 51 additions & 53 deletions pe.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,7 @@ def redraw(self, flag):
self.mouse_reporting(True)
if sys.implementation.name == "micropython":
gc.collect()
if flag:
self.message = "{} Bytes Memory available".format(gc.mem_free())
if flag: self.message = "{} Bytes Memory available".format(gc.mem_free())
def get_input(self):
while True:
in_buffer = self.rd()
Expand Down Expand Up @@ -214,6 +213,11 @@ def line_edit(self, prompt, default):
elif key == 0x7f:
self.wr('\b \b' * len(res))
res = ''
elif key == 0x16:
if Editor.yank_buffer:
self.wr('\b \b' * len(res))
res = Editor.yank_buffer[0].strip()[:len(prompt) + Editor.width - 2]
self.wr(res)
elif 0x20 <= key < 0xfff0:
if len(prompt) + len(res) < Editor.width - 2:
res += chr(key)
Expand Down Expand Up @@ -475,17 +479,15 @@ def handle_edit_keys(self, key):
self.cur_line = cur_line
self.message = "'{}' replaced {} times".format(pat, count)
elif key == 0x18:
if self.mark != None:
self.delete_lines(True)
if self.mark != None: self.delete_lines(True)
elif key == 0x04:
if self.mark != None:
lrange = self.line_range()
Editor.yank_buffer = self.content[lrange[0]:lrange[1]]
self.mark = None
elif key == 0x16:
if Editor.yank_buffer:
if self.mark != None:
self.delete_lines(False)
if self.mark != None: self.delete_lines(False)
self.undo_add(self.cur_line, None, 0, -len(Editor.yank_buffer))
self.content[self.cur_line:self.cur_line] = Editor.yank_buffer
self.total_lines += len(Editor.yank_buffer)
Expand All @@ -510,8 +512,7 @@ def handle_edit_keys(self, key):
else:
del self.content[action[0]:action[0] - action[1]]
self.total_lines = len(self.content)
if len(self.undo) == self.undo_zero:
self.changed = ''
if len(self.undo) == self.undo_zero: self.changed = ''
self.mark = None
elif key == 0x05:
self.redraw(True)
Expand All @@ -521,48 +522,44 @@ def edit_loop(self):
self.total_lines = len(self.content)
self.redraw(self.message == "")
while True:
try:
if not self.rd_any():
self.display_window()
key = self.get_input()
self.message = ''
if key == 0x11:
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.goto(Editor.height, 0)
self.clear_to_eol()
self.undo = []
return key
elif key in (0x17, 0x0f):
return key
else: self.handle_edit_keys(key)
except Exception as err:
self.message = "{!r}".format(err)
if not self.rd_any():
self.display_window()
key = self.get_input()
self.message = ''
if key == 0x11:
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.goto(Editor.height, 0)
self.clear_to_eol()
self.undo = []
return key
elif key in (0x17, 0x0f):
return key
else: self.handle_edit_keys(key)
def packtabs(self, s):
from _io import StringIO
sb = StringIO()
for i in range(0, len(s), 8):
c = s[i:i + 8]
cr = c.rstrip(" ")
if c != cr:
sb.write(cr + "\t")
else:
sb.write(c)
if c != cr: sb.write(cr + "\t")
else: sb.write(c)
return sb.getvalue()
def get_file(self, fname):
import os
if not fname:
fname = self.line_edit("Open file: ", "")
if fname:
self.fname = fname
try:
if (os.stat(fname)[0] & 0x4000):
self.content = sorted(os.listdir(fname))
else:
self.fname = fname
if True:
with open(fname) as f:
self.content = f.readlines()
except Exception as err:
self.content, self.message = [""], "{!r}".format(err)
else:
for i in range(len(self.content)):
self.content[i] = expandtabs(self.content[i].rstrip('\r\n\t '))
def put_file(self, fname):
Expand Down Expand Up @@ -593,33 +590,34 @@ def expandtabs(s):
return s
def pye(*content, tab_size = 4, undo = 50, device = 0, baud = 115200):
gc.collect()
slot = [Editor(tab_size, undo)]
if content:
slot = []
index = 0
for f in content:
slot.append(Editor(tab_size, undo))
if index: slot.append(Editor(tab_size, undo))
if type(f) == str and f:
slot[index].get_file(f)
elif type(f) == list and len(f) > 0 and type(f[0]) == str:
slot[index].content = f
index += 1
else:
slot = [Editor(tab_size, undo)]
Editor.init_tty(device, baud)
index = 0
while True:
index %= len(slot)
key = slot[index].edit_loop()
if key == 0x11:
if len(slot) == 1:
break
del slot[index]
elif key == 0x0f:
slot.append(Editor(tab_size, undo))
index = len(slot) - 1
slot[index].get_file(None)
elif key == 0x17:
index += 1
try:
index %= len(slot)
key = slot[index].edit_loop()
if key == 0x11:
if len(slot) == 1:
break
del slot[index]
elif key == 0x0f:
slot.append(Editor(tab_size, undo))
index = len(slot) - 1
slot[index].get_file(None)
elif key == 0x17:
index += 1
except Exception as err:
slot[index].message = "{!r}".format(err)
Editor.deinit_tty()
Editor.yank_buffer = []
return slot[0].content if (slot[0].fname == "") else slot[0].fname
104 changes: 51 additions & 53 deletions pe2.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,7 @@ def redraw(self, flag):
self.mouse_reporting(True)
if sys.implementation.name == "micropython":
gc.collect()
if flag:
self.message = "{} Bytes Memory available".format(gc.mem_free())
if flag: self.message = "{} Bytes Memory available".format(gc.mem_free())
def get_input(self):
while True:
in_buffer = self.rd()
Expand Down Expand Up @@ -213,6 +212,11 @@ def line_edit(self, prompt, default):
elif key == 0x7f:
self.wr('\b \b' * len(res))
res = ''
elif key == 0x16:
if Editor.yank_buffer:
self.wr('\b \b' * len(res))
res = Editor.yank_buffer[0].strip()[:len(prompt) + Editor.width - 2]
self.wr(res)
elif 0x20 <= key < 0xfff0:
if len(prompt) + len(res) < Editor.width - 2:
res += chr(key)
Expand Down Expand Up @@ -483,17 +487,15 @@ def handle_edit_keys(self, key):
self.cur_line = cur_line
self.message = "'{}' replaced {} times".format(pat, count)
elif key == 0x18:
if self.mark != None:
self.delete_lines(True)
if self.mark != None: self.delete_lines(True)
elif key == 0x04:
if self.mark != None:
lrange = self.line_range()
Editor.yank_buffer = self.content[lrange[0]:lrange[1]]
self.mark = None
elif key == 0x16:
if Editor.yank_buffer:
if self.mark != None:
self.delete_lines(False)
if self.mark != None: self.delete_lines(False)
self.undo_add(self.cur_line, None, 0, -len(Editor.yank_buffer))
self.content[self.cur_line:self.cur_line] = Editor.yank_buffer
self.total_lines += len(Editor.yank_buffer)
Expand All @@ -518,8 +520,7 @@ def handle_edit_keys(self, key):
else:
del self.content[action[0]:action[0] - action[1]]
self.total_lines = len(self.content)
if len(self.undo) == self.undo_zero:
self.changed = ''
if len(self.undo) == self.undo_zero: self.changed = ''
self.mark = None
elif key == 0x05:
self.redraw(True)
Expand All @@ -529,48 +530,44 @@ def edit_loop(self):
self.total_lines = len(self.content)
self.redraw(self.message == "")
while True:
try:
if not self.rd_any():
self.display_window()
key = self.get_input()
self.message = ''
if key == 0x11:
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.goto(Editor.height, 0)
self.clear_to_eol()
self.undo = []
return key
elif key in (0x17, 0x0f):
return key
else: self.handle_edit_keys(key)
except Exception as err:
self.message = "{!r}".format(err)
if not self.rd_any():
self.display_window()
key = self.get_input()
self.message = ''
if key == 0x11:
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.goto(Editor.height, 0)
self.clear_to_eol()
self.undo = []
return key
elif key in (0x17, 0x0f):
return key
else: self.handle_edit_keys(key)
def packtabs(self, s):
from _io import StringIO
sb = StringIO()
for i in range(0, len(s), 8):
c = s[i:i + 8]
cr = c.rstrip(" ")
if c != cr:
sb.write(cr + "\t")
else:
sb.write(c)
if c != cr: sb.write(cr + "\t")
else: sb.write(c)
return sb.getvalue()
def get_file(self, fname):
import os
if not fname:
fname = self.line_edit("Open file: ", "")
if fname:
self.fname = fname
try:
if (os.stat(fname)[0] & 0x4000):
self.content = sorted(os.listdir(fname))
else:
self.fname = fname
if True:
with open(fname) as f:
self.content = f.readlines()
except Exception as err:
self.content, self.message = [""], "{!r}".format(err)
else:
for i in range(len(self.content)):
self.content[i] = expandtabs(self.content[i].rstrip('\r\n\t '))
def put_file(self, fname):
Expand Down Expand Up @@ -601,33 +598,34 @@ def expandtabs(s):
return s
def pye(*content, tab_size = 4, undo = 50, device = 0, baud = 115200):
gc.collect()
slot = [Editor(tab_size, undo)]
if content:
slot = []
index = 0
for f in content:
slot.append(Editor(tab_size, undo))
if index: slot.append(Editor(tab_size, undo))
if type(f) == str and f:
slot[index].get_file(f)
elif type(f) == list and len(f) > 0 and type(f[0]) == str:
slot[index].content = f
index += 1
else:
slot = [Editor(tab_size, undo)]
Editor.init_tty(device, baud)
index = 0
while True:
index %= len(slot)
key = slot[index].edit_loop()
if key == 0x11:
if len(slot) == 1:
break
del slot[index]
elif key == 0x0f:
slot.append(Editor(tab_size, undo))
index = len(slot) - 1
slot[index].get_file(None)
elif key == 0x17:
index += 1
try:
index %= len(slot)
key = slot[index].edit_loop()
if key == 0x11:
if len(slot) == 1:
break
del slot[index]
elif key == 0x0f:
slot.append(Editor(tab_size, undo))
index = len(slot) - 1
slot[index].get_file(None)
elif key == 0x17:
index += 1
except Exception as err:
slot[index].message = "{!r}".format(err)
Editor.deinit_tty()
Editor.yank_buffer = []
return slot[0].content if (slot[0].fname == "") else slot[0].fname
Loading

0 comments on commit 9bd3f09

Please sign in to comment.