Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local e=dofile("rom/modules/main/cc/expect.lua").expect local
- t=dofile("rom/modules/main/cc/require.lua").make local a=multishell local
- o=shell local i=term.current()if a then a.setTitle(a.getCurrent(),"shell")end
- local n=false local s=o and o.dir()or""local h=o and
- o.path()or".:/rom/programs"local r=o and o.aliases()or{}local d=o and
- o.getCompletionInfo()or{}local l={}local u={}local function c(m)local
- f={shell=u,multishell=a}f.require,f.package=t(f,m)return f end local w,y,p if
- term.isColour()then w=colours.yellow y=colours.white p=colours.black else
- w=colours.white y=colours.white p=colours.black end function
- u.execute(v,...)e(1,v,"string")for b=1,select('#',...)do
- e(b+1,select(b,...),"string")end local h=u.resolveProgram(v)if h~=nil then
- l[#l+1]=h if a then local g=fs.getName(h)if g:sub(-4)==".lua"then
- g=g:sub(1,-5)end a.setTitle(a.getCurrent(),g)end local s=fs.getDir(h)local
- k=c(s)k.arg={[0]=v,...}local q=os.run(k,h,...)l[#l]=nil if a then if#l>0 then
- local j=fs.getName(l[#l])if j:sub(-4)==".lua"then j=j:sub(1,-5)end
- a.setTitle(a.getCurrent(),j)else a.setTitle(a.getCurrent(),"shell")end end
- return q else printError("No such program")return false end end local function
- x(...)local z=table.concat({...}," ")local E={}local T=false for A in
- string.gmatch(z.."\"","(.-)\"")do if T then table.insert(E,A)else for O in
- string.gmatch(A,"[^ \t]+")do table.insert(E,O)end end T=not T end return E end
- function u.run(...)local I=x(...)local N=I[1]if N then return
- u.execute(N,table.unpack(I,2))end return false end function u.exit()n=true end
- function u.dir()return s end function u.setDir(S)e(1,S,"string")if not
- fs.isDir(S)then error("Not a directory",2)end s=fs.combine(S,"")end function
- u.path()return h end function u.setPath(H)e(1,H,"string")h=H end function
- u.resolve(R)e(1,R,"string")local D=string.sub(R,1,1)if D=="/"or D=="\\"then
- return fs.combine("",R)else return fs.combine(s,R)end end local function
- L(U,C)local M=#h local F=string.sub(U,M,M)if F=="/"or F=="\\"then
- U=string.sub(U,1,M-1)end return U.."."..C end function
- u.resolveProgram(W)e(1,W,"string")if r[W]~=nil then W=r[W]end if W:find("/")or
- W:find("\\")then local h=u.resolve(W)if fs.exists(h)and not fs.isDir(h)then
- return h else local Y=L(h,"lua")if fs.exists(Y)and not fs.isDir(Y)then return Y
- end end return nil end for h in string.gmatch(h,"[^:]+")do
- h=fs.combine(u.resolve(h),W)if fs.exists(h)and not fs.isDir(h)then return h
- else local P=L(h,"lua")if fs.exists(P)and not fs.isDir(P)then return P end end
- end return nil end function u.programs(V)e(1,V,"boolean","nil")local B={}for h
- in string.gmatch(h,"[^:]+")do h=u.resolve(h)if fs.isDir(h)then local
- G=fs.list(h)for K=1,#G do local Q=G[K]if not fs.isDir(fs.combine(h,Q))and(V or
- string.sub(Q,1,1)~=".")then if#Q>4 and Q:sub(-4)==".lua"then Q=Q:sub(1,-5)end
- B[Q]=true end end end end local J={}for X in pairs(B)do table.insert(J,X)end
- table.sort(J)return J end local function Z(et)if#et>0 and(et:find("/")or
- et:find("\\"))then return fs.complete(et,s,true,false)else local tt={}local
- at={}for ot in pairs(r)do if#ot>#et and string.sub(ot,1,#et)==et then local
- it=string.sub(ot,#et+1)if not at[it]then table.insert(tt,it)at[it]=true end end
- end local nt=fs.complete(et,s,false,false)for st=1,#nt do local ht=nt[st]if not
- at[ht]then table.insert(tt,ht)at[ht]=true end end local rt=u.programs()for
- dt=1,#rt do local lt=rt[dt]if#lt>#et and string.sub(lt,1,#et)==et then local
- ut=string.sub(lt,#et+1)if not at[ut]then table.insert(tt,ut)at[ut]=true end end
- end table.sort(tt)return tt end end local function ct(mt,ft,wt,yt)local
- pt=d[mt]if pt then return pt.fnComplete(u,ft,wt,yt)end return nil end function
- u.complete(vt)e(1,vt,"string")if#vt>0 then local bt=x(vt)local gt=#bt if
- string.sub(vt,#vt,#vt)==" "then gt=gt+1 end if gt==1 then local
- kt=bt[1]or""local h=u.resolveProgram(kt)if d[h]then return{" "}else local
- qt=Z(kt)for jt=1,#qt do local xt=qt[jt]local h=u.resolveProgram(kt..xt)if
- d[h]then qt[jt]=xt.." "end end return qt end elseif gt>1 then local
- h=u.resolveProgram(bt[1])local zt=bt[gt]or""local Et=bt Et[gt]=nil return
- ct(h,gt-1,zt,Et)end end return nil end function
- u.completeProgram(Tt)e(1,Tt,"string")return Z(Tt)end function
- u.setCompletionFunction(At,Ot)e(1,At,"string")e(2,Ot,"function")d[At]={fnComplete=Ot,}end
- function u.getCompletionInfo()return d end function u.getRunningProgram()if#l>0
- then return l[#l]end return nil end function
- u.setAlias(It,Nt)e(1,It,"string")e(2,Nt,"string")r[It]=Nt end function
- u.clearAlias(St)e(1,St,"string")r[St]=nil end function u.aliases()local
- Ht={}for Rt,Dt in pairs(r)do Ht[Rt]=Dt end return Ht end if a then function
- u.openTab(...)local Lt=x(...)local Ut=Lt[1]if Ut then local
- h=u.resolveProgram(Ut)if h=="rom/programs/shell.lua"then return
- a.launch(c("rom/programs"),h,table.unpack(Lt,2))elseif h~=nil then return
- a.launch(c("rom/programs"),"rom/programs/shell.lua",Ut,table.unpack(Lt,2))else
- printError("No such program")end end end function
- u.switchTab(Ct)e(1,Ct,"number")a.setFocus(Ct)end end local Mt={...}if#Mt>0 then
- u.run(...)else
- term.setBackgroundColor(p)term.setTextColour(w)print(os.version())term.setTextColour(y)if
- o==nil then u.run("/rom/startup.lua")end local Ft={}while not n do
- term.redirect(i)term.setBackgroundColor(p)term.setTextColour(w)write(u.dir().."> ")term.setTextColour(y)local
- Wt if settings.get("shell.autocomplete")then Wt=read(nil,Ft,u.complete)else
- Wt=read(nil,Ft)end if Wt:match("%S")and Ft[#Ft]~=Wt then table.insert(Ft,Wt)end
- u.shell.openTab(Wt)end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement