Skip to content

Commit 9bd3f09

Browse files
committed
Further cleaning and some slight improvements
1 parent 0e0cfe3 commit 9bd3f09

File tree

11 files changed

+394
-385
lines changed

11 files changed

+394
-385
lines changed

Pyboard Editor.doc

1.5 KB
Binary file not shown.

Pyboard Editor.pdf

41.6 KB
Binary file not shown.

README.md

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ a) find_in_file() supporting regular expressions,
2929
b) line_edit() supporting the cursor left/right/home/end keys, and
3030
c) expandtabs() and packtabs() with a second argument for tabsize (not for pye, but maybe useful)
3131
- 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.
32-
- 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.
32+
- 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.
3333

3434
**Short Version History**
3535

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

152152
**2.1** Some shrinking for WiPy
153-
- Make Indent/Un-Indent optional in the WiPy version, to allow all variants to get compile w/o running out of memory.
153+
- Make Indent/Un-Indent optional in the WiPy version, to allow all variants to get compiled w/o running out of memory.
154154
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.
155155
- Fixing a glitch which added an extra line when un-doing the delete of all lines
156156
- Some shifting around of code lines
157157
- Making the MOUSE support an extra option
158158
- Removed the extra indent after ':' as the last char on the line. More confusing than helpful.
159159
- Update of the doc file
160160

161+
**2.2** Further cleaning and some slight improvements
162+
- Moved error catching one level up to the function pye(), catching load-file errors too.
163+
- If open file names a directory, the list of files is loaded to the edit buffer.
164+
- Ctrl-V in line edit mode gets the first line of the paste buffer.
165+
- 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.
166+
- And of course: update of the doc file
167+

pe.py

Lines changed: 51 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,7 @@ def redraw(self, flag):
119119
self.mouse_reporting(True)
120120
if sys.implementation.name == "micropython":
121121
gc.collect()
122-
if flag:
123-
self.message = "{} Bytes Memory available".format(gc.mem_free())
122+
if flag: self.message = "{} Bytes Memory available".format(gc.mem_free())
124123
def get_input(self):
125124
while True:
126125
in_buffer = self.rd()
@@ -214,6 +213,11 @@ def line_edit(self, prompt, default):
214213
elif key == 0x7f:
215214
self.wr('\b \b' * len(res))
216215
res = ''
216+
elif key == 0x16:
217+
if Editor.yank_buffer:
218+
self.wr('\b \b' * len(res))
219+
res = Editor.yank_buffer[0].strip()[:len(prompt) + Editor.width - 2]
220+
self.wr(res)
217221
elif 0x20 <= key < 0xfff0:
218222
if len(prompt) + len(res) < Editor.width - 2:
219223
res += chr(key)
@@ -475,17 +479,15 @@ def handle_edit_keys(self, key):
475479
self.cur_line = cur_line
476480
self.message = "'{}' replaced {} times".format(pat, count)
477481
elif key == 0x18:
478-
if self.mark != None:
479-
self.delete_lines(True)
482+
if self.mark != None: self.delete_lines(True)
480483
elif key == 0x04:
481484
if self.mark != None:
482485
lrange = self.line_range()
483486
Editor.yank_buffer = self.content[lrange[0]:lrange[1]]
484487
self.mark = None
485488
elif key == 0x16:
486489
if Editor.yank_buffer:
487-
if self.mark != None:
488-
self.delete_lines(False)
490+
if self.mark != None: self.delete_lines(False)
489491
self.undo_add(self.cur_line, None, 0, -len(Editor.yank_buffer))
490492
self.content[self.cur_line:self.cur_line] = Editor.yank_buffer
491493
self.total_lines += len(Editor.yank_buffer)
@@ -510,8 +512,7 @@ def handle_edit_keys(self, key):
510512
else:
511513
del self.content[action[0]:action[0] - action[1]]
512514
self.total_lines = len(self.content)
513-
if len(self.undo) == self.undo_zero:
514-
self.changed = ''
515+
if len(self.undo) == self.undo_zero: self.changed = ''
515516
self.mark = None
516517
elif key == 0x05:
517518
self.redraw(True)
@@ -521,48 +522,44 @@ def edit_loop(self):
521522
self.total_lines = len(self.content)
522523
self.redraw(self.message == "")
523524
while True:
524-
try:
525-
if not self.rd_any():
526-
self.display_window()
527-
key = self.get_input()
528-
self.message = ''
529-
if key == 0x11:
530-
if self.changed:
531-
res = self.line_edit("Content changed! Quit without saving (y/N)? ", "N")
532-
if not res or res[0].upper() != 'Y':
533-
continue
534-
self.mouse_reporting(False)
535-
self.goto(Editor.height, 0)
536-
self.clear_to_eol()
537-
self.undo = []
538-
return key
539-
elif key in (0x17, 0x0f):
540-
return key
541-
else: self.handle_edit_keys(key)
542-
except Exception as err:
543-
self.message = "{!r}".format(err)
525+
if not self.rd_any():
526+
self.display_window()
527+
key = self.get_input()
528+
self.message = ''
529+
if key == 0x11:
530+
if self.changed:
531+
res = self.line_edit("Content changed! Quit without saving (y/N)? ", "N")
532+
if not res or res[0].upper() != 'Y':
533+
continue
534+
self.mouse_reporting(False)
535+
self.goto(Editor.height, 0)
536+
self.clear_to_eol()
537+
self.undo = []
538+
return key
539+
elif key in (0x17, 0x0f):
540+
return key
541+
else: self.handle_edit_keys(key)
544542
def packtabs(self, s):
545543
from _io import StringIO
546544
sb = StringIO()
547545
for i in range(0, len(s), 8):
548546
c = s[i:i + 8]
549547
cr = c.rstrip(" ")
550-
if c != cr:
551-
sb.write(cr + "\t")
552-
else:
553-
sb.write(c)
548+
if c != cr: sb.write(cr + "\t")
549+
else: sb.write(c)
554550
return sb.getvalue()
555551
def get_file(self, fname):
552+
import os
556553
if not fname:
557554
fname = self.line_edit("Open file: ", "")
558555
if fname:
559-
self.fname = fname
560-
try:
556+
if (os.stat(fname)[0] & 0x4000):
557+
self.content = sorted(os.listdir(fname))
558+
else:
559+
self.fname = fname
560+
if True:
561561
with open(fname) as f:
562562
self.content = f.readlines()
563-
except Exception as err:
564-
self.content, self.message = [""], "{!r}".format(err)
565-
else:
566563
for i in range(len(self.content)):
567564
self.content[i] = expandtabs(self.content[i].rstrip('\r\n\t '))
568565
def put_file(self, fname):
@@ -593,33 +590,34 @@ def expandtabs(s):
593590
return s
594591
def pye(*content, tab_size = 4, undo = 50, device = 0, baud = 115200):
595592
gc.collect()
593+
slot = [Editor(tab_size, undo)]
596594
if content:
597-
slot = []
598595
index = 0
599596
for f in content:
600-
slot.append(Editor(tab_size, undo))
597+
if index: slot.append(Editor(tab_size, undo))
601598
if type(f) == str and f:
602599
slot[index].get_file(f)
603600
elif type(f) == list and len(f) > 0 and type(f[0]) == str:
604601
slot[index].content = f
605602
index += 1
606-
else:
607-
slot = [Editor(tab_size, undo)]
608603
Editor.init_tty(device, baud)
609604
index = 0
610605
while True:
611-
index %= len(slot)
612-
key = slot[index].edit_loop()
613-
if key == 0x11:
614-
if len(slot) == 1:
615-
break
616-
del slot[index]
617-
elif key == 0x0f:
618-
slot.append(Editor(tab_size, undo))
619-
index = len(slot) - 1
620-
slot[index].get_file(None)
621-
elif key == 0x17:
622-
index += 1
606+
try:
607+
index %= len(slot)
608+
key = slot[index].edit_loop()
609+
if key == 0x11:
610+
if len(slot) == 1:
611+
break
612+
del slot[index]
613+
elif key == 0x0f:
614+
slot.append(Editor(tab_size, undo))
615+
index = len(slot) - 1
616+
slot[index].get_file(None)
617+
elif key == 0x17:
618+
index += 1
619+
except Exception as err:
620+
slot[index].message = "{!r}".format(err)
623621
Editor.deinit_tty()
624622
Editor.yank_buffer = []
625623
return slot[0].content if (slot[0].fname == "") else slot[0].fname

pe2.py

Lines changed: 51 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,7 @@ def redraw(self, flag):
119119
self.mouse_reporting(True)
120120
if sys.implementation.name == "micropython":
121121
gc.collect()
122-
if flag:
123-
self.message = "{} Bytes Memory available".format(gc.mem_free())
122+
if flag: self.message = "{} Bytes Memory available".format(gc.mem_free())
124123
def get_input(self):
125124
while True:
126125
in_buffer = self.rd()
@@ -213,6 +212,11 @@ def line_edit(self, prompt, default):
213212
elif key == 0x7f:
214213
self.wr('\b \b' * len(res))
215214
res = ''
215+
elif key == 0x16:
216+
if Editor.yank_buffer:
217+
self.wr('\b \b' * len(res))
218+
res = Editor.yank_buffer[0].strip()[:len(prompt) + Editor.width - 2]
219+
self.wr(res)
216220
elif 0x20 <= key < 0xfff0:
217221
if len(prompt) + len(res) < Editor.width - 2:
218222
res += chr(key)
@@ -483,17 +487,15 @@ def handle_edit_keys(self, key):
483487
self.cur_line = cur_line
484488
self.message = "'{}' replaced {} times".format(pat, count)
485489
elif key == 0x18:
486-
if self.mark != None:
487-
self.delete_lines(True)
490+
if self.mark != None: self.delete_lines(True)
488491
elif key == 0x04:
489492
if self.mark != None:
490493
lrange = self.line_range()
491494
Editor.yank_buffer = self.content[lrange[0]:lrange[1]]
492495
self.mark = None
493496
elif key == 0x16:
494497
if Editor.yank_buffer:
495-
if self.mark != None:
496-
self.delete_lines(False)
498+
if self.mark != None: self.delete_lines(False)
497499
self.undo_add(self.cur_line, None, 0, -len(Editor.yank_buffer))
498500
self.content[self.cur_line:self.cur_line] = Editor.yank_buffer
499501
self.total_lines += len(Editor.yank_buffer)
@@ -518,8 +520,7 @@ def handle_edit_keys(self, key):
518520
else:
519521
del self.content[action[0]:action[0] - action[1]]
520522
self.total_lines = len(self.content)
521-
if len(self.undo) == self.undo_zero:
522-
self.changed = ''
523+
if len(self.undo) == self.undo_zero: self.changed = ''
523524
self.mark = None
524525
elif key == 0x05:
525526
self.redraw(True)
@@ -529,48 +530,44 @@ def edit_loop(self):
529530
self.total_lines = len(self.content)
530531
self.redraw(self.message == "")
531532
while True:
532-
try:
533-
if not self.rd_any():
534-
self.display_window()
535-
key = self.get_input()
536-
self.message = ''
537-
if key == 0x11:
538-
if self.changed:
539-
res = self.line_edit("Content changed! Quit without saving (y/N)? ", "N")
540-
if not res or res[0].upper() != 'Y':
541-
continue
542-
self.mouse_reporting(False)
543-
self.goto(Editor.height, 0)
544-
self.clear_to_eol()
545-
self.undo = []
546-
return key
547-
elif key in (0x17, 0x0f):
548-
return key
549-
else: self.handle_edit_keys(key)
550-
except Exception as err:
551-
self.message = "{!r}".format(err)
533+
if not self.rd_any():
534+
self.display_window()
535+
key = self.get_input()
536+
self.message = ''
537+
if key == 0x11:
538+
if self.changed:
539+
res = self.line_edit("Content changed! Quit without saving (y/N)? ", "N")
540+
if not res or res[0].upper() != 'Y':
541+
continue
542+
self.mouse_reporting(False)
543+
self.goto(Editor.height, 0)
544+
self.clear_to_eol()
545+
self.undo = []
546+
return key
547+
elif key in (0x17, 0x0f):
548+
return key
549+
else: self.handle_edit_keys(key)
552550
def packtabs(self, s):
553551
from _io import StringIO
554552
sb = StringIO()
555553
for i in range(0, len(s), 8):
556554
c = s[i:i + 8]
557555
cr = c.rstrip(" ")
558-
if c != cr:
559-
sb.write(cr + "\t")
560-
else:
561-
sb.write(c)
556+
if c != cr: sb.write(cr + "\t")
557+
else: sb.write(c)
562558
return sb.getvalue()
563559
def get_file(self, fname):
560+
import os
564561
if not fname:
565562
fname = self.line_edit("Open file: ", "")
566563
if fname:
567-
self.fname = fname
568-
try:
564+
if (os.stat(fname)[0] & 0x4000):
565+
self.content = sorted(os.listdir(fname))
566+
else:
567+
self.fname = fname
568+
if True:
569569
with open(fname) as f:
570570
self.content = f.readlines()
571-
except Exception as err:
572-
self.content, self.message = [""], "{!r}".format(err)
573-
else:
574571
for i in range(len(self.content)):
575572
self.content[i] = expandtabs(self.content[i].rstrip('\r\n\t '))
576573
def put_file(self, fname):
@@ -601,33 +598,34 @@ def expandtabs(s):
601598
return s
602599
def pye(*content, tab_size = 4, undo = 50, device = 0, baud = 115200):
603600
gc.collect()
601+
slot = [Editor(tab_size, undo)]
604602
if content:
605-
slot = []
606603
index = 0
607604
for f in content:
608-
slot.append(Editor(tab_size, undo))
605+
if index: slot.append(Editor(tab_size, undo))
609606
if type(f) == str and f:
610607
slot[index].get_file(f)
611608
elif type(f) == list and len(f) > 0 and type(f[0]) == str:
612609
slot[index].content = f
613610
index += 1
614-
else:
615-
slot = [Editor(tab_size, undo)]
616611
Editor.init_tty(device, baud)
617612
index = 0
618613
while True:
619-
index %= len(slot)
620-
key = slot[index].edit_loop()
621-
if key == 0x11:
622-
if len(slot) == 1:
623-
break
624-
del slot[index]
625-
elif key == 0x0f:
626-
slot.append(Editor(tab_size, undo))
627-
index = len(slot) - 1
628-
slot[index].get_file(None)
629-
elif key == 0x17:
630-
index += 1
614+
try:
615+
index %= len(slot)
616+
key = slot[index].edit_loop()
617+
if key == 0x11:
618+
if len(slot) == 1:
619+
break
620+
del slot[index]
621+
elif key == 0x0f:
622+
slot.append(Editor(tab_size, undo))
623+
index = len(slot) - 1
624+
slot[index].get_file(None)
625+
elif key == 0x17:
626+
index += 1
627+
except Exception as err:
628+
slot[index].message = "{!r}".format(err)
631629
Editor.deinit_tty()
632630
Editor.yank_buffer = []
633631
return slot[0].content if (slot[0].fname == "") else slot[0].fname

0 commit comments

Comments
 (0)