Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -------------------------------------------------------------------------
- -- Programme de Shiranuit --
- -- Copyright © 2018 Propriétés intellectuelles de Shiranuit --
- -- Toutes tentatives visant à s'approprier --
- -- le travail de Shiranuit ne sont donc pas acceptable, --
- -- et seront passible de sanction/amende pour --
- -- dédommagement et intérêts envers le créateur du Programme --
- -------------------------------------------------------------------------
- local args = {...}
- local mode = {
- byte={
- char=function(...)
- local argschar={...}
- local t = ""
- for i=1,#argschar do
- local v = tonumber(argschar[i]) or 0
- if v<0 then
- v=0
- end
- if v>255 then
- v=0
- end
- t=t..string.char(v)
- end
- return t
- end,
- byte=string.byte,
- auth="0123456789"
- },
- hex={
- byte=function(c)
- local c=string.byte(c)
- local hex = "0123456789abcdef"
- local r=""
- while c>16 do
- local l=c%16+1
- c=(c-(c%16))/16
- r=r..hex:sub(l,l)
- end
- r=r..hex:sub(c+1,c+1)
- r=string.reverse(r)
- return r
- end,
- char=function(...)
- local argschar={...}
- local t = ""
- for i=1,#argschar do
- local v = tonumber("0x"..argschar[i]) or 0
- if v<0 then
- v=0
- end
- if v>255 then
- v=0
- end
- t=t..string.char(v)
- end
- return t
- end,
- auth="0123456789abcdef"
- },
- bits={
- byte=function(c)
- local num=string.byte(c)
- local t={}
- while num>0 do
- rest=math.fmod(num,2)
- t[#t+1]=rest
- num=(num-rest)/2
- end
- return string.reverse(table.concat(t,""))
- end,
- char=function(...)
- local argschar={...}
- local t = ""
- for i=1,#argschar do
- local v = tonumber(argschar[i],2) or 0
- if v<0 then
- v=0
- end
- if v>255 then
- v=0
- end
- t=t..string.char(v)
- end
- return t
- end,
- auth="01"
- },
- base3={
- byte=function(c)
- local num=string.byte(c)
- local t={}
- while num>0 do
- rest=math.fmod(num,3)
- t[#t+1]=rest
- num=(num-rest)/3
- end
- return string.reverse(table.concat(t,""))
- end,
- char=function(...)
- local argschar={...}
- local t = ""
- for i=1,#argschar do
- local v = tonumber(argschar[i],3) or 0
- if v<0 then
- v=0
- end
- if v>255 then
- v=0
- end
- t=t..string.char(v)
- end
- return t
- end,
- auth="012"
- },
- base4={
- byte=function(c)
- local num=string.byte(c)
- local t={}
- while num>0 do
- rest=math.fmod(num,4)
- t[#t+1]=rest
- num=(num-rest)/4
- end
- return string.reverse(table.concat(t,""))
- end,
- char=function(...)
- local argschar={...}
- local t = ""
- for i=1,#argschar do
- local v = tonumber(argschar[i],4) or 0
- if v<0 then
- v=0
- end
- if v>255 then
- v=0
- end
- t=t..string.char(v)
- end
- return t
- end,
- auth="01234"
- }
- }
- if term.isColor() then
- _colors={select=colors.red,
- multiselect=colors.blue,
- border=colors.red,
- background=colors.black,
- text=colors.white,
- actioninfo=colors.green,
- highlight=colors.gray,
- info=colors.yellow}
- else
- _colors={select=colors.gray,
- multiselect=colors.lightGray,
- border=colors.white,
- background=colors.black,
- text=colors.white,
- actioninfo=colors.white,
- highlight=colors.gray,
- info=colors.white}
- end
- local lang = {
- en = {
- loading="Loading file",
- time="Time estimated",
- paste="Pasted",
- internalpaste="Internal pasted",
- deletebyte="Byte deleted",
- deletesel="Selection deleted",
- clipboard="Clipboarded",
- clearsel="Selection cleared",
- clearbyte="Byte cleared",
- bytesel="Byte(s) selected",
- byteinsert="Byte(s) inserted",
- undo="Undo",
- found="Sequence found",
- notfound="Sequence not found",
- redo="Redo",
- save="Saved",
- saveas="Saved as",
- clipboardset="Clipboard set",
- mode="Mode",
- langset="Language",
- help="Help",
- usage="Usage",
- list="List",
- docs={
- insert="Insert specified byte(s) or char(s) right to the cursor pos",
- help="Display the help for a command",
- bindlist="Show the list of binds",
- lang="Set the language of the editor",
- color="Change the color of a component",
- deleteAll="Delete all bytes",
- redraw="Force the redraw of the editor",
- mode="Change the mode of the editor for this file",
- undo="Undo the previous action",
- redo="Cancel the previous undo",
- setClipboard="Set the internal clipboard",
- save="Save the file",
- exit="exit the editor",
- gotoLn="Go to a specified line",
- find="Find specified byte(s) or char(s) in the file",
- editmode="Leave the command-line and return in editmode",
- clearHistory="Clear the actions history",
- },
- usages={
- insert={"insert byte <bytes...>","insert char <chars...>"},
- help={"help <command>"},
- bindlist={"bindlist"},
- lang={"lang <language>"},
- color={"color <component> <color>"},
- deleteAll={"deleteAll"},
- redraw={"redraw"},
- mode={"mode <mode>"},
- undo={"undo <count>","undo"},
- redo={"redo <count>","redo"},
- setClipboard={"setClipboard <text...>"},
- save={"save","save <filename>"},
- exit={"exit"},
- gotoLn={"gotoLn <line>"},
- find={"find byte <bytes...>","find char <chars...>"},
- editmode={"editmode"},
- clearHistory={"clearHistory"},
- },
- binds={
- ["Paste"]="CTRL+V",
- ["Copy"]="CTRL+C",
- ["Find selected sequence"]="CTRL+F",
- ["Internal Paste"]="CTRL+P",
- ["Delete selected byte(s)"]="CTRL+DEL",
- ["Select all"]="CTRL+A",
- ["Insert byte"]="CTRL+INSER",
- ["Jump to the next byte"]="TAB",
- ["Increase selection to the left"]="SHIFT+LEFT",
- ["Increase selection to the right"]="SHIFT+RIGHT"
- }
- },
- fr = {
- loading="Chargement du fichier",
- time="Temps estimé",
- paste="Coller",
- internalpaste="Collage interne",
- deletebyte="Byte supprimer",
- deletesel="Selection supprimer",
- clipboard="Copier",
- clearsel="Selection effacer",
- clearbyte="Byte effacer",
- bytesel="Byte(s) selectionner",
- byteinsert="Byte(s) inserer",
- undo="Annuler",
- found="Trouver",
- notfound="Non trouver",
- redo="Refaire",
- save="Enregistrer",
- saveas="Enregistrer sous",
- clipboardset="Presse-papiers",
- mode="Mode",
- langset="Langue",
- help="Aide",
- usage="Utilisation",
- list="Liste",
- docs={
- insert="Insere un/des byte(s) ou caractere(s) a droite du curseur",
- help="Affiche l'aide",
- bindlist="Affiche la liste des raccourcis clavier",
- lang="Change la langue de l'editeur",
- color="Change la couleur d'un composant de l'editeur",
- deleteAll="Supprime tous les bytes",
- redraw="Force la mise a jour de l'ecran",
- mode="Change le mode de l'editeur pour ce fichier",
- undo="Annule la precedente action",
- redo="Refait l'action precedente",
- setClipboard="Definit le Presse-papiers",
- save="Enregistre le fichier",
- exit="quitte l'editeur",
- gotoLn="Va a la ligne specifier",
- find="trouve une sequence de byte(s) ou de caractere(s) dans le fichier",
- editmode="Quitte la console et reviens en mode edition",
- clearHistory="Efface l'historique des actions",
- },
- usages={
- insert={"insert byte <bytes...>","insert char <chars...>"},
- help={"help <commande>"},
- bindlist={"bindlist"},
- lang={"lang <langue>"},
- color={"color <composant> <couleur>"},
- deleteAll={"deleteAll"},
- redraw={"redraw"},
- mode={"mode <mode>"},
- undo={"undo <nombre>","undo"},
- redo={"redo <nombre>","redo"},
- setClipboard={"setClipboard <texte...>"},
- save={"save","save <nom du fichier>"},
- exit={"exit"},
- gotoLn={"gotoLn <ligne>"},
- find={"find byte <bytes...>","find char <chars...>"},
- editmode={"editmode"},
- clearHistory={"clearHistory"},
- },
- binds={
- ["Colle"]="CTRL+V",
- ["Copie"]="CTRL+C",
- ["Trouve la sequence selectionner"]="CTRL+F",
- ["Collage interbe"]="CTRL+P",
- ["Supprime les bytes selectionner"]="CTRL+DEL",
- ["Selectionne tout"]="CTRL+A",
- ["Insere un byte"]="CTRL+INSER",
- ["Saute au byte suivant"]="TAB",
- ["Selectionne vers la gauche"]="SHIFT+LEFT",
- ["Selectionne vers la droite"]="SHIFT+RIGHT"
- }
- }
- }
- local w,h = term.getSize()
- local buff = window.create(term.current(),1,1,w,h,false)
- buff.clear()
- local revcolor = {[1]="white",[2]="orange",[4]="magenta",[8]="lightBlue",[16]="yellow",[32]="lime",[64]="pink",[128]="gray",[256]="lightGray",[512]="cyan",[1024]="purple",[2048]="blue",[4096]="brown",[8192]="green",[16384]="red",[32768]="black"}
- local language = lang["en"]
- local name = "ByteEditor"
- if fs.exists("."..name) then
- local h=fs.open("."..name,"r")
- local dt=h.readAll()
- h.close()
- local info = textutils.unserialise(dt)
- language=info.lang or lang["en"]
- _colors=info.colors or _colors
- else
- local dt = {colors=_colors, lang=language}
- local h=fs.open("."..name,"w")
- h.write(textutils.serialise(dt))
- h.close()
- end
- function drawProgressBar(x,y,w,progress,message,message2)
- term.clear()
- term.setCursorPos(x,y)
- term.write("[")
- term.setCursorPos(x+w+1,y)
- term.write("]")
- local size = math.floor((w-1)*progress/100)
- for i=x+1, x+size+1 do
- term.setCursorPos(i,y)
- term.write("=")
- end
- message = string.format(message or "", math.floor(progress))
- local mlen = math.floor(#message/2)
- local mid = x+math.floor(w/2)
- term.setCursorPos(mid-mlen,y-2)
- term.write(message)
- message2=message2 or ""
- local mlen2 = math.floor(#message2/2)
- term.setCursorPos(mid-mlen2,y-1)
- term.write(message2)
- end
- w=w-2
- local apercent = 0
- local atime = os.clock()
- if #args>=1 then
- local fname = args[1]
- local code = ""
- local nread = args[3] or "rb"
- if fname then
- if fs.exists(fname) and not fs.isDir(fname) then
- local len = fs.getSize(fname)
- if nread ~= "r" then
- local handle = fs.open(args[1],"rb")
- code=""
- local count = 0
- while true do
- char = handle.read()
- if char then
- code=code..string.char(char)
- else
- break
- end
- count = count+1
- if math.floor(count/len*100) ~= math.floor(apercent) then
- local t = os.clock()
- local timePassed = t-atime
- local prog = count/len*100 - apercent
- local delta = prog/timePassed
- local estimated = (100-count/len*100)/delta
- drawProgressBar(1,h,w,count/len*100,language.loading.." : "..fname.." - %d%%",language.time.." : "..estimated.."s")
- apercent = count/len*100
- atime = os.clock()
- end
- if count%10000==0 then
- sleep(0.01)
- end
- end
- handle.close()
- else
- local handle = fs.open(args[1],"r")
- code=handle.readAll()
- handle.close()
- end
- end
- end
- local _type = mode[args[2] or "byte"] or mode["byte"]
- local size = #tostring(_type.byte("\255"))
- local function conv(c)
- local r=_type.byte(c)
- return string.rep("0",size-#tostring(r))..r
- end
- local sep=math.floor(w/2+w/4)
- for i=1, w-3 do
- if i*(size+1)+i <= w-3 then
- sep=w-i-1
- else
- break
- end
- end
- local drawcount = h-4
- local function calc(code)
- local bytes={}
- for i=1,#code do
- local char=code:sub(i,i)
- local bt =conv(char)
- bytes[i]=bt
- end
- local lines = {}
- local txt={}
- local n=1
- for i=1, #bytes do
- if n*(size+1)<=sep-1 and #txt+1+sep<w then
- txt[#txt+1]=bytes[i]
- n=n+1
- else
- lines[#lines+1]=txt
- txt={bytes[i]}
- n=2
- end
- end
- lines[#lines+1]=#txt>0 and txt or nil
- return lines
- end
- local lines = calc(code)
- local offset=1
- local _bytepos = 3
- local select = true
- local multiselect = false
- local sStart={line=1, byte=1}
- local sEnd
- local _byte = 1
- local _line = 1
- local internalX = 1
- local ctrl=false
- local shift=false
- local alt=false
- local altgr=false
- local xPos, yPos, down=1,1,false
- local aSep
- local history={}
- local clipboard=""
- local hpos=1
- local editmode = true
- local cmd = ""
- local poscmd=0
- local tCompletions
- local nCompletion
- local tCompletionInfo = {}
- local docs = {}
- local modeinfo = false
- local paged=false
- local infodt = {}
- local infooffset = 1
- local wind = window.create(buff,3,3,sep-3,drawcount,false)
- local function charPos(line, byte)
- if lines[1] and line and byte then
- return (line-1)*#lines[1]+byte
- end
- end
- local function bytePos(charPos)
- if lines[1] then
- local _line=((charPos-1)-((charPos-1)%#lines[1]))/#lines[1]+1
- local _byte=(charPos-1)%#lines[1]+1
- return _line, _byte
- end
- end
- function draw(lines,offset,movesep)
- local ascii = true
- if _HOST then
- ascii=false
- end
- buff.setCursorBlink(false)
- movesep = movesep or false
- buff.setBackgroundColor(_colors.background)
- buff.setTextColor(_colors.text)
- buff.clear()
- buff.setCursorPos(2,1)
- buff.write(name..">")
- for i=3,drawcount+2 do
- buff.setCursorPos(3,i)
- buff.write(table.concat(lines[offset+i-3] or {}," "))
- buff.setCursorPos(sep+1,i)
- buff.write(_type.char(unpack(lines[offset+i-3] or {})))
- end
- buff.setBackgroundColor(_colors.background)
- buff.setTextColor(_colors.border)
- for i=2,w do
- buff.setCursorPos(i,drawcount+3)
- buff.write(ascii and "-" or "\140")
- end
- buff.setBackgroundColor(_colors.background)
- buff.setTextColor(_colors.border)
- for i=2,w do
- buff.setCursorPos(i,2)
- buff.write(ascii and "-" or "\131")
- end
- buff.setBackgroundColor(_colors.background)
- buff.setTextColor(_colors.border)
- for i=2,drawcount+3 do
- local char
- if ascii then
- char="|"
- if i==2 or i==drawcount+3 then
- char="*"
- end
- else
- char = "\149"
- if i==2 then
- char="\151"
- elseif i==drawcount+3 then
- char="\141"
- end
- end
- buff.setCursorPos(2,i)
- buff.write(char)
- end
- for i=2,drawcount+3 do
- local char
- if movesep then
- buff.setBackgroundColor(_colors.background)
- buff.setTextColor(_colors.separateurselect)
- else
- buff.setBackgroundColor(_colors.background)
- buff.setTextColor(_colors.border)
- end
- if ascii then
- char="|"
- if i==2 or i==drawcount+3 then
- char="*"
- end
- else
- char = "\149"
- if i==2 then
- char="\151"
- buff.setBackgroundColor(_colors.background)
- buff.setTextColor(_colors.border)
- elseif i==drawcount+3 then
- char="\141"
- buff.setBackgroundColor(_colors.background)
- buff.setTextColor(_colors.border)
- end
- end
- buff.setCursorPos(sep,i)
- buff.write(char)
- end
- for i=2,drawcount+3 do
- local char
- if ascii then
- buff.setBackgroundColor(_colors.background)
- buff.setTextColor(_colors.border)
- char="|"
- if i==2 or i==drawcount+3 then
- char="*"
- end
- else
- buff.setBackgroundColor(_colors.border)
- buff.setTextColor(_colors.background)
- char = "\149"
- if i==2 then
- char="\148"
- elseif i==drawcount+3 then
- char="\142"
- buff.setBackgroundColor(_colors.background)
- buff.setTextColor(_colors.border)
- end
- end
- buff.setCursorPos(w,i)
- buff.write(char)
- end
- local aff="Ln:"..(_line or 0).."/"..#lines.." Col:"..(_byte or 0).." Byte:"..(charPos(_line,_byte) or 0).."/"..#code
- buff.setCursorPos(w-#aff+1,drawcount+4)
- buff.setTextColor(_colors.info)
- buff.setBackgroundColor(_colors.background)
- buff.write(aff)
- end
- local function mSelPos()
- if sStart and sEnd then
- local sStartLine, sEndLine, sStartByte, sEndByte
- if sStart.line == sEnd.line then
- sStartLine=math.min(sStart.line,sEnd.line)
- sEndLine=math.max(sStart.line,sEnd.line)
- sStartByte=math.min(sStart.byte,sEnd.byte)
- sEndByte=math.max(sStart.byte,sEnd.byte)
- else
- sStartLine=math.min(sStart.line,sEnd.line)
- sEndLine=math.max(sStart.line,sEnd.line)
- if sStartLine == sStart.line then
- sStartByte=sStart.byte
- sEndByte=sEnd.byte
- else
- sStartByte=sEnd.byte
- sEndByte=sStart.byte
- end
- end
- return sStartLine, sEndLine, sStartByte, sEndByte
- end
- end
- local function clear()
- if #code<1 then
- draw(lines,offset)
- return
- end
- buff.setCursorBlink(false)
- if lines[_line] and lines[_line][_byte] then
- if _line+2-offset+1 > 2 and _line+2-offset+1 < drawcount+3 then
- buff.setCursorPos(_bytepos,_line+2-offset+1)
- buff.setBackgroundColor(_colors.background)
- buff.setTextColor(_colors.text)
- buff.write(lines[_line][_byte])
- buff.setCursorPos(sep+_byte,_line+2-offset+1)
- buff.setBackgroundColor(_colors.background)
- buff.setTextColor(_colors.text)
- buff.write(_type.char(lines[_line][_byte]))
- end
- else
- select = false
- end
- if multiselect then
- buff.setBackgroundColor(_colors.background)
- buff.setTextColor(_colors.text)
- local sStartLine, sEndLine, sStartByte, sEndByte = mSelPos()
- if sStartLine then
- if sStart.line == sEnd.line then
- for i=sStartByte, sEndByte do
- if lines[sStartLine] and lines[sStartLine][i] then
- if sStartLine+2-offset+1 > 2 and sStartLine+2-offset+1 < drawcount+3 then
- local _bytepos = 3+(i-1)*size+i-1
- buff.setCursorPos(_bytepos,sStartLine+2-offset+1)
- buff.write(lines[sStartLine][i])
- buff.setCursorPos(sep+i,sStartLine+2-offset+1)
- buff.write(_type.char(lines[sStartLine][i]))
- end
- end
- end
- else
- if lines[sStartLine] then
- for i=sStartByte, #lines[sStartLine] do
- if lines[sStartLine] and lines[sStartLine][i] then
- if sStartLine+2-offset+1 > 2 and sStartLine+2-offset+1 < drawcount+3 then
- local _bytepos = 3+(i-1)*size+i-1
- buff.setCursorPos(_bytepos,sStartLine+2-offset+1)
- buff.write(lines[sStartLine][i])
- buff.setCursorPos(sep+i,sStartLine+2-offset+1)
- buff.write(_type.char(lines[sStartLine][i]))
- end
- end
- end
- end
- for j=sStartLine+1, sEndLine-1 do
- if lines[j] then
- for i=1, #lines[j] do
- if j-offset+3 > 2 and j-offset+3 < drawcount+3 then
- local _bytepos = 3+(i-1)*size+i-1
- buff.setCursorPos(_bytepos,j-offset+3)
- buff.write(lines[j][i])
- buff.setCursorPos(sep+i,j-offset+3)
- buff.write(_type.char(lines[j][i]))
- end
- end
- end
- end
- for i=1, sEndByte do
- if lines[sEndLine] and lines[sEndLine][i] then
- if sEndLine+2-offset+1 > 2 and sEndLine+2-offset+1 < drawcount+3 then
- local _bytepos = 3+(i-1)*size+i-1
- buff.setCursorPos(_bytepos,sEndLine+2-offset+1)
- buff.write(lines[sEndLine][i])
- buff.setCursorPos(sep+i,sEndLine+2-offset+1)
- buff.write(_type.char(lines[sEndLine][i]))
- end
- end
- end
- end
- end
- end
- local aff="Ln:"..(_line or 0).."/"..#lines.." Col:"..(_byte or 0).." Byte:"..(charPos(_line,_byte) or 0).."/"..#code
- buff.setCursorPos(w-#aff+1,drawcount+4)
- buff.setTextColor(_colors.info)
- buff.setBackgroundColor(_colors.background)
- buff.clearLine()
- buff.setCursorPos(w-#aff+1,drawcount+4)
- buff.write(aff)
- buff.setTextColor(_colors.text)
- buff.setBackgroundColor(_colors.background)
- if _line+3-offset > 2 and _line+3-offset < drawcount+3 then
- if lines[_line] and lines[_line][_byte] then
- buff.setCursorPos(_bytepos+internalX-1,_line+3-offset)
- buff.setCursorBlink(select)
- end
- end
- if #code<1 then
- select=false
- buff.setCursorBlink(false)
- end
- end
- local function update()
- buff.setCursorBlink(false)
- if lines[_line] and lines[_line][_byte] then
- if _line+2-offset+1 > 2 and _line+2-offset+1 < drawcount+3 then
- buff.setCursorPos(_bytepos,_line+2-offset+1)
- buff.setBackgroundColor(_colors.select)
- buff.setTextColor(_colors.text)
- buff.write(lines[_line][_byte])
- buff.setCursorPos(sep+_byte,_line+2-offset+1)
- buff.setBackgroundColor(_colors.select)
- buff.setTextColor(_colors.text)
- buff.write(_type.char(lines[_line][_byte]))
- select=true
- end
- else
- select = false
- end
- if multiselect then
- buff.setBackgroundColor(_colors.multiselect)
- buff.setTextColor(_colors.text)
- local sStartLine, sEndLine, sStartByte, sEndByte = mSelPos()
- if sStartLine then
- if sStart.line == sEnd.line then
- for i=sStartByte, sEndByte do
- if lines[sStartLine] and lines[sStartLine][i] then
- if sStartLine+2-offset+1 > 2 and sStartLine+2-offset+1 < drawcount+3 then
- local _bytepos = 3+(i-1)*size+i-1
- buff.setCursorPos(_bytepos,sStartLine+2-offset+1)
- buff.write(lines[sStartLine][i])
- buff.setCursorPos(sep+i,sStartLine+2-offset+1)
- buff.write(_type.char(lines[sStartLine][i]))
- end
- end
- end
- else
- if lines[sStartLine] then
- for i=sStartByte, #lines[sStartLine] do
- if lines[sStartLine] and lines[sStartLine][i] then
- if sStartLine-offset+3 > 2 and sStartLine-offset+3 < drawcount+3 then
- local _bytepos = 3+(i-1)*size+i-1
- buff.setCursorPos(_bytepos,sStartLine-offset+3)
- buff.write(lines[sStartLine][i])
- buff.setCursorPos(sep+i,sStartLine-offset+3)
- buff.write(_type.char(lines[sStartLine][i]))
- end
- end
- end
- end
- for j=sStartLine+1, sEndLine-1 do
- if lines[j] then
- for i=1, #lines[j] do
- if j-offset+3 > 2 and j-offset+3 < drawcount+3 then
- local _bytepos = 3+(i-1)*size+i-1
- buff.setCursorPos(_bytepos,j-offset+3)
- buff.write(lines[j][i])
- buff.setCursorPos(sep+i,j-offset+3)
- buff.write(_type.char(lines[j][i]))
- end
- end
- end
- end
- for i=1, sEndByte do
- if lines[sEndLine] and lines[sEndLine][i] then
- if sEndLine-offset+3 > 2 and sEndLine-offset+3 < drawcount+3 then
- local _bytepos = 3+(i-1)*size+i-1
- buff.setCursorPos(_bytepos,sEndLine-offset+3)
- buff.write(lines[sEndLine][i])
- buff.setCursorPos(sep+i,sEndLine-offset+3)
- buff.write(_type.char(lines[sEndLine][i]))
- end
- end
- end
- end
- end
- end
- local aff="Ln:"..(_line or 0).."/"..#lines.." Col:"..(_byte or 0).." Byte:"..(charPos(_line,_byte) or 0).."/"..#code
- buff.setCursorPos(w-#aff+1,drawcount+4)
- buff.setTextColor(_colors.info)
- buff.setBackgroundColor(_colors.background)
- buff.clearLine()
- buff.setCursorPos(w-#aff+1,drawcount+4)
- buff.write(aff)
- buff.setTextColor(_colors.text)
- buff.setBackgroundColor(_colors.background)
- if _line+3-offset > 2 and _line+3-offset < drawcount+3 then
- if lines[_line] and lines[_line][_byte] then
- buff.setCursorPos(_bytepos+internalX-1,_line+3-offset)
- buff.setCursorBlink(select)
- end
- end
- if #code<1 then
- select=false
- buff.setCursorBlink(false)
- end
- end
- local function skipLeft()
- if lines[_line] then
- local min,max=1,#lines[_line]
- if _byte-1 >= 1 then
- internalX=size
- clear()
- multiselect=false
- _byte=_byte-1
- _bytepos = 3+(_byte-1)*size+_byte-1
- update()
- else
- if _line-offset>=1 then
- if _line-1 >= 1 then
- clear()
- multiselect=false
- _byte=max
- _line=_line-1
- _bytepos = 3+(_byte-1)*size+_byte-1
- update()
- end
- else
- if offset > 1 then
- if _line-1 >= 1 then
- clear()
- multiselect=false
- offset=offset-1
- _line=_line-1
- _byte=max
- _bytepos = 3+(_byte-1)*size+_byte-1
- internalX=size
- draw(lines,offset)
- update()
- end
- end
- end
- end
- sStart = {line=_line, byte=_byte}
- sEnd=nil
- end
- clear()
- draw(lines,offset)
- update()
- end
- local function skipRight()
- if lines[_line] then
- local min,max=1,#lines[_line]
- if _byte+1 <= max then
- internalX=1
- clear()
- multiselect=false
- _byte=_byte+1
- _bytepos = 3+(_byte-1)*size+_byte-1
- update()
- else
- if _line+2-offset<=drawcount then
- if _line+1 < #lines then
- clear()
- multiselect=false
- _byte=1
- _line=_line+1
- _bytepos = 3+(_byte-1)*size+_byte-1
- update()
- end
- else
- if offset+drawcount <= #lines then
- if _line+1 < #lines then
- clear()
- multiselect=false
- offset=offset+1
- _line=_line+1
- _byte=1
- _bytepos = 3+(_byte-1)*size+_byte-1
- internalX=1
- draw(lines,offset)
- update()
- end
- end
- end
- end
- sStart = {line=_line, byte=_byte}
- sEnd=nil
- end
- clear()
- draw(lines,offset)
- update()
- end
- local function cursorLeft()
- if internalX-1>=1 then
- clear()
- multiselect=false
- internalX=internalX-1
- update()
- else
- skipLeft()
- end
- clear()
- update()
- sStart = {line=_line, byte=_byte}
- sEnd=nil
- end
- local function cursorRight()
- local min,max=1,(lines and lines[_line] and #lines[_line]) or 0
- if internalX+1 <= size then
- clear()
- multiselect=false
- internalX=internalX+1
- update()
- else
- skipRight()
- end
- clear()
- update()
- sStart = {line=_line, byte=_byte}
- sEnd=nil
- end
- local function dispInfo(txt)
- buff.setBackgroundColor(_colors.background)
- buff.setTextColor(_colors.actioninfo)
- local aff="Ln:"..(_line or 0).." Col:"..(_byte or 0).." Byte:"..(charPos(_line,_byte) or 0)
- buff.setCursorPos(w-#aff,drawcount+4)
- buff.setTextColor(_colors.info)
- buff.setBackgroundColor(_colors.background)
- buff.clearLine()
- buff.setCursorPos(w-#aff,drawcount+4)
- buff.write(aff)
- buff.setCursorPos(2,drawcount+4)
- buff.setTextColor(_colors.actioninfo)
- buff.setBackgroundColor(_colors.background)
- buff.write(txt)
- buff.setTextColor(_colors.text)
- buff.setBackgroundColor(_colors.background)
- if editmode then
- buff.setCursorPos(_bytepos+internalX-1,_line+3-offset)
- else
- local s=name.."> "
- local sx = 2+#s
- local nScroll = 0
- if sx + poscmd >= w then
- nScroll = (sx + poscmd) - w
- end
- buff.setCursorPos( sx + poscmd - nScroll, 1 )
- end
- buff.setCursorBlink(true)
- end
- local function copy(tb)
- local t={}
- for k, v in pairs(tb) do
- t[k]=v
- end
- return t
- end
- local function updatecmd(_bClear)
- buff.setTextColor(_colors.text)
- buff.setBackgroundColor(_colors.background)
- buff.setCursorBlink(true)
- buff.setCursorPos(2,1)
- buff.clearLine()
- local s=name.."> "
- local nScroll = 0
- local sx = 2+#s
- local size = w-sx
- if sx + poscmd >= w then
- nScroll = (sx + poscmd) - w
- end
- buff.write(s..string.sub( cmd, nScroll + 1, nScroll+1+size))
- if nCompletion then
- local sCompletion = tCompletions[ nCompletion ]
- local oldText, oldBg
- if not _bClear then
- oldText = buff.getTextColor()
- oldBg = buff.getBackgroundColor()
- buff.setTextColor( _colors.text )
- buff.setBackgroundColor( _colors.highlight )
- end
- buff.write( sCompletion )
- if not _bClear then
- buff.setTextColor( oldText )
- buff.setBackgroundColor( oldBg )
- end
- end
- buff.setCursorPos( sx + poscmd - nScroll, 1 )
- buff.setCursorBlink(true)
- end
- local function completeMultipleChoice( sText, tOptions, bAddSpaces )
- local tResults = {}
- for n=1,#tOptions do
- local sOption = tOptions[n]
- if #sOption + (bAddSpaces and 1 or 0) > #sText and string.sub( sOption, 1, #sText ) == sText then
- local sResult = string.sub( sOption, #sText + 1 )
- if bAddSpaces then
- table.insert( tResults, sResult .. " " )
- else
- table.insert( tResults, sResult )
- end
- end
- end
- return tResults
- end
- local function tokenise( ... )
- local sLine = table.concat( { ... }, " " )
- local tWords = {}
- local bQuoted = false
- for match in string.gmatch( sLine .. "\"", "(.-)\"" ) do
- if bQuoted then
- table.insert( tWords, match )
- else
- for m in string.gmatch( match, "[^ \t]+" ) do
- table.insert( tWords, m )
- end
- end
- bQuoted = not bQuoted
- end
- return tWords
- end
- local function completeCommand(sLine)
- local t = {}
- for k, v in pairs(tCompletionInfo) do
- if #k>#sLine and k:sub(1,#sLine) == sLine then
- t[#t+1]=k:sub(#sLine+1, #k)
- end
- end
- table.sort(t)
- return t
- end
- local function completeProgramArgument( sProgram, nArgument, sPart, tPreviousParts )
- local tInfo = tCompletionInfo[ sProgram ]
- if tInfo then
- return tInfo.fnComplete( shell, nArgument, sPart, tPreviousParts )
- end
- return nil
- end
- local function complete( sLine )
- if #sLine > 0 then
- local tWords = tokenise( sLine )
- local nIndex = #tWords
- if string.sub( sLine, #sLine, #sLine ) == " " then
- nIndex = nIndex + 1
- end
- if nIndex == 1 then
- local sBit = tWords[1] or ""
- local sPath = sBit
- if tCompletionInfo[ sPath ] then
- return { " " }
- else
- local tResults = completeCommand(sBit)
- for n=1,#tResults do
- local sResult = tResults[n]
- local sPath = sBit .. sResult
- if tCompletionInfo[ sPath ] then
- tResults[n] = sResult .. " "
- end
- end
- return tResults
- end
- elseif nIndex > 1 then
- local sPath = tWords[1]
- local sPart = tWords[nIndex] or ""
- local tPreviousParts = tWords
- tPreviousParts[nIndex] = nil
- return completeProgramArgument( sPath , nIndex - 1, sPart, tPreviousParts )
- end
- end
- return nil
- end
- local function addCommand( cmd, args, bAddSpaces)
- tCompletionInfo[ cmd ] = {
- fnComplete = function( shell, nIndex, sText, tPreviousText ) return completeMultipleChoice( sText, args[nIndex] or {}, bAddSpaces) end
- }
- end
- local function recomplete()
- if complete and poscmd == string.len(cmd) then
- tCompletions = complete( cmd )
- if tCompletions and #tCompletions > 0 then
- nCompletion = 1
- else
- nCompletion = nil
- end
- else
- tCompletions = nil
- nCompletion = nil
- end
- end
- local function uncomplete()
- tCompletions = nil
- nCompletion = nil
- end
- local function acceptCompletion()
- if nCompletion then
- updatecmd( true )
- local sCompletion = tCompletions[ nCompletion ]
- local sFirstLetter = string.sub( sCompletion, 1, 1 )
- local sCommonPrefix = sCompletion
- for n=1,#tCompletions do
- local sResult = tCompletions[n]
- if n ~= nCompletion and string.find( sResult, sFirstLetter, 1, true ) == 1 then
- while #sCommonPrefix > 1 do
- if string.find( sResult, sCommonPrefix, 1, true ) == 1 then
- break
- else
- sCommonPrefix = string.sub( sCommonPrefix, 1, #sCommonPrefix - 1 )
- end
- end
- end
- end
- cmd = cmd .. sCommonPrefix
- poscmd = string.len( cmd )
- end
- recomplete()
- updatecmd()
- end
- local function writemode()
- select=false
- update()
- poscmd=#cmd
- recomplete()
- updatecmd()
- editmode = false
- end
- local function termwrite( sText , term)
- local w,h = term.getSize()
- local x,y = term.getCursorPos()
- local nLinesPrinted = 0
- local function newLine()
- if y + 1 <= h then
- term.setCursorPos(1, y + 1)
- else
- term.setCursorPos(1, h)
- term.scroll(1)
- end
- x, y = term.getCursorPos()
- nLinesPrinted = nLinesPrinted + 1
- end
- while string.len(sText) > 0 do
- local whitespace = string.match( sText, "^[ \t]+" )
- if whitespace then
- term.write( whitespace )
- x,y = term.getCursorPos()
- sText = string.sub( sText, string.len(whitespace) + 1 )
- end
- local newline = string.match( sText, "^\n" )
- if newline then
- newLine()
- sText = string.sub( sText, 2 )
- end
- local text = string.match( sText, "^[^ \t\n]+" )
- if text then
- sText = string.sub( sText, string.len(text) + 1 )
- if string.len(text) > w then
- while string.len( text ) > 0 do
- if x > w then
- newLine()
- end
- term.write( text )
- text = string.sub( text, (w-x) + 2 )
- x,y = term.getCursorPos()
- end
- else
- if x + string.len(text) - 1 > w then
- newLine()
- end
- term.write( text )
- x,y = term.getCursorPos()
- end
- end
- end
- return nLinesPrinted
- end
- local function termprint(term, ... )
- local nLinesPrinted = 0
- for n,v in ipairs( { ... } ) do
- nLinesPrinted = nLinesPrinted + termwrite( tostring( v ) , term)
- end
- nLinesPrinted = nLinesPrinted + termwrite( "\n", term)
- return nLinesPrinted
- end
- function updateInfo()
- wind.setVisible(true)
- wind.setBackgroundColor(_colors.background)
- wind.setTextColor(_colors.text)
- wind.clear()
- wind.setCursorPos(1,1)
- local n=1
- for k, v in pairs(infodt) do
- if n>infooffset+drawcount-2 then
- break
- end
- if n>=infooffset then
- termprint(wind,"- "..k.." : "..v)
- end
- n=n+1
- end
- modeinfo=true
- if not editmode then
- updatecmd()
- end
- end
- -- Command Completion
- addCommand("insert",{{"byte","char"}})
- addCommand("save",{{}})
- addCommand("exit",{{}})
- addCommand("deleteAll",{{}})
- addCommand("clearHistory",{{}})
- addCommand("redraw",{{}})
- addCommand("gotoLn",{{}})
- tCompletionInfo[ "help" ] = { fnComplete = function( shell, nIndex, sText, tPreviousText ) local cmd={} for k, v in pairs(tCompletionInfo) do cmd[#cmd+1]=k end local args={cmd} return completeMultipleChoice( sText, args[nIndex] or {}, false) end }
- addCommand("bindlist",{{}})
- addCommand("editmode",{{}})
- addCommand("setClipboard",{{}})
- addCommand("undo",{{}})
- addCommand("redo",{{}})
- addCommand("find",{{"byte","char"}},true)
- local modes={}
- for k, v in pairs(mode) do
- modes[#modes+1]=k
- end
- addCommand("mode",{modes})
- local langs={}
- for k, v in pairs(lang) do
- langs[#langs+1]=k
- end
- addCommand("lang",{langs})
- local keycolor = {}
- for k, v in pairs(_colors) do
- keycolor[#keycolor+1]=k
- end
- if term.isColor() then
- addCommand("color",{keycolor,{"white","red","green","blue","lime","gray","lightGray","grey","lightGrey","black","purple","brown","cyan","lightBlue","magenta","orange","yellow","pink"}},true)
- else
- addCommand("color",{keycolor,{"white","gray","lightGray","grey","lightGrey","black"}},true)
- end
- -- Draw
- draw(lines,offset)
- writemode()
- while true do
- buff.setVisible(true)
- buff.setVisible(false)
- local event, p1,p2,p3,p4,p5 = os.pullEvent()
- if modeinfo then
- if event == "mouse_drag" or event=="mouse_click" or event=="mouse_up" then
- draw(lines,offset)
- update()
- if not editmode then
- updatecmd()
- end
- modeinfo=false
- event = ""
- end
- end
- if event=="mouse_click" then
- local x, y = p2,p3
- if x>2 and x<sep and y>2 and y<drawcount+3 then
- editmode = true
- clear()
- multiselect=false
- local a,b,c = _byte, _line, _bytepos
- local _x = x-3
- _byte=(_x-(_x%(size+1)))/(size+1)+1
- _line = y-2+offset-1
- _bytepos = 3+(_byte-1)*size+_byte-1
- if lines[_line] and lines[_line][_byte] and x>=_bytepos and x<= _bytepos+size-1 then
- internalX = x-_bytepos+1
- update()
- else
- internalX=1
- select = true
- update()
- end
- xPos,yPos,down=x,y,true
- sStart = {line=_line, byte=_byte}
- sEnd=nil
- elseif x>sep and x<w and y>2 and y<drawcount+3 then
- editmode = true
- local line = y-2+offset-1
- local byte = x-sep
- if lines[line] and lines[line][byte] then
- clear()
- multiselect=false
- _line=y-2+offset-1
- local _x=x-sep
- _byte = _x
- _bytepos = 3+(_byte-1)*size+_byte-1
- internalX = 1
- update()
- xPos,yPos,down=x,y,true
- sStart = {line=_line, byte=_byte}
- sEnd=nil
- else
- writemode()
- end
- else
- local s=name.."> "
- local t=s..cmd
- if editmode then
- writemode()
- end
- end
- elseif event == "mouse_up" then
- down=false
- if xPos==aSep and yPos>=2 and yPos<=drawcount+3 then
- draw(lines,offset)
- update()
- end
- elseif event=="mouse_drag" then
- if down==true and xPos<sep and yPos>2 and yPos<drawcount+3 then -- Bytes
- editmode = true
- local x, y = p2, p3
- if x>2 and x<sep and y>2 and y<drawcount+3 then
- local a,b,c = _byte, _line, _bytepos
- multiselect=true
- clear()
- _line=y-2+offset-1
- local _x = x-3
- _byte=(_x-(_x%(size+1)))/(size+1)+1
- _bytepos = 3+(_byte-1)*size+_byte-1
- if lines[_line] and lines[_line][_byte] and x>=_bytepos and x<= _bytepos+size-1 then
- internalX = x-_bytepos+1
- sEnd = {byte=_byte, line=_line}
- else
- _byte=a
- _line=b
- _bytepos=c
- end
- update()
- end
- elseif down==true and xPos>sep and xPos<w and yPos>2 and yPos<drawcount+3 then -- Chars
- editmode = true
- local x, y = p2, p3
- if x>sep and x<w and y>2 and y<drawcount+3 then
- local a,b,c = _byte, _line, _bytepos
- clear()
- multiselect=true
- _line=y-2+offset-1
- local _x=x-sep
- _byte = _x
- _bytepos = 3+(_byte-1)*size+_byte-1
- internalX=1
- if lines[_line] and lines[_line][_byte] then
- sEnd = {byte=_byte, line=_line}
- else
- _byte=a
- _line=b
- _bytepos=c
- end
- update()
- end
- end
- elseif event == "paste" then
- if editmode then
- if #clipboard>0 then
- local olines={}
- for k, v in pairs(lines) do
- olines[k]=copy(v)
- end
- local pos=(_line-1)*#lines[1]+_byte
- if multiselect then
- local sStartLine, sEndLine, sStartByte, sEndByte = mSelPos()
- local startPos = (sStartLine-1)*#lines[1]+sStartByte
- local endPos = (sEndLine-1)*#lines[1]+sEndByte
- code=code:sub(1,startPos-1)..clipboard..code:sub(endPos+1)
- _line=((startPos+#clipboard-1)-((startPos+#clipboard-1)%#lines[1]))/#lines[1]+1
- _byte=(startPos+#clipboard-1)%#lines[1]+1
- else
- code=code:sub(1,pos-1)..clipboard..code:sub(pos+#clipboard)
- _line=((pos+#clipboard-1)-((pos+#clipboard-1)%#lines[1]))/#lines[1]+1
- _byte=(pos+#clipboard-1)%#lines[1]+1
- end
- _bytepos = 3+(_byte-1)*size+_byte-1
- internalX=1
- lines=calc(code)
- local nlines = {}
- for k, v in pairs(lines) do
- nlines[k]=copy(v)
- end
- history[hpos]={type="byte",info={oldlines=olines, newlines=nlines, line=_line, byte=_byte, bytepos=_bytepos, internalX=internalX,offset=offset, drawcount=drawcount}}
- hpos=hpos+1
- multiselect=false
- draw(lines,offset)
- update()
- sStart={line=_line,byte=_byte}
- sEnd=nil
- dispInfo(language.paste)
- end
- else
- cmd=cmd:sub(1,poscmd)..p1..cmd:sub(poscmd+1)
- poscmd=poscmd+#p1
- recomplete()
- updatecmd()
- dispInfo(language.paste)
- end
- elseif event == "mouse_scroll" then
- if modeinfo then
- if not paged then
- if p1==1 then
- local c = 0
- for k, v in pairs(infodt) do
- c=c+1
- end
- if infooffset+drawcount-1<=c then
- infooffset=infooffset+1
- updateInfo()
- end
- elseif p1==-1 then
- if infooffset-1>=1 then
- infooffset=infooffset-1
- updateInfo()
- end
- end
- end
- else
- if p1==1 then
- if offset+drawcount <= #lines then
- clear()
- offset=offset+1
- draw(lines,offset)
- update()
- if not editmode then
- updatecmd()
- end
- end
- elseif p1==-1 then
- if offset-1 >= 1 then
- clear()
- offset=offset-1
- draw(lines,offset)
- update()
- if not editmode then
- updatecmd()
- end
- end
- end
- end
- elseif event=="key_up" then
- if p1==29 then -- Ctrl
- ctrl=false
- elseif p1==42 then -- Shift
- shift=false
- elseif p1==56 then -- Alt
- alt=false
- elseif p1==184 then -- AltGr
- altgr=false
- end
- elseif event=="key" then
- if p1==29 then -- Ctrl
- ctrl=true
- elseif p1==42 then -- Shift
- shift=true
- elseif p1==56 then -- Alt
- alt=true
- if editmode then
- writemode()
- else
- editmode=true
- update()
- end
- elseif p1==184 then -- AltGr
- altgr=true
- end
- if editmode then
- if #code>0 then
- if p1==211 then -- Delete
- if ctrl then
- local olines={}
- for k, v in pairs(lines) do
- olines[k]=copy(v)
- end
- if multiselect then
- local sStartLine, sEndLine, sStartByte, sEndByte = mSelPos()
- local startPos = charPos(sStartLine, sStartByte)
- local endPos = charPos(sEndLine, sEndByte)
- code=code:sub(1,startPos-1)..code:sub(endPos+1)
- local olines={}
- for k, v in pairs(lines) do
- olines[k]=copy(v)
- end
- local nlines = calc(code)
- history[hpos]={type="byte",info={oldlines=olines, newlines=nlines, line=sStartLine, byte=sStartByte, bytepos=3+(sStartByte-1)*size+sStartByte-1, internalX=internalX,offset=offset, drawcount=drawcount}}
- hpos=hpos+1
- lines=nlines
- _byte=sStartByte
- _line=sStartLine
- _bytepos = 3+(_byte-1)*size+_byte-1
- else
- if lines and lines[_line] then
- table.remove(lines[_line],_byte)
- local code=""
- for i=1, #lines do
- for j=1, #lines[i] do
- code=code.._type.char(lines[i][j])
- end
- end
- lines=calc(code)
- local nlines = {}
- for k, v in pairs(lines) do
- nlines[k]=copy(v)
- end
- history[hpos]={type="byte",info={oldlines=olines, newlines=nlines, line=_line, byte=_byte, bytepos=_bytepos, internalX=internalX,offset=offset, drawcount=drawcount}}
- hpos=hpos+1
- end
- end
- if offset+drawcount-1>#lines then
- offset=#lines-drawcount+1
- end
- if offset<1 then
- offset=1
- end
- skipLeft()
- local msel = multiselect
- multiselect=false
- draw(lines,offset)
- update()
- if #code<1 then
- writemode()
- end
- if msel then
- dispInfo(language.deletesel)
- else
- dispInfo(language.deletebyte)
- end
- else
- local bt = lines and lines[_line] and lines[_line][_byte]
- if bt then
- if multiselect then
- local sStartLine, sEndLine, sStartByte, sEndByte = mSelPos()
- local startPos = charPos(sStartLine, sStartByte)
- local endPos = charPos(sEndLine, sEndByte)
- code=code:sub(1,startPos-1)..string.rep("\0",endPos-startPos+1)..code:sub(endPos+1)
- local olines={}
- for k, v in pairs(lines) do
- olines[k]=copy(v)
- end
- local nlines = calc(code)
- history[hpos]={type="byte",info={oldlines=olines, newlines=nlines, line=_line, byte=_byte, bytepos=_bytepos, internalX=internalX,offset=offset, drawcount=drawcount}}
- hpos=hpos+1
- lines=nlines
- draw(lines,offset)
- dispInfo(language.clearsel)
- else
- bt=bt:sub(1,internalX-1).."0"..bt:sub(internalX+1)
- history[hpos]={type="char",info={line=_line,byte=_byte,old=lines[_line][_byte], new=bt, internalX=internalX, bytepos=_bytepos,offset=offset, drawcount=drawcount}}
- hpos=hpos+1
- lines[_line][_byte]=bt
- cursorRight()
- dispInfo(language.clearbyte)
- end
- end
- end
- elseif p1==46 then -- c
- if ctrl then
- if multiselect then
- local sStartLine, sEndLine, sStartByte, sEndByte = mSelPos()
- local startPos = (sStartLine-1)*#lines[1]+sStartByte
- local endPos = (sEndLine-1)*#lines[1]+sEndByte
- local code=""
- for i=1, #lines do
- for j=1, #lines[i] do
- code=code.._type.char(lines[i][j])
- end
- end
- clipboard=code:sub(startPos,endPos)
- else
- clipboard=_type.char(lines[_line][_byte])
- end
- dispInfo(language.clipboard)
- end
- elseif p1==30 then -- a
- if ctrl then
- multiselect=true
- clear()
- local drawcount = drawcount
- drawcount = drawcount<=#lines and drawcount or #lines
- _byte = #lines[drawcount+offset-1]
- _line=drawcount+offset-1
- _bytepos = 3+(_byte-1)*size+_byte-1
- sStart={line=1,byte=1}
- sEnd={line=#lines,byte=#lines[#lines]}
- update()
- dispInfo(language.bytesel)
- end
- elseif p1==210 then -- insert
- if ctrl then
- local olines={}
- for k, v in pairs(lines) do
- olines[k]=copy(v)
- end
- table.insert(lines[_line],_byte+1,string.rep("0",size))
- local code=""
- for i=1, #lines do
- for j=1, #lines[i] do
- code=code.._type.char(lines[i][j])
- end
- end
- local nlines=calc(code)
- history[hpos]={type="byte",info={oldlines=olines, newlines=nlines, line=_line, byte=_byte, bytepos=_bytepos, internalX=internalX,offset=offset, drawcount=drawcount}}
- lines=nlines
- hpos=hpos+1
- draw(lines,offset)
- update()
- dispInfo(language.byteinsert)
- end
- elseif p1==14 then -- Backspace
- local bt = lines[_line] and lines[_line][_byte]
- if bt then
- cursorLeft()
- bt=bt:sub(1,internalX-1).."0"..bt:sub(internalX+1)
- history[hpos]={type="char",info={line=_line,byte=_byte,old=lines[_line][_byte], new=bt, internalX=internalX, bytepos=_bytepos,offset=offset, drawcount=drawcount}}
- hpos=hpos+1
- lines[_line][_byte]=bt
- update()
- end
- elseif p1==44 then -- z
- if ctrl then
- local h=history[hpos-1]
- if h then
- hpos=hpos-1
- if h.type == "char" then
- clear()
- _line=h.info.line
- _byte=h.info.byte
- internalX=h.info.internalX
- offset=h.info.offset
- _bytepos=h.info.bytepos
- drawcount=h.info.drawcount
- lines[_line][_byte]=h.info.old
- draw(lines,offset)
- update()
- elseif h.type == "byte" then
- lines=h.info.oldlines
- _line=h.info.line
- _byte=h.info.byte
- internalX=h.info.internalX
- _bytepos=h.info.bytepos
- offset=h.info.offset
- drawcount=h.info.drawcount
- code=""
- for i=1, #lines do
- for j=1, #lines[i] do
- code=code.._type.char(lines[i][j])
- end
- end
- draw(lines,offset)
- update()
- end
- dispInfo(language.undo)
- end
- end
- elseif p1==33 then -- f
- local txt
- if multiselect then
- local sStartLine, sEndLine, sStartByte, sEndByte = mSelPos()
- if sStartLine and sEndLine and sStartByte and sEndByte and lines and lines[1] then
- local startPos = (sStartLine-1)*#lines[1]+sStartByte
- local endPos = (sEndLine-1)*#lines[1]+sEndByte
- local code=""
- for i=1, #lines do
- for j=1, #lines[i] do
- code=code.._type.char(lines[i][j])
- end
- end
- txt=code:sub(startPos,endPos)
- end
- else
- if (lines and lines[_line] and lines[_line][_byte]) then
- txt=_type.char(lines[_line][_byte])
- end
- end
- txt=(txt and #txt>0 and txt) or "\0"
- local pos = charPos(_line,_byte)
- local _s, _e = string.find(code,txt,pos+1)
- if not _s then
- _s, _e = string.find(code,txt,1)
- end
- if _s then
- local line1, byte1 = bytePos(_s)
- local line2, byte2 = bytePos(_e)
- _byte=byte2
- _line=line2
- _bytepos = 3+(_byte-1)*size+_byte-1
- offset=_line
- local drawcount = drawcount
- drawcount=drawcount<=#lines and drawcount or #lines
- if offset+drawcount-1>#lines then
- offset=#lines-drawcount+1
- end
- if offset<1 then
- offset=1
- end
- sStart={line=line1,byte=byte1}
- sEnd={line=line2,byte=byte2}
- multiselect=true
- draw(lines,offset)
- update()
- if not editmode then
- updatecmd()
- end
- dispInfo(language.found)
- else
- dispInfo(language.notfound)
- end
- elseif p1==21 then -- y
- if ctrl then
- local h=history[hpos]
- if h then
- hpos=hpos+1
- if h.type == "char" then
- clear()
- _line=h.info.line
- _byte=h.info.byte
- internalX=h.info.internalX
- _bytepos=h.info.bytepos
- offset=h.info.offset
- drawcount=h.info.drawcount
- lines[_line][_byte]=h.info.new
- draw(lines,offset)
- update()
- elseif h.type == "byte" then
- code=""
- for i=1, #lines do
- for j=1, #lines[i] do
- code=code.._type.char(lines[i][j])
- end
- end
- lines=h.info.newlines
- _line=h.info.line
- _byte=h.info.byte
- internalX=h.info.internalX
- _bytepos=h.info.bytepos
- offset=h.info.offset
- drawcount=h.info.drawcount
- draw(lines,offset)
- update()
- end
- dispInfo(language.redo)
- end
- end
- elseif p1==15 then -- Tab
- local sx = internalX
- skipRight()
- internalX=sx
- update()
- elseif p1==203 then -- Left
- if shift then
- multiselect=true
- clear()
- sEnd=sEnd or copy(sStart or {line=1,byte=1})
- if sEnd.byte-1<1 then
- if sEnd.line-1 >= 1 then
- sEnd.line=sEnd.line-1
- _line=_line-1
- sEnd.byte=#lines[_line]
- _byte=#lines[_line]
- if _line<offset then
- if _line-1 >= 1 then
- offset=_line
- _byte=#lines[_line]
- _bytepos = 3+(_byte-1)*size+_byte-1
- internalX=size
- draw(lines,offset)
- end
- end
- end
- else
- _byte=_byte-1
- sEnd.byte=sEnd.byte-1
- end
- if _line<offset then
- offset=_line
- draw(lines,offset)
- end
- _bytepos = 3+(_byte-1)*size+_byte-1
- update()
- else
- cursorLeft()
- end
- elseif p1==205 then -- Right
- if shift then
- multiselect=true
- clear()
- sEnd=sEnd or copy(sStart or {line=1,byte=1})
- if lines and lines[sEnd.line] then
- if sEnd.byte+1>#lines[sEnd.line] then
- if sEnd.line+1 <= #lines then
- sEnd.line=sEnd.line+1
- sEnd.byte=1
- _line=_line+1
- _byte=1
- if offset+drawcount <= #lines then
- if _line+1-offset>drawcount then
- offset=offset+1
- _byte=1
- _bytepos = 3+(_byte-1)*size+_byte-1
- internalX=1
- draw(lines,offset)
- end
- end
- end
- else
- _byte=_byte+1
- sEnd.byte=sEnd.byte+1
- end
- end
- if _line+1-offset>drawcount then
- offset=_line
- local drawcount = drawcount
- drawcount=drawcount<=#lines and drawcount or #lines
- if offset+drawcount-1>#lines then
- offset=#lines-drawcount+1
- end
- if offset<1 then
- offset=1
- end
- draw(lines,offset)
- end
- _bytepos = 3+(_byte-1)*size+_byte-1
- update()
- else
- cursorRight()
- end
- elseif p1==200 then -- Up
- if _line-offset>=1 then
- if _line-1 >= 1 then
- clear()
- multiselect=false
- _line=_line-1
- update()
- end
- else
- if offset-1 >= 1 then
- if _line-1 >= 1 then
- clear()
- multiselect=false
- offset=offset-1
- _line=_line-1
- draw(lines,offset)
- update()
- end
- end
- end
- sStart={line=_line,byte=_byte}
- sEnd=nil
- elseif p1==208 then -- Down
- if _line+2-offset<=drawcount then
- if _line+1 < #lines then
- clear()
- multiselect=false
- _line=_line+1
- if lines[_line] and _byte> #lines[_line] then
- _byte=#lines[_line]
- _bytepos = 3+(_byte-1)*size+_byte-1
- end
- update()
- end
- else
- if offset+drawcount <= #lines then
- if _line+1 <= #lines then
- clear()
- multiselect=false
- offset=offset+1
- _line=_line+1
- if _byte> #lines[_line] then
- _byte=#lines[_line]
- _bytepos = 3+(_byte-1)*size+_byte-1
- end
- draw(lines,offset)
- update()
- end
- end
- end
- sStart={line=_line,byte=_byte}
- sEnd=nil
- end
- else
- writemode()
- end
- else
- if p1==14 then -- Backspace
- if poscmd>0 then
- cmd=cmd:sub(1,poscmd-1)..cmd:sub(poscmd+1)
- poscmd=poscmd-1
- recomplete()
- updatecmd()
- end
- elseif p1==203 then -- Left
- if poscmd>0 then
- poscmd=poscmd-1
- recomplete()
- updatecmd()
- end
- elseif p1==205 then -- Right
- if poscmd<#cmd then
- poscmd=poscmd+1
- recomplete()
- updatecmd()
- else
- acceptCompletion()
- end
- elseif p1==211 then -- Delete
- if poscmd<#cmd then
- cmd=cmd:sub(1,poscmd)..cmd:sub(poscmd+2)
- recomplete()
- updatecmd()
- end
- elseif p1 == 200 or p1 == 208 then -- Up, Down
- if nCompletion then
- updatecmd( true )
- if p1 == 200 then
- nCompletion = nCompletion - 1
- if nCompletion < 1 then
- nCompletion = #tCompletions
- end
- elseif p1 == 208 then
- nCompletion = nCompletion + 1
- if nCompletion > #tCompletions then
- nCompletion = 1
- end
- end
- updatecmd()
- end
- elseif p1 == 15 then -- Tab
- acceptCompletion()
- elseif p1 == 199 then -- Start
- poscmd=0
- recomplete()
- updatecmd()
- elseif p1 == 207 then -- End
- poscmd=#cmd
- recomplete()
- updatecmd()
- elseif p1 == 25 then -- p
- if ctrl then
- cmd=cmd:sub(1,poscmd)..clipboard..cmd:sub(poscmd+1)
- poscmd=poscmd+#clipboard
- recomplete()
- updatecmd()
- dispInfo(language.internalpaste)
- end
- elseif p1 == 28 then -- Enter
- poscmd=0
- os.queueEvent("command",tokenise(cmd))
- cmd=""
- recomplete()
- updatecmd()
- end
- end
- elseif event == "char" then
- if editmode then
- if _type.auth:find(p1) then
- local bt = lines[_line] and lines[_line][_byte]
- if bt then
- bt=bt:sub(1,internalX-1)..p1..bt:sub(internalX+1)
- history[hpos]={type="char",info={line=_line,byte=_byte,old=lines[_line][_byte], new=bt, internalX=internalX, bytepos=_bytepos, offset=offset, drawcount=drawcount}}
- hpos=hpos+1
- lines[_line][_byte]=bt
- cursorRight()
- code=""
- for i=1, #lines do
- for j=1, #lines[i] do
- code=code.._type.char(lines[i][j])
- end
- end
- end
- end
- else
- cmd=cmd:sub(1,poscmd)..p1..cmd:sub(poscmd+1)
- poscmd=poscmd+1
- recomplete()
- updatecmd()
- end
- elseif event == "command" then
- local command = p1[1]
- table.remove(p1,1)
- local args=p1
- if command == "insert" then
- local mode = args[1] or "char"
- local olines={}
- for k, v in pairs(lines) do
- olines[k]=copy(v)
- end
- if code and #code>0 then
- local pos=(_line-1)*#lines[1]+_byte
- if mode == "byte" then
- table.remove(args,1)
- local txt=_type.char(unpack(args))
- txt=#txt>0 and txt or "\0"
- code=code:sub(1,pos)..txt..code:sub(pos+1)
- local nlines=calc(code)
- history[hpos]={type="byte",info={oldlines=olines, newlines=nlines, line=_line, byte=_byte, bytepos=_bytepos, internalX=internalX,offset=offset, drawcount=drawcount}}
- lines=nlines
- hpos=hpos+1
- elseif mode == "char" then
- table.remove(args,1)
- local txt=table.concat(args,"")
- txt=#txt>0 and txt or "\0"
- code=code:sub(1,pos)..txt..code:sub(pos+1)
- local nlines=calc(code)
- history[hpos]={type="byte",info={oldlines=olines, newlines=nlines, line=_line, byte=_byte, bytepos=_bytepos, internalX=internalX,offset=offset, drawcount=drawcount}}
- lines=nlines
- hpos=hpos+1
- end
- offset=_line
- local drawcount = drawcount
- drawcount=drawcount<=#lines and drawcount or #lines
- if offset+drawcount-1>#lines then
- offset=#lines-drawcount+1
- end
- if offset<1 then
- offset=1
- end
- draw(lines,offset)
- update()
- if not editmode then
- updatecmd()
- end
- else
- if mode == "byte" then
- table.remove(args,1)
- local txt=_type.char(unpack(args))
- txt=#txt>0 and txt or "\0"
- code = txt
- local nlines=calc(code)
- history[hpos]={type="byte",info={oldlines=olines, newlines=nlines, line=_line, byte=_byte, bytepos=_bytepos, internalX=internalX,offset=offset, drawcount=drawcount}}
- lines=nlines
- _line=1
- _byte=1
- _bytepos = 3+(_byte-1)*size+_byte-1
- hpos=hpos+1
- elseif mode == "char" then
- table.remove(args,1)
- local txt=table.concat(args,"")
- txt=#txt>0 and txt or "\0"
- code=txt
- local nlines=calc(code)
- history[hpos]={type="byte",info={oldlines=olines, newlines=nlines, line=_line, byte=_byte, bytepos=_bytepos, internalX=internalX,offset=offset, drawcount=drawcount}}
- lines=nlines
- _line=1
- _byte=1
- _bytepos = 3+(_byte-1)*size+_byte-1
- hpos=hpos+1
- end
- offset=_line
- local drawcount = drawcount
- drawcount=drawcount<=#lines and drawcount or #lines
- if offset+drawcount-1>#lines then
- offset=#lines-drawcount+1
- end
- if offset<1 then
- offset=1
- end
- draw(lines,offset)
- update()
- if not editmode then
- updatecmd()
- end
- end
- elseif command == "save" then
- if #args>0 then
- h=fs.open(args[1],"w")
- h.write(code)
- h.close()
- else
- h=fs.open(fname,"w")
- h.write(code)
- h.close()
- end
- if not editmode then
- updatecmd()
- end
- if #args>0 then
- dispInfo(language.saveas.." "..args[1])
- else
- dispInfo(language.save)
- end
- elseif command == "exit" then
- buff.setVisible(false)
- term.clear()
- term.setCursorPos(1,1)
- break
- elseif command == "deleteAll" then
- code=""
- sEnd=nil
- multiselect=false
- lines=calc(code)
- draw(lines,offset)
- if not editmode then
- updatecmd()
- end
- elseif command == "clearHistory" then
- history={}
- hpos=1
- elseif command == "redraw" then
- local drawcount = drawcount
- drawcount=drawcount<=#lines and drawcount or #lines
- if offset+drawcount-1>#lines then
- offset=#lines-drawcount+1
- end
- if offset<1 then
- offset=1
- end
- draw(lines,offset)
- update()
- if not editmode then
- updatecmd()
- end
- elseif command == "gotoLn" then
- local ln = tonumber(args[1] or 1)
- if lines[ln] then
- offset=ln
- local drawcount = drawcount
- drawcount=drawcount<=#lines and drawcount or #lines
- if offset+drawcount-1>#lines then
- offset=#lines-drawcount+1
- end
- if offset<1 then
- offset=1
- end
- multiselect=false
- _line=ln
- sStart={line=_line,byte=_byte}
- draw(lines,offset)
- update()
- if not editmode then
- updatecmd()
- end
- end
- elseif command == "color" then
- local color = _colors[args[1]]
- if color then
- local v = colors[args[2]] or color
- _colors[args[1]]=v
- draw(lines,offset)
- update()
- if not editmode then
- updatecmd()
- end
- local dt = {colors=_colors, lang=language}
- local h=fs.open("."..name,"w")
- h.write(textutils.serialise(dt))
- h.close()
- dispInfo(args[1].." : "..revcolor[v])
- end
- elseif command == "editmode" then
- editmode=true
- select=true
- draw(lines,offset)
- update()
- if not editmode then
- updatecmd()
- end
- elseif command == "setClipboard" then
- clipboard=args[1] or ""
- dispInfo(language.clipboardset)
- elseif command == "undo" then
- local count = tonumber(args[1]) or 1
- for i=1, count do
- local h=history[hpos-1]
- if h then
- hpos=hpos-1
- if h.type == "char" then
- clear()
- _line=h.info.line
- _byte=h.info.byte
- internalX=h.info.internalX
- offset=h.info.offset
- _bytepos=h.info.bytepos
- drawcount=h.info.drawcount
- lines[_line][_byte]=h.info.old
- elseif h.type == "byte" then
- lines=h.info.oldlines
- _line=h.info.line
- _byte=h.info.byte
- internalX=h.info.internalX
- _bytepos=h.info.bytepos
- offset=h.info.offset
- drawcount=h.info.drawcount
- code=""
- for i=1, #lines do
- for j=1, #lines[i] do
- code=code.._type.char(lines[i][j])
- end
- end
- end
- end
- end
- draw(lines,offset)
- update()
- if not editmode then
- updatecmd()
- end
- dispInfo(language.undo.." "..count)
- elseif command == "redo" then
- local count = tonumber(args[1]) or 1
- for i=1, count do
- local h=history[hpos]
- if h then
- hpos=hpos+1
- if h.type == "char" then
- clear()
- _line=h.info.line
- _byte=h.info.byte
- internalX=h.info.internalX
- _bytepos=h.info.bytepos
- offset=h.info.offset
- drawcount=h.info.drawcount
- lines[_line][_byte]=h.info.new
- draw(lines,offset)
- update()
- elseif h.type == "byte" then
- code=""
- for i=1, #lines do
- for j=1, #lines[i] do
- code=code.._type.char(lines[i][j])
- end
- end
- lines=h.info.newlines
- _line=h.info.line
- _byte=h.info.byte
- internalX=h.info.internalX
- _bytepos=h.info.bytepos
- offset=h.info.offset
- drawcount=h.info.drawcount
- end
- end
- end
- draw(lines,offset)
- update()
- if not editmode then
- updatecmd()
- end
- dispInfo(language.redo.." "..count)
- elseif command == "mode" then
- _type = mode[args[1] or "byte"] or mode["byte"]
- size = #tostring(_type.byte("\255"))
- sep=math.floor(w/2+w/4)
- for i=1, w-3 do
- if i*(size+1)+i <= w-3 then
- sep=w-i-1
- else
- break
- end
- end
- if #code>0 then
- local pos = charPos(_line, _byte)
- local md = args[1] or "byte"
- md = mode[md] and md or "byte"
- lines=calc(code)
- _line, _byte = bytePos(pos)
- if _line then
- offset=_line
- local drawcount = drawcount
- drawcount=drawcount<=#lines and drawcount or #lines
- if offset+drawcount-1>#lines then
- offset=#lines-drawcount+1
- end
- if offset<1 then
- offset=1
- end
- _bytepos = 3+(_byte-1)*size+_byte-1
- draw(lines,offset)
- update()
- if not editmode then
- updatecmd()
- end
- dispInfo(language.mode..":"..md)
- end
- end
- elseif command == "find" then
- local mode = args[1] or "char"
- local olines={}
- for k, v in pairs(lines) do
- olines[k]=copy(v)
- end
- if #code>0 then
- local pos=(_line-1)*#lines[1]+_byte
- if mode == "byte" then
- table.remove(args,1)
- local txt=_type.char(unpack(args))
- txt=#txt>0 and txt or "\0"
- local pos = charPos(_line,_byte)
- local _s, _e = string.find(code,txt,pos)
- if not _s then
- _s, _e = string.find(code,txt,1)
- end
- if _s then
- local line1, byte1 = bytePos(_s)
- local line2, byte2 = bytePos(_e)
- _byte=byte2
- _line=line2
- _bytepos = 3+(_byte-1)*size+_byte-1
- sStart={line=line1,byte=byte1}
- sEnd={line=line2,byte=byte2}
- offset=_line
- local drawcount = drawcount
- drawcount=drawcount<=#lines and drawcount or #lines
- if offset+drawcount-1>#lines then
- offset=#lines-drawcount+1
- end
- if offset<1 then
- offset=1
- end
- multiselect=true
- draw(lines,offset)
- update()
- if not editmode then
- updatecmd()
- end
- dispInfo(language.found)
- else
- dispInfo(language.notfound)
- end
- elseif mode == "char" then
- table.remove(args,1)
- local txt=table.concat(args,"")
- txt=#txt>0 and txt or "\0"
- local pos = charPos(_line,_byte)
- local _s, _e = string.find(code,txt,pos)
- if not _s then
- _s, _e = string.find(code,txt,1)
- end
- if _s then
- local line1, byte1 = bytePos(_s)
- local line2, byte2 = bytePos(_e)
- _byte=byte2
- _line=line2
- _bytepos = 3+(_byte-1)*size+_byte-1
- offset=_line
- local drawcount = drawcount
- drawcount=drawcount<=#lines and drawcount or #lines
- if offset+drawcount-1>#lines then
- offset=#lines-drawcount+1
- end
- if offset<1 then
- offset=1
- end
- sStart={line=line1,byte=byte1}
- sEnd={line=line2,byte=byte2}
- multiselect=true
- draw(lines,offset)
- update()
- if not editmode then
- updatecmd()
- end
- dispInfo(language.found)
- else
- dispInfo(language.notfound)
- end
- end
- end
- elseif command == "lang" then
- local langname = args[1] or "en"
- language = lang[langname] or lang["en"]
- langname = lang[langname] and langname or "en"
- local dt = {colors=_colors, lang=language}
- local h=fs.open("."..name,"w")
- h.write(textutils.serialise(dt))
- h.close()
- dispInfo(language.langset.." "..langname)
- elseif command == "help" then
- if language.docs[args[1]] then
- wind.setVisible(true)
- wind.setBackgroundColor(_colors.background)
- wind.setTextColor(_colors.text)
- wind.clear()
- wind.setCursorPos(1,1)
- termprint(wind, language.help.." : "..language.docs[args[1]])
- termprint(wind,"")
- for k, v in pairs(language.usages[args[1]]) do
- termprint(wind,language.usage.." : "..v)
- end
- paged=true
- modeinfo=true
- if not editmode then
- updatecmd()
- end
- else
- local cmd={}
- for k, v in pairs(tCompletionInfo) do
- cmd[#cmd+1]=k
- end
- table.sort(cmd)
- infodt = cmd
- updateInfo()
- end
- elseif command == "bindlist" then
- local lst = {}
- for k, v in pairs(language.binds) do
- lst[v]=k
- end
- paged=false
- infodt=lst
- updateInfo()
- end
- end
- end
- else
- local t={}
- for k, v in pairs(mode) do
- t[#t+1]=k
- end
- term.clear()
- term.setCursorPos(1,1)
- table.sort(t)
- print("Usage: "..shell.getRunningProgram().." <filename> <mode["..table.concat(t,",").."]> <readMode[rb,r]>")
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement