JustDoesGames

Default Edit Program Compressed

Aug 13th, 2020
1,557
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.81 KB | None | 0 0
  1. local a={...}if#a==0 then print("Usage: edit <path>")return end;local b=shell.resolve(a[1])local c=fs.isReadOnly(b)if fs.exists(b)and fs.isDir(b)then print("Cannot edit a directory.")return end;if not fs.exists(b)and not string.find(b,"%.")then local d=settings.get("edit.default_extension","")if d~=""and type(d)=="string"then b=b.."."..d end end;local e,f=1,1;local g,h=term.getSize()local i,j=0,0;local k={}local l=true;local m,n,o,p,q,r;if term.isColour()then q=colours.black;p=colours.white;m=colours.yellow;n=colours.yellow;o=colours.green;r=colours.red else q=colours.black;p=colours.white;m=colours.white;n=colours.white;o=colours.white;r=colours.white end;local s=false;local t=1;local u={}if not c then table.insert(u,"Save")end;if shell.openTab then table.insert(u,"Run")end;if peripheral.find("printer")then table.insert(u,"Print")end;table.insert(u,"Exit")local v="Press Ctrl to access menu"if string.len(v)>g-5 then v="Press Ctrl for menu"end;local function w(x)k={}if fs.exists(x)then local y=io.open(x,"r")local z=y:read()while z do table.insert(k,z)z=y:read()end;y:close()end;if#k==0 then table.insert(k,"")end end;local function A(x)local B=x:sub(1,x:len()-fs.getName(x):len())if not fs.exists(B)then fs.makeDir(B)end;local y=nil;local function C()y=fs.open(x,"w")if y then for D,z in ipairs(k)do y.write(z.."\n")end else error("Failed to open "..x)end end;local E,F=pcall(C)if y then y.close()end;return E,F end;local G={["and"]=true,["break"]=true,["do"]=true,["else"]=true,["elseif"]=true,["end"]=true,["false"]=true,["for"]=true,["function"]=true,["if"]=true,["in"]=true,["local"]=true,["nil"]=true,["not"]=true,["or"]=true,["repeat"]=true,["return"]=true,["then"]=true,["true"]=true,["until"]=true,["while"]=true}local function H(z,I,J)local K=string.match(z,I)if K then if type(J)=="number"then term.setTextColour(J)else term.setTextColour(J(K))end;term.write(K)term.setTextColour(p)return string.sub(z,string.len(K)+1)end;return nil end;local function L(z)while string.len(z)>0 do z=H(z,"^%-%-%[%[.-%]%]",o)or H(z,"^%-%-.*",o)or H(z,"^\"\"",r)or H(z,"^\".-[^\\]\"",r)or H(z,"^\'\'",r)or H(z,"^\'.-[^\\]\'",r)or H(z,"^%[%[.-%]%]",r)or H(z,"^[%w_]+",function(K)if G[K]then return n end;return p end)or H(z,"^[^%w_]",p)end end;local M;local N;local O=_ENV;local function P(z)if settings.get("edit.autocomplete")then local Q=string.find(z,"[a-zA-Z0-9_%.:]+$")if Q then z=string.sub(z,Q)end;if#z>0 then return textutils.complete(z,O)end end;return nil end;local function R()local z=k[f]if not s and not c and e==string.len(z)+1 then M=P(z)if M and#M>0 then N=1 else N=nil end else M=nil;N=nil end end;local function S(z)if N then local T=M[N]term.setTextColor(colours.white)term.setBackgroundColor(colours.grey)term.write(T)term.setTextColor(p)term.setBackgroundColor(q)end end;local function U()local V,W=e,f;for f=1,h-1 do term.setCursorPos(1-i,f)term.clearLine()local z=k[f+j]if z~=nil then L(z)if W==f and V==#z+1 then S()end end end;term.setCursorPos(e-i,f-j)end;local function X(Y)local z=k[Y]if z then term.setCursorPos(1-i,Y-j)term.clearLine()L(z)if Y==f and e==#z+1 then S()end;term.setCursorPos(e-i,Y-j)end end;local function Z()term.setCursorPos(1,h)term.clearLine()term.setCursorPos(g-string.len("Ln "..f)+1,h)term.setTextColour(m)term.write("Ln ")term.setTextColour(p)term.write(f)term.setCursorPos(1,h)if s then term.setTextColour(p)for _,a0 in pairs(u)do if _==t then term.setTextColour(m)term.write("[")term.setTextColour(p)term.write(a0)term.setTextColour(m)term.write("]")term.setTextColour(p)else term.write(" "..a0 .." ")end end else term.setTextColour(m)term.write(v)term.setTextColour(p)end;term.setCursorPos(e-i,f-j)end;local a1={Save=function()if c then v="Access denied"else local E,F=A(b)if E then v="Saved to "..b else v="Error saving to "..b end end;Z()end,Print=function()local a2=peripheral.find("printer")if not a2 then v="No printer attached"return end;local a3=0;local a4=fs.getName(b)if a2.getInkLevel()<1 then v="Printer out of ink"return elseif a2.getPaperLevel()<1 then v="Printer out of paper"return end;local a5=term.current()local a6={getCursorPos=a2.getCursorPos,setCursorPos=a2.setCursorPos,getSize=a2.getPageSize,write=a2.write}a6.scroll=function()if a3==1 then a2.setPageTitle(a4 .." (page "..a3 ..")")end;while not a2.newPage()do if a2.getInkLevel()<1 then v="Printer out of ink, please refill"elseif a2.getPaperLevel()<1 then v="Printer out of paper, please refill"else v="Printer output tray full, please empty"end;term.redirect(a5)Z()term.redirect(a6)local a7=os.startTimer(0.5)sleep(0.5)end;a3=a3+1;if a3==1 then a2.setPageTitle(a4)else a2.setPageTitle(a4 .." (page "..a3 ..")")end end;s=false;term.redirect(a6)local E,error=pcall(function()term.scroll()for D,z in ipairs(k)do print(z)end end)term.redirect(a5)if not E then print(error)end;while not a2.endPage()do v="Printer output tray full, please empty"Z()sleep(0.5)end;s=true;if a3>1 then v="Printed "..a3 .." Pages"else v="Printed 1 Page"end;Z()end,Exit=function()l=false end,Run=function()local a8="/.temp"local E,F=A(a8)if E then local a9=shell.openTab(a8)if a9 then shell.switchTab(a9)else v="Error starting Task"end;fs.delete(a8)else v="Error saving to "..a8 end;Z()end}local function aa(ab)a1[u[ab]]()if s then s=false;term.setCursorBlink(true)end;Z()end;local function ac(ad,ae)local af,ag=e,f;e,f=ad,ae;local ah=e-i;local ai=f-j;local aj=false;if ah<1 then i=e-1;ah=1;aj=true elseif ah>g then i=e-g;ah=g;aj=true end;if ai<1 then j=f-1;ai=1;aj=true elseif ai>h-1 then j=f-(h-1)ai=h-1;aj=true end;R()if aj then U()elseif f~=ag then X(ag)X(f)else X(f)end;term.setCursorPos(ah,ai)Z()end;w(b)term.setBackgroundColour(q)term.clear()term.setCursorPos(e,f)term.setCursorBlink(true)R()U()Z()local function ak()if N then local T=M[N]k[f]=k[f]..T;ac(e+string.len(T),f)end end;while l do local al,am,an,ao=os.pullEvent()if al=="key"then local af,ag=e,f;if am==keys.up then if not s then if N then N=N-1;if N<1 then N=#M end;X(f)elseif f>1 then ac(math.min(e,string.len(k[f-1])+1),f-1)end end elseif am==keys.down then if not s then if N then N=N+1;if N>#M then N=1 end;X(f)elseif f<#k then ac(math.min(e,string.len(k[f+1])+1),f+1)end end elseif am==keys.tab then if not s and not c then if N and e==string.len(k[f])+1 then ak()else local z=k[f]k[f]=string.sub(z,1,e-1).."    "..string.sub(z,e)ac(e+4,f)end end elseif am==keys.pageUp then if not s then local ae;if f-(h-1)>=1 then ae=f-(h-1)else ae=1 end;ac(math.min(e,string.len(k[ae])+1),ae)end elseif am==keys.pageDown then if not s then local ae;if f+h-1<=#k then ae=f+h-1 else ae=#k end;local ad=math.min(e,string.len(k[ae])+1)ac(ad,ae)end elseif am==keys.home then if not s then if e>1 then ac(1,f)end end elseif am==keys["end"]then if not s then local ap=string.len(k[f])+1;if e<ap then ac(ap,f)end end elseif am==keys.left then if not s then if e>1 then ac(e-1,f)elseif e==1 and f>1 then ac(string.len(k[f-1])+1,f-1)end else t=t-1;if t<1 then t=#u end;Z()end elseif am==keys.right then if not s then local ap=string.len(k[f])+1;if e<ap then ac(e+1,f)elseif N and e==string.len(k[f])+1 then ak()elseif e==ap and f<#k then ac(1,f+1)end else t=t+1;if t>#u then t=1 end;Z()end elseif am==keys.delete then if not s and not c then local ap=string.len(k[f])+1;if e<ap then local z=k[f]k[f]=string.sub(z,1,e-1)..string.sub(z,e+1)R()X(f)elseif f<#k then k[f]=k[f]..k[f+1]table.remove(k,f+1)R()U()end end elseif am==keys.backspace then if not s and not c then if e>1 then local z=k[f]if e>4 and string.sub(z,e-4,e-1)=="    "and not string.sub(z,1,e-1):find("%S")then k[f]=string.sub(z,1,e-5)..string.sub(z,e)ac(e-4,f)else k[f]=string.sub(z,1,e-2)..string.sub(z,e)ac(e-1,f)end elseif f>1 then local aq=string.len(k[f-1])k[f-1]=k[f-1]..k[f]table.remove(k,f)ac(aq+1,f-1)U()end end elseif am==keys.enter then if not s and not c then local z=k[f]local ar,as=string.find(z,"^[ ]+")if not as then as=0 end;k[f]=string.sub(z,1,e-1)table.insert(k,f+1,string.rep(' ',as)..string.sub(z,e))ac(as+1,f+1)U()elseif s then aa(t)end elseif am==keys.leftCtrl or am==keys.rightCtrl or am==keys.rightAlt then s=not s;if s then term.setCursorBlink(false)else term.setCursorBlink(true)end;Z()end elseif al=="char"then if not s and not c then local z=k[f]k[f]=string.sub(z,1,e-1)..am..string.sub(z,e)ac(e+1,f)elseif s then for D,at in ipairs(u)do if string.lower(string.sub(at,1,1))==string.lower(am)then aa(D)break end end end elseif al=="paste"then if not c then if s then s=false;term.setCursorBlink(true)Z()end;local z=k[f]k[f]=string.sub(z,1,e-1)..am..string.sub(z,e)ac(e+string.len(am),f)end elseif al=="mouse_click"then if not s then if am==1 then local au,av=an,ao;if av<h then local ae=math.min(math.max(j+av,1),#k)local ad=math.min(math.max(i+au,1),string.len(k[ae])+1)ac(ad,ae)end end end elseif al=="mouse_scroll"then if not s then if am==-1 then if j>0 then j=j-1;U()end elseif am==1 then local aw=#k-(h-1)if j<aw then j=j+1;U()end end end elseif al=="term_resize"then g,h=term.getSize()ac(e,f)Z()U()end end;term.clear()term.setCursorBlink(false)term.setCursorPos(1,1)
Advertisement
Add Comment
Please, Sign In to add comment