2760 views |5 replies
Last login 2023-2-28
Online Time 1974 hours
Prestige 136 points
Points 29 points
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.pyimport 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.pyfrom 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<4): self.console.Write('.') cursor_pos+=1 n-=1 nn+=1; self.console.SetColor(37,40) str_pos+=1 elif(c<'\x20' or c>'\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>='\x20' and c<'\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>=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>=2): c1=self.curLine.s[self.cursorToStrIndex-1] c2=self.curLine.s[self.cursorToStrIndex-2] if(c1=='\t'): if(self .cursor_x<=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<=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>=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>=2): c1=self.curLine.s[self.cursorToStrIndex-1] c2=self.curLine.s[self.cursorToStrIndex-2] if(c1=='\t'): if(self.cursor_x<=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<=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>=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>=2): c1=self.curLine.s[self.cursorToStrIndex-1] c2=self.curLine.s[self.cursorToStrIndex-2] if(c1=='\t'): if(self.cursor_x<=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<=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>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<=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>=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.pyclass 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; 复制代码
赞赏
1
查看全部赞赏