2760 views|5 replies

165

Posts

0

Resources
The OP
 

Beginner MicroPython, write an editor to practice: [Copy link]

 Here is a picture of the effect first: Usage method Putty serial port settings: cmd: 1. import editor 2. editor.edit("sd.cpp") Only four direction keys are implemented, del, ctrl-s to save, ctrl-x to exit, tab input is supported but utf8 is not supported. Although the memory is tight, it is still not stressful to open a file of several KB. Note: Data has value, scripts are ruthless. No strict testing... Code:
链接已隐藏,如需查看请登录或者注册
console.py
  1. import sys class Console: def __init__(self): pass def SetColor(self,f,b): s="\033[%(fg)d;%(b g)dm"%{'fg':f,'bg':b} sys.stdout.write(s) def ClearSetting(self): s="\033[0m" sys.stdout.write(s) def ClearScreen(self): s="\033[2J" sys.stdout.write(s) def ClearToEnd(self): s="\033[K" sys.stdout.write( s) def HideCursor(self): s="\033[?25l" sys.stdout.write(s) def ShowCursor(self): s="\033[?25h" sys.stdout.write(s) def SetCursor(self,x,y): x=x+1 y=y+1 s="\033[%(x)d;%(y)dH"%{'x':x,'y':y} sys.st dout.write(s) def Write(self,s): sys.stdout.write(s) def Read(self): return sys.stdin.buffer.read(1).decode() #return sys.stdin.read(1)
复制代码
editor.py
  1. from linelist import * from console import * class FileIo: def __init__(self): self.f=None def open(self,s,opt): self.f=open( s,opt) def load(self): self.f.seek(0) return self.f.readlines() def seek(self,n): self.f.seek(n) def close(self): self.f.close() def write(self,s): self.f.write(s) def flush(self): self.f.flush() class Editor: def __init__(self): self.w=50 self.h=20 self.path=None self.console = Console() self.cursor _x = 0 self.cursor_y = 0 self.cursorToStrIndex =0; self.curLine = None; self.firstDispLine = None self.lines = LineList() self.isModified=0 self.enableDebug=0 def edit(self,s): self.console.ClearScreen() self.console.ClearSetting() self.path=s self.lines.h = None self.lines.t = None file = FileIo( ) file.open(s,"r") buf=file.load() for i in range(len(buf)): self.lines.add(buf[i]) file.close() self.lines.add("") self.curLine = self.lines.h; self.firstDispLine = self.lines.h; self.updateTitle() self.updateEdit() self.cursor_x = 0 self.cursor_y = 1 self.cursorToStrIndex =0; self.console.SetCursor(self .cursor_y,self.cursor_x) self.isModified=0 self.loop() def doSave(self): #RENAME(SAVE AS) NOT SUPPORT file = FileIo() file.open(self.path,"w") file.seek(0) it = self.lines.h while (it is not None): file.write(it.s) it = it.n file.flush() file.close() self.isModified=0 self. updateTitle() self.dbgPrint("file saved") def updateTitle(self):if(self.isModified==0): self.updateLine(0,0,self.path) else: self.updateLine(0,0,self.path+"*") def updateEdit(self): it = self.firstDispLine linenumber=0 while (it is not None): self.updateLine(linenumber+1,0,it.s) it = it.n linenumber+=1; if(linenumber==self.h): break def updateLine(self, y,x,s): self.console.SetCursor(y,x) self.console.ClearToEnd() cursor_pos=x str_pos=0 while(str_pos<len(s) and="" c="s[str_pos]" cursor_pos+="1" cursor_pos<self.w):="" elif(c="='\n'):" if(c="='\r'):" n="self.w-cursor_pos" nn="0" self.console.setcolor(30,47)="" self.console.setcolor(37,40)="" self.console.setcolor(93,47)="" self.console.write('n');="" self.console.write('r');="" str_pos+="1" while(n="">0 and nn&lt;4): self.console.Write('.') cursor_pos+=1 n-=1 nn+=1; self.console.SetColor(37,40) str_pos+=1 elif(c&lt;'\x20' or c&gt;'\x7e'): self.console.SetColor(30,47) self.console.Write('?'); self.console.SetColor(37,40) str_pos+=1 cursor_pos+=1 else: self.console .Write(c); str_pos+=1 cursor_pos+=1 def dbgPrint(self,s,n=0): if(self.enableDebug): self.console.SetCursor(self.h+2+n,0) self.console .ClearToEnd() self.updateLine(self.h+2+n,0,s) self.console.SetCursor(self.cursor_y,self.cursor_x) def loop(self): state=0 while(True): c=self.console.Read() if(state==0): if(c== '\033'): state=1 elif(c=='\x7f'): self.dbgPrint("key Delete") self.KeyDelete() elif(c=='\x08'): self.dbgPrint(" key Backspace") elif(c=='\x09'): self.dbgPrint("key Tab") self.KeyTab(); elif(c=='\x0d'): self.dbgPrint("key Enter") self.KeyEnter() elif(c=='\x03'): self.dbgPrint("key Ctrl-C") elif(c=='\x13'): self.dbgPrint("key Ctrl-S") self.KeyCtrlS() elif(c=='\x18'): self.dbgPrint("key Ctrl-X") self.KeyCtrlX() elif(c&gt;='\x20' and c&lt;'\x7f'): self.KeyNormal(c) elif(state==1): if(c=='['): state=2 else: state=0 else: if(c=='A'): self.dbgPrint("key Up") self.KeyUp() if(c=='B'): self.dbgPrint("key Down") self.KeyDown() if(c=='C'): self.dbgPrint("key Right" ) self.KeyRight() if(c=='D'): self.dbgPrint("key Left") self.KeyLeft() if(c=='L'): self.dbgPrint("key Insert") if (c=='H'): self.dbgPrint("key Home") if(c=='I'): self.dbgPrint("key PageUp") if(c=='F'): self.dbgPrint("key End") if(c=='G'): self.dbgPrint("key PageDown") if(c =='M'): self.dbgPrint("key F1") if(c=='N'): self.dbgPrint("key F2") if(c=='O'): self.dbgPrint(" key F3") if(c=='P'): self.dbgPrint("key F4") if(c=='Q'): self.dbgPrint("key F5") if(c=='R' ): self.dbgPrint("key F6") if(c=='S'): self.dbgPrint("key F7") if(c=='T'): self.dbgPrint("key F8") if (c=='U'): self.dbgPrint("key F9") if(c=='V'): self.dbgPrint("key F10") if(c=='W'): self.dbgPrint("key F11") if(c=='X'): self.dbgPrint("key F12") state=0 def KeyNormal(self,c): self.curLine.s=self.curLine.s[:self.cursorToStrIndex]+c+self.curLine.s[self.cursorToStrIndex:] if(self.isModified==0): self.isModified=1 self.updateTitle() self.cursorToStrIndex+=1 if( self.cursor_x==self.w-1): self.cursor_x=1 self.updateLine(self.cursor_y,0,self.curLine.s[self.cursorToStrIndex-1:]) self.console.SetCursor(self.cursor_y ,self.cursor_x) else: self.cursor_x+=1 self.updateLine(self.cursor_y,self.cursor_x-1,self.curLine.s[self.cursorToStrIndex-1:]) self.console.SetCursor(self.cursor_y,self.cursor_x) def KeyTab(self): self.curLine.s=self.curLine.s[:self.cursorToStrIndex]+'\t'+self.curLine.s[self .cursorToStrIndex:] if(self.isModified==0): self.isModified=1 self.updateTitle() self.cursorToStrIndex+=1 if(self.cursor_x&gt;=self.w-4): self.cursor_x=4 self. updateLine(self.cursor_y,0,self.curLine.s[self.cursorToStrIndex-1:]) self.console.SetCursor(self.cursor_y,self.cursor_x) else: self.cursor_x+=4 self.updateLine(self.cursor_y,self.cursor_x-4,self.curLine.s[self.cursorToStrIndex-1:]) self.console.SetCursor(self.cursor_y,self.cursor_x) def KeyLeft(self): if( self.cursorToStrIndex&gt;=2): c1=self.curLine.s[self.cursorToStrIndex-1] c2=self.curLine.s[self.cursorToStrIndex-2] if(c1=='\t'): if(self .cursor_x&lt;=4): if(c2=='\t'): self.cursor_x=4 else: self.cursor_x=1 self.updateLine(self.cursor_y,0,self.curLine.s[self.cursorToStrIndex- 2:]) self.console.SetCursor(self.cursor_y,self.cursor_x) else: self.cursor_x-=4 self.console.SetCursor(self.cursor_y,self.cursor_x) else: if(self.cursor_x&lt;=1): if( c2=='\t'): self.cursor_x=4 else: self.cursor_x=1 self.updateLine(self.cursor_y,0,self.curLine.s[self.cursorToStrIndex-2:]) self.console.SetCursor (self.cursor_y,self.cursor_x) else: self.cursor_x-=1 self.console.SetCursor(self.cursor_y,self.cursor_x) self.cursorToStrIndex-=1 elif(self.cursorToStrIndex==1): self.cursor_x=0 self.updateLine(self.cursor_y,0,self.curLine.s) self.console.SetCursor(self.cursor_y,self.cursor_x) self.cursorToStrIndex-= 1 def KeyRight(self): if(len(self.curLine.s)==0): return if(self.cursorToStrIndexcursorToStrIndex+=1 if(self.cursor_x&gt;=self.w-4): self.cursor_x=4 self.updateLine(self.cursor_y,0,self.curLine.s[self.cursorToStrIndex-1:]) self.console. SetCursor(self.cursor_y,self.cursor_x) else: self.cursor_x+=4 self.updateLine(self.cursor_y,self.cursor_x-4,self.curLine.s[self.cursorToStrIndex-1:]) self.console.SetCursor(self.cursor_y,self.cursor_x) def KeyLeft(self): if( self.cursorToStrIndex&gt;=2): c1=self.curLine.s[self.cursorToStrIndex-1] c2=self.curLine.s[self.cursorToStrIndex-2] if(c1=='\t'): if(self.cursor_x&lt;=4): if(c2=='\t'): self.cursor_x= 4 else: self.cursor_x=1 self.updateLine(self.cursor_y,0,self.curLine.s[self.cursorToStrIndex-2:]) self.console.SetCursor(self.cursor_y,self.cursor_x) else: self.cursor_x-=4 self.console. SetCursor(self.cursor_y,self.cursor_x) else: if(self.cursor_x&lt;=1): if(c2=='\t'): self.cursor_x=4 else: self.cursor_x=1 self.updateLine(self.cursor_y,0,self.curLine.s[self.cursorToStrIndex-2:]) self.console.SetCursor(self.cursor_y,self.cursor_x) else: self.cursor_x-=1 self.console. SetCursor(self.cursor_y,self.cursor_x) self.cursorToStrIndex-=1 elif(self.cursorToStrIndex==1): self.cursor_x=0 self.updateLine(self.cursor_y,0,self.curLine.s) self.console.SetCursor(self.cursor_y,self.cursor_x) self.cursorToStrIndex-= 1 def KeyRight(self): if(len(self.curLine.s)==0): return if(self.cursorToStrIndexcursorToStrIndex+=1 if(self.cursor_x&gt;=self.w-4): self.cursor_x=4 self.updateLine(self.cursor_y,0,self.curLine.s[self.cursorToStrIndex-1:]) self.console. SetCursor(self.cursor_y,self.cursor_x) else: self.cursor_x+=4 self.updateLine(self.cursor_y,self.cursor_x-4,self.curLine.s[self.cursorToStrIndex-1:]) self.console.SetCursor (self.cursor_y,self.cursor_x) def KeyLeft(self): if(self.cursorToStrIndex&gt;=2): c1=self.curLine.s[self.cursorToStrIndex-1] c2=self.curLine.s[self.cursorToStrIndex-2] if(c1=='\t'): if(self.cursor_x&lt;=4): if(c2=='\t'): self.cursor_x= 4 else: self.cursor_x=1 self.updateLine(self.cursor_y,0,self.curLine.s[self.cursorToStrIndex-2:]) self.console.SetCursor(self.cursor_y,self.cursor_x) else: self. cursor_x-=4 self.console.SetCursor(self.cursor_y,self.cursor_x) else: if(self.cursor_x&lt;=1): if(c2=='\t'): self.cursor_x=4 else: self. cursor_x=1 self.updateLine(self.cursor_y,0,self.curLine.s[self.cursorToStrIndex-2:]) self.console.SetCursor(self.cursor_y,self.cursor_x) else: self.cursor_x-=1 self.console. SetCursor(self.cursor_y,self.cursor_x) self.cursorToStrIndex-=1 elif(self.cursorToStrIndex==1): self.cursor_x=0 self.updateLine(self.cursor_y,0,self.curLine.s) self.console .SetCursor(self.cursor_y,self.cursor_x) self.cursorToStrIndex-=1 def KeyRight(self): if(len(self.curLine.s)==0): return if(self.cursorToStrIndexif(len(self.curLine.s)==0): return if(self.cursorToStrIndexif(len(self.curLine.s)==0): return if(self.cursorToStrIndex<len(self.curline.s)): #note!!="" c="self.curLine.s[self.cursorToStrIndex]" elif(c="='\t'):" else:="" if(c="='\n'):#" if(self.cursor_x+4="" or="" return="">self.w-1): self.cursor_x = 4 self.cursorToStrIndex+=1 self.updateLine(self.cursor_y,0,self.curLine.s[self.cursorToStrIndex-1:]) self.console.SetCursor(self.cursor_y ,self.cursor_x) else: self.cursor_x+=4 self.cursorToStrIndex+=1 self.console.SetCursor(self.cursor_y,self.cursor_x) else: if(self.cursor_x+1&gt;self.w-1): self.cursor_x = 1 self.cursorToStrIndex+=1 self.updateLine(self.cursor_y,0,self.curLine.s[self.cursorToStrIndex-1:]) self.console.SetCursor(self.cursor_y,self.cursor_x) else: self.cursor_x+=1 self.cursorToStrIndex+=1 self.console.SetCursor(self.cursor_y,self.cursor_x) def KeyUp(self): if(self. curLine == self.lines.h): return else: if(self.cursor_y&lt;=1): self.cursor_x=0 self.cursorToStrIndex=0 self.curLine=self.curLine.p self.firstDispLine = self.firstDispLine.p self.updateEdit() self.console.SetCursor(self .cursor_y,self.cursor_x) else: self.updateLine(self.cursor_y,0,self.curLine.s) self.cursor_y-=1 self.cursor_x=0 self.cursorToStrIndex=0 self.curLine=self.curLine.p self.console.SetCursor(self.cursor_y,self.cursor_x) def KeyDown(self): if(self.curLine == self.lines.t): return else : if(self.cursor_y&gt;=self.h): self.cursor_x=0 self.cursorToStrIndex=0 self.curLine=self.curLine.n self.firstDispLine = self.firstDispLine.n self.updateEdit() self.console.SetCursor(self.cursor_y,self.cursor_x) else: self.updateLine(self.cursor_y,0,self .curLine.s) self.cursor_y+=1 self.cursor_x=0 self.cursorToStrIndex=0 self.curLine=self.curLine.n self.console.SetCursor(self.cursor_y,self.cursor_x) def KeyDelete(self): if(self.cursorToStrIndex<len(self.curline.s)): #note!!="" ((it="" ):="" a="self.curLine.s[:self.cursorToStrIndex]" a+="\x0a" and="" b="self.curLine.s[self.cursorToStrIndex:]" c="self.curLine.s[self.cursorToStrIndex]" def="" else:="" if(c!="\n" if(self.curline.n!="None):" if(self.cursor_y="" if(self.ismodified="=0):" is="" it="self.curLine.n;" keyenter(self):="" none)="" not="" return="" self.console.setcursor(self.cursor_y,self.cursor_x)="" self.curline.s="self.curLine.s[:self.cursorToStrIndex]+self.curLine.s[1+self.cursorToStrIndex:]" self.curline.s+="self.curLine.n.s" self.ismodified="1" self.lines.erase(self.curline.n)="" self.lines.insert(self.curline,b)="" self.updateline(self.cursor_y,self.cursor_x,self.curline.s[self.cursortostrindex:])="" self.updateline(y,0,"")="" self.updateline(y,0,it.s)="" self.updatetitle()="" while="" while(y<="self.h):" y="self.cursor_y+1;" y+="1" y<="self.h):">=self.h): self.cursor_x=0 self.cursorToStrIndex=0 self.curLine=self.curLine.n self.firstDispLine = self.firstDispLine.n self.updateEdit() self.console.SetCursor(self.cursor_y,self .cursor_x) else: self.cursor_y+=1 self.cursor_x=0 self.cursorToStrIndex=0 self.curLine=self.curLine.n self.updateEdit() self.console.SetCursor(self.cursor_y,self.cursor_x) def KeyCtrlS(self): if(self.isModified==1): self .doSave(); def KeyCtrlX(self): self.console.ClearSetting() self.console.ClearScreen() self.console.SetCursor(0,0) import sys sys.exit(0) def edit(s): editor = Editor() editor.edit(s) def edit_debug(s): editor = Editor() editor.enableDebug=1 editor.edit(s)
复制代码
linelist.py
  1. class Line : def __init__(self,str=""): self.s = str self.n = None self.p = None class LineList(object): def __init__(self): self.h = None self.t = None def add(self,a): nd = Line(a) if(self.h == None): self.h = nd; if(self.t != None): self.tn = nd; nd.p = self .t; nd.n = None; self.t = nd; def insert(self,a,b): nd = Line(b) if(an == None): self.add(b); else: c= an; nd.p = a; nd.n = c; an = nd; cp = nd; def erase(self,a): if(ap == None): self.h = an; elif(an == None): self.t = ap; else: a1 = ap; a2 = an; a1.n = a2; a2.p = a1; def isBegin(self,a): if(self.h == a): return True; else: return False; def isEnd(self,a): if(self.t == a): return True; else: return False;
复制代码








]3ABY6)5]$_WP$6]W46~81T.png (18.09 KB, downloads: 0)

]3ABY6)5]$_WP$6]W46~81T.png

1@OE[%G2Y`0@9WA@4T1%E$G.png (12.55 KB, downloads: 0)

1@OE[%G2Y`0@9WA@4T1%E$G.png

Latest reply

I made a mistake. I need to input an existing file. If the file does not exist, an error will be reported.  Details Published on 2018-6-26 10:24

赞赏

1

查看全部赞赏

 
 

1903

Posts

0

Resources
2
 
Good, check it out.
 
 
 

1w

Posts

25

Resources
3
 
How is this program used?

Comments

Copy the three files in and then either  Details Published on 2018-6-26 09:09
 
 
 

165

Posts

0

Resources
4
 
dcexpert posted on 2018-6-25 21:52 How to use this program?
Copy the three files and then
  1. >>> import editor >>> editor.edit("Here is the file path")
复制代码
or
  1. >>> from editor import edit >>> edit("Here is the file path")
复制代码


 
 
 

1w

Posts

25

Resources
5
 
When I run it, it prompts an error
 
 
 

1w

Posts

25

Resources
6
 
I made a mistake. I need to input an existing file. If the file does not exist, an error will be reported.
 
 
 

Find a datasheet?

EEWorld Datasheet Technical Support

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list