Advertisement
9551

shell

Aug 6th, 2021
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.44 KB | None | 0 0
  1. local e=dofile("rom/modules/main/cc/expect.lua").expect local
  2. t=dofile("rom/modules/main/cc/require.lua").make local a=multishell local
  3. o=shell local i=term.current()if a then a.setTitle(a.getCurrent(),"shell")end
  4. local n=false local s=o and o.dir()or""local h=o and
  5. o.path()or".:/rom/programs"local r=o and o.aliases()or{}local d=o and
  6. o.getCompletionInfo()or{}local l={}local u={}local function c(m)local
  7. f={shell=u,multishell=a}f.require,f.package=t(f,m)return f end local w,y,p if
  8. term.isColour()then w=colours.yellow y=colours.white p=colours.black else
  9. w=colours.white y=colours.white p=colours.black end function
  10. u.execute(v,...)e(1,v,"string")for b=1,select('#',...)do
  11. e(b+1,select(b,...),"string")end local h=u.resolveProgram(v)if h~=nil then
  12. l[#l+1]=h if a then local g=fs.getName(h)if g:sub(-4)==".lua"then
  13. g=g:sub(1,-5)end a.setTitle(a.getCurrent(),g)end local s=fs.getDir(h)local
  14. k=c(s)k.arg={[0]=v,...}local q=os.run(k,h,...)l[#l]=nil if a then if#l>0 then
  15. local j=fs.getName(l[#l])if j:sub(-4)==".lua"then j=j:sub(1,-5)end
  16. a.setTitle(a.getCurrent(),j)else a.setTitle(a.getCurrent(),"shell")end end
  17. return q else printError("No such program")return false end end local function
  18. x(...)local z=table.concat({...}," ")local E={}local T=false for A in
  19. string.gmatch(z.."\"","(.-)\"")do if T then table.insert(E,A)else for O in
  20. string.gmatch(A,"[^ \t]+")do table.insert(E,O)end end T=not T end return E end
  21. function u.run(...)local I=x(...)local N=I[1]if N then return
  22. u.execute(N,table.unpack(I,2))end return false end function u.exit()n=true end
  23. function u.dir()return s end function u.setDir(S)e(1,S,"string")if not
  24. fs.isDir(S)then error("Not a directory",2)end s=fs.combine(S,"")end function
  25. u.path()return h end function u.setPath(H)e(1,H,"string")h=H end function
  26. u.resolve(R)e(1,R,"string")local D=string.sub(R,1,1)if D=="/"or D=="\\"then
  27. return fs.combine("",R)else return fs.combine(s,R)end end local function
  28. L(U,C)local M=#h local F=string.sub(U,M,M)if F=="/"or F=="\\"then
  29. U=string.sub(U,1,M-1)end return U.."."..C end function
  30. u.resolveProgram(W)e(1,W,"string")if r[W]~=nil then W=r[W]end if W:find("/")or
  31. W:find("\\")then local h=u.resolve(W)if fs.exists(h)and not fs.isDir(h)then
  32. return h else local Y=L(h,"lua")if fs.exists(Y)and not fs.isDir(Y)then return Y
  33. end end return nil end for h in string.gmatch(h,"[^:]+")do
  34. h=fs.combine(u.resolve(h),W)if fs.exists(h)and not fs.isDir(h)then return h
  35. else local P=L(h,"lua")if fs.exists(P)and not fs.isDir(P)then return P end end
  36. end return nil end function u.programs(V)e(1,V,"boolean","nil")local B={}for h
  37. in string.gmatch(h,"[^:]+")do h=u.resolve(h)if fs.isDir(h)then local
  38. G=fs.list(h)for K=1,#G do local Q=G[K]if not fs.isDir(fs.combine(h,Q))and(V or
  39. string.sub(Q,1,1)~=".")then if#Q>4 and Q:sub(-4)==".lua"then Q=Q:sub(1,-5)end
  40. B[Q]=true end end end end local J={}for X in pairs(B)do table.insert(J,X)end
  41. table.sort(J)return J end local function Z(et)if#et>0 and(et:find("/")or
  42. et:find("\\"))then return fs.complete(et,s,true,false)else local tt={}local
  43. at={}for ot in pairs(r)do if#ot>#et and string.sub(ot,1,#et)==et then local
  44. it=string.sub(ot,#et+1)if not at[it]then table.insert(tt,it)at[it]=true end end
  45. end local nt=fs.complete(et,s,false,false)for st=1,#nt do local ht=nt[st]if not
  46. at[ht]then table.insert(tt,ht)at[ht]=true end end local rt=u.programs()for
  47. dt=1,#rt do local lt=rt[dt]if#lt>#et and string.sub(lt,1,#et)==et then local
  48. ut=string.sub(lt,#et+1)if not at[ut]then table.insert(tt,ut)at[ut]=true end end
  49. end table.sort(tt)return tt end end local function ct(mt,ft,wt,yt)local
  50. pt=d[mt]if pt then return pt.fnComplete(u,ft,wt,yt)end return nil end function
  51. u.complete(vt)e(1,vt,"string")if#vt>0 then local bt=x(vt)local gt=#bt if
  52. string.sub(vt,#vt,#vt)==" "then gt=gt+1 end if gt==1 then local
  53. kt=bt[1]or""local h=u.resolveProgram(kt)if d[h]then return{" "}else local
  54. qt=Z(kt)for jt=1,#qt do local xt=qt[jt]local h=u.resolveProgram(kt..xt)if
  55. d[h]then qt[jt]=xt.." "end end return qt end elseif gt>1 then local
  56. h=u.resolveProgram(bt[1])local zt=bt[gt]or""local Et=bt Et[gt]=nil return
  57. ct(h,gt-1,zt,Et)end end return nil end function
  58. u.completeProgram(Tt)e(1,Tt,"string")return Z(Tt)end function
  59. u.setCompletionFunction(At,Ot)e(1,At,"string")e(2,Ot,"function")d[At]={fnComplete=Ot,}end
  60. function u.getCompletionInfo()return d end function u.getRunningProgram()if#l>0
  61. then return l[#l]end return nil end function
  62. u.setAlias(It,Nt)e(1,It,"string")e(2,Nt,"string")r[It]=Nt end function
  63. u.clearAlias(St)e(1,St,"string")r[St]=nil end function u.aliases()local
  64. Ht={}for Rt,Dt in pairs(r)do Ht[Rt]=Dt end return Ht end if a then function
  65. u.openTab(...)local Lt=x(...)local Ut=Lt[1]if Ut then local
  66. h=u.resolveProgram(Ut)if h=="rom/programs/shell.lua"then return
  67. a.launch(c("rom/programs"),h,table.unpack(Lt,2))elseif h~=nil then return
  68. a.launch(c("rom/programs"),"rom/programs/shell.lua",Ut,table.unpack(Lt,2))else
  69. printError("No such program")end end end function
  70. u.switchTab(Ct)e(1,Ct,"number")a.setFocus(Ct)end end local Mt={...}if#Mt>0 then
  71. u.run(...)else
  72. term.setBackgroundColor(p)term.setTextColour(w)print(os.version())term.setTextColour(y)if
  73. o==nil then u.run("/rom/startup.lua")end local Ft={}while not n do
  74. term.redirect(i)term.setBackgroundColor(p)term.setTextColour(w)write(u.dir().."> ")term.setTextColour(y)local
  75. Wt if settings.get("shell.autocomplete")then Wt=read(nil,Ft,u.complete)else
  76. Wt=read(nil,Ft)end if Wt:match("%S")and Ft[#Ft]~=Wt then table.insert(Ft,Wt)end
  77. u.shell.openTab(Wt)end
  78. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement