Advertisement
tima_gt

tde-terminal 3.4-alpha

Aug 19th, 2014
282
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 24.51 KB | None | 0 0
  1. --this is not tde-terminal, this is advanced shell from computercraft forum
  2. local parentShell = shell
  3.  
  4. local bExit = false
  5. local sDir = (parentShell and parentShell.dir and parentShell.dir()) or ""
  6. local sPath = (parentShell and parentShell.path and parentShell.path()) or ".:/rom/programs"
  7. local tAliases = (parentShell and parentShell.aliases and parentShell.aliases()) or {}
  8. local tProgramStack = {}
  9.  
  10. local bClock = true
  11.  
  12. local saveDir= (parentShell and parentShell.getRunningProgram and parentShell.getRunningProgram()) or "/"
  13. saveDir=string.sub(saveDir,1,#saveDir-#fs.getName(saveDir))
  14. if fs.isReadOnly(saveDir) then saveDir="/" end
  15.  
  16.  
  17. local shell = {}
  18. local tEnv = {
  19.     ["shell"] = shell
  20. }
  21.  
  22. --[[ Colours  --]]
  23. local promptColour, textColour, bgColour
  24. if term.isColour() then
  25.     promptColour = colours.yellow
  26.     textColour = colours.white
  27.     bgColour = colours.black
  28. else
  29.     promptColour = colours.white
  30.     textColour = colours.white
  31.     bgColour = colours.black
  32. end
  33.  
  34. --[[ Standard Shell Functions --]]
  35.  
  36. local function run( _sCommand, ... )
  37.     local sPath = shell.resolveProgram( _sCommand )
  38.     if sPath ~= nil then
  39.         tProgramStack[#tProgramStack + 1] = sPath
  40.         local result = os.run( tEnv, sPath, ... )
  41.         tProgramStack[#tProgramStack] = nil
  42.         return result
  43.     else
  44.         printError( "No such program" )
  45.         return false
  46.     end
  47. end
  48.  
  49. local function runLine( _sLine )
  50.     local tWords = {}
  51.     for match in string.gmatch( _sLine, "[^ \t]+" ) do
  52.         table.insert( tWords, match )
  53.     end
  54.     local sCommand = tWords[1]
  55.     if sCommand then
  56.         return run( sCommand, unpack( tWords, 2 ) )
  57.     end
  58.     return false
  59. end
  60.  
  61. --[[ Install shell API  --]]
  62. function shell.run( ... )
  63.     return runLine(table.concat( { ... }, " " ))
  64. end
  65.  
  66. function shell.exit()
  67.     bExit = true
  68. end
  69.  
  70. function shell.dir()
  71.     return sDir
  72. end
  73.  
  74. function shell.setDir( _sDir )
  75.     sDir = _sDir
  76. end
  77.  
  78. function shell.path()
  79.     return sPath
  80. end
  81.  
  82. function shell.setPath( _sPath )
  83.     sPath = _sPath
  84. end
  85.  
  86. function shell.resolve( _sPath )
  87.     local sStartChar = string.sub( _sPath, 1, 1 )
  88.     if sStartChar == "/" or sStartChar == "\\" then
  89.         return fs.combine( "", _sPath )
  90.     else
  91.         return fs.combine( sDir, _sPath )
  92.     end
  93. end
  94.  
  95. function shell.resolveProgram( _sCommand )
  96.     --[[ Substitute aliases firsts  --]]
  97.     if tAliases[ _sCommand ] ~= nil then
  98.         _sCommand = tAliases[ _sCommand ]
  99.     end
  100.  
  101.     --[[ If the path is a global path, use it directly  --]]
  102.     local sStartChar = string.sub( _sCommand, 1, 1 )
  103.     if sStartChar == "/" or sStartChar == "\\" then
  104.         local sPath = fs.combine( "", _sCommand )
  105.         if fs.exists( sPath ) and not fs.isDir( sPath ) then
  106.             return sPath
  107.         end
  108.         return nil
  109.     end
  110.    
  111.     --[[  Otherwise, look on the path variable  --]]
  112.     for sPath in string.gmatch(sPath, "[^:]+") do
  113.         sPath = fs.combine( shell.resolve( sPath ), _sCommand )
  114.         if fs.exists( sPath ) and not fs.isDir( sPath ) then
  115.             return sPath
  116.         end
  117.     end
  118.    
  119.     --[[ Not found  --]]
  120.     return nil
  121. end
  122.  
  123. function shell.programs( _bIncludeHidden )
  124.     local tItems = {}
  125.    
  126.     --[[ Add programs from the path  --]]
  127.     for sPath in string.gmatch(sPath, "[^:]+") do
  128.         sPath = shell.resolve( sPath )
  129.         if fs.isDir( sPath ) then
  130.             local tList = fs.list( sPath )
  131.             for n,sFile in pairs( tList ) do
  132.                 if not fs.isDir( fs.combine( sPath, sFile ) ) and
  133.                    (_bIncludeHidden or string.sub( sFile, 1, 1 ) ~= ".") then
  134.                     tItems[ sFile ] = true
  135.                 end
  136.             end
  137.         end
  138.     end
  139.  
  140.     --[[ Sort and return  --]]
  141.     local tItemList = {}
  142.     for sItem, b in pairs( tItems ) do
  143.         table.insert( tItemList, sItem )
  144.     end
  145.     table.sort( tItemList )
  146.     return tItemList
  147. end
  148.  
  149. function shell.getRunningProgram()
  150.     if #tProgramStack > 0 then
  151.         return tProgramStack[#tProgramStack]
  152.     end
  153.     return nil
  154. end
  155.  
  156. function shell.setAlias( _sCommand, _sProgram )
  157.     tAliases[ _sCommand ] = _sProgram
  158. end
  159.  
  160. function shell.clearAlias( _sCommand )
  161.     tAliases[ _sCommand ] = nil
  162. end
  163.  
  164. function shell.aliases()
  165.     --[[ Add aliases  --]]
  166.     local tCopy = {}
  167.     for sAlias, sCommand in pairs( tAliases ) do
  168.         tCopy[sAlias] = sCommand
  169.     end
  170.     return tCopy
  171. end
  172.  
  173.  
  174. --[[ My Functions --]] --[[ My Functions --]] --[[ My Functions --]]
  175.  
  176. --[[ File Useage --]]
  177. local function save(A,B) local file = fs.open(tostring(A),"w") file.write(B) file.close() end
  178. local function saveT(A,B) save(A,textutils.serialize(B)) end
  179. local function saveTH(A,B) save(A,string.gsub(textutils.serialize(B),"},","},\r\n")) end
  180. local function get(A) local file = fs.open(tostring(A),"r") if not file then return false end local data = file.readAll() file.close() if data then return data end end
  181. local function getT(A) local data = get(A) if data then data = textutils.unserialize(data) end if data then return data end end
  182.  
  183. -- pastebin code
  184.  
  185. local pastebin={}
  186. pastebin={
  187. api_user_key = false,
  188. api_dev_key="6ee24782806f592dc9b3be30960fe117",
  189. login=function(api_user_name,api_user_password)
  190.     handle=http.post("http://pastebin.com/api/api_login.php","api_user_name="..textutils.urlEncode(api_user_name).."&api_user_password="..textutils.urlEncode(api_user_password).."&api_dev_key="..textutils.urlEncode(pastebin.api_dev_key))
  191.     local out=handle.readAll()
  192.     handle.close()
  193.     return out
  194. end,
  195. listall=function()
  196.     handle=http.post("http://pastebin.com/api/api_post.php","api_user_key="..textutils.urlEncode(pastebin.api_user_key).."&api_results_limit="..textutils.urlEncode(tostring(1000)).."&api_dev_key="..textutils.urlEncode(pastebin.api_dev_key).."&api_option=list")
  197.     local out=handle.readAll()
  198.     handle.close()
  199.     return out
  200. end,
  201. user=function()
  202.     handle=http.post("http://pastebin.com/api/api_post.php","api_user_key="..textutils.urlEncode(pastebin.api_user_key).."&api_dev_key="..textutils.urlEncode(pastebin.api_dev_key).."&api_option=userdetails")
  203.     local out=handle.readAll()
  204.     handle.close()
  205.     return out
  206. end,
  207. tabulate=function(A)
  208.     local out = {}
  209.     for i, k in string.gmatch(A, "<(.-)>(.-)</%1>") do
  210.         local temp = {}
  211.         for j, l in string.gmatch(k, "<"..i.."_(.-)>(.-)</"..i.."_%1>") do
  212.             temp[j] = l
  213.         end
  214.         table.insert(out,temp)
  215.     end
  216.     return out
  217. end,
  218. getone=function(code)
  219.     handle=http.get("http://pastebin.com/raw.php?i="..code)
  220.     if not handle then return nil end
  221.     local out=handle.readAll()
  222.     handle.close()
  223.     return out
  224. end
  225. }
  226.  
  227. --[[ Read without any writes() - visually conceals leanght of password from wiever --]]
  228. local function saferead()
  229.     term.setCursorBlink( true )
  230.     local sLine = ""
  231.     local nPos = 0
  232.     local w, h = term.getSize()
  233.     local sx, sy = term.getCursorPos()     
  234.     while true do
  235.         local sEvent, param = os.pullEvent()
  236.         if sEvent == "char" then
  237.             sLine = string.sub( sLine, 1, nPos ) .. param .. string.sub( sLine, nPos + 1 )
  238.             nPos = nPos + 1
  239.         elseif sEvent == "key" then
  240.             if param == keys.enter then
  241.                 --[[ Enter --]]
  242.                 break
  243.             elseif param == keys.backspace then
  244.                 --[[ Backspace --]]
  245.                 if nPos > 0 then
  246.                     sLine = string.sub( sLine, 1, nPos - 1 ) .. string.sub( sLine, nPos + 1 )
  247.                     nPos = nPos - 1    
  248.                 end
  249.             end
  250.         end
  251.     end
  252.     term.setCursorBlink( false )
  253.     term.setCursorPos( w + 1, sy )
  254.     print()
  255.     return sLine
  256. end
  257.  
  258. local function pastelogin()
  259.     if not http then print("Http Api is offline - this will not work. Sorry") return end
  260.     pcall( function()
  261.         if not pastebin.api_user_key then
  262.             write("Username: ")
  263.             local a=read()
  264.             write("Password: ")
  265.             --[[ Passowrd is not stored - is used and forgotten --]]
  266.             local response=pastebin.login(a,saferead())
  267.             if string.find(string.lower(response), "bad api request" , 1 , true) then print("Login Unsuccessful") pastebin.api_user_key = false
  268.             else print("Login Successful") pastebin.api_user_key = response end
  269.         else
  270.             print("Already Logged in as "..pastebin.tabulate(pastebin.user())[1]["name"]..". Use plogout< to logout if you want to change users.")
  271.         end
  272.     end)
  273. end
  274.  
  275. local function pastelogout()
  276.     pastebin.api_user_key = false print("Logout Successful")
  277. end
  278.  
  279. local function pastewho()
  280.     if not http then print("Http Api is offline - this will not work. Sorry") return end
  281.     if not pastebin.api_user_key then print(" No user logged in - Please use plogin< to login") return end
  282.     pcall( function()
  283.         print("User logged in - displaying data:")
  284.         local user=pastebin.tabulate(pastebin.user())[1]
  285.         for i,k in pairs(user) do
  286.         print(i," : ",k)
  287.         end
  288.     end)
  289. end
  290.  
  291. local function pasteget(_Where)
  292.     if not http then print("Http Api is offline - this will not work. Sorry") return end
  293.     if not pastebin.api_user_key then print(" No user logged in - Please use plogin< to login") return end
  294.    
  295.     pcall( function()
  296.         local dir=fs.combine(saveDir,"Pastes")
  297.         if not _Where and not fs.exists(dir) then fs.makeDir(dir) end
  298.        
  299.         print("Downloading Filelist")
  300.         local pastes=pastebin.tabulate(pastebin.listall())
  301.        
  302.         for i=#pastes,1,-1 do --[[Removing private Pastes - no way to download them--]]
  303.             if pastes[i].private==2 then table.remove(pastes,i) end
  304.         end
  305.        
  306.         local offset=0
  307.         local selected=1
  308.         local All=false
  309.        
  310.         local function List()
  311.             local x,y=term.getSize()
  312.             term.clear()
  313.             if #pastes > y-1 then offset=math.max(0,math.min(#pastes-(y-1),selected-math.floor((y-1)/2))) else offset=0 end
  314.             for i=1,math.min(y-1,#pastes) do
  315.                 term.setCursorPos(1,i)
  316.                 if i+offset==selected then
  317.                 term.setTextColour(colours.black)
  318.                 term.setBackgroundColor(colours.white)
  319.                 else
  320.                 term.setTextColour(colours.white)
  321.                 term.setBackgroundColor(colours.black)
  322.                 end
  323.                 local temp=pastes[i+offset]
  324.                 term.write((temp.private=="1" and "." or "")..(temp.selected and "> " or "")..temp["title"]..(temp.selected and " <" or ""))
  325.             end
  326.             term.setTextColour(colours.white)
  327.             term.setBackgroundColor(colours.black)
  328.             term.setCursorPos(1,y)
  329.             if _Where then
  330.             term.write("Up/Down,D-Download")
  331.             else
  332.             term.write("Up/Down,Enter-Sel,D-Download,Tab-D-All")
  333.             end
  334.         end
  335.  
  336.         List()
  337.        
  338.         while true do
  339.             event= {os.pullEvent()}
  340.             if event[1]=="key" then
  341.                 if event[2]==keys.down then
  342.                     selected=math.min(selected+1,#pastes)
  343.                     List()
  344.                 elseif event[2]==keys.up then
  345.                     selected=math.max(selected-1,1)
  346.                     List()
  347.                 elseif event[2]==keys.d then
  348.                     if _Where then pastes[selected]["selected"]=true end
  349.                     print()
  350.                     print("Downloading Selected")  
  351.                     break
  352.                 elseif not _Where and (event[2]==keys.numPadEnter or event[2]==keys.enter)then
  353.                     pastes[selected]["selected"]=not pastes[selected]["selected"]
  354.                     List()
  355.                 elseif not _Where and  event[2]==keys.tab then
  356.                     print()
  357.                     print("Downloading All")
  358.                     All=true
  359.                     break
  360.                 end
  361.             end --if event
  362.         end
  363.        
  364.         term.setTextColour(colours.white)
  365.         term.setBackgroundColor(colours.black)
  366.        
  367.         for _,k in pairs(pastes) do
  368.             if k.selected or All then
  369.                 local name=string.gsub(k["title"]," ","_")
  370.                 print("Downloading "..name)
  371.                 local file =nil
  372.                 while true do
  373.                     file = pastebin.getone(k["key"])
  374.                     if file then break end
  375.                     print("Failed - Retrying in 5 sec")
  376.                     sleep(5)
  377.                     print("Downloading "..name)
  378.                 end
  379.                 if _Where then save(_Where,file)
  380.                 else save(fs.combine(dir,name),file) end
  381.                 print("Finished")
  382.             end
  383.         end
  384.        
  385.         print("Downloaded to ",_Where or dir )
  386.     end)
  387. end
  388.  
  389. local function pasteput()
  390.     if not http then print("Http Api is offline - this will not work. Sorry") return end
  391.     --[[To be added at later date--]]
  392. end
  393.  
  394. -- lua code
  395.  
  396. local LuatEnv = {}
  397. setmetatable( LuatEnv, { __index = getfenv() } )
  398.  
  399. local function runlua( _sCommand )
  400.     if not _sCommand then return false end
  401.  
  402.  
  403.     local nForcePrint = 0
  404.     local func, e = loadstring( _sCommand, "lua" )
  405.     local func2, e2 = loadstring( "return ".._sCommand, "lua" )
  406.     if not func then
  407.         if func2 then
  408.             func = func2
  409.             e = nil
  410.             nForcePrint = 1
  411.         end
  412.     else
  413.         if func2 then
  414.             func = func2
  415.         end
  416.     end
  417.    
  418.     if func then
  419.         setfenv( func, LuatEnv )
  420.         local tResults = { pcall( function() return func() end ) }
  421.         if tResults[1] then
  422.             local n = 1
  423.             while (tResults[n + 1] ~= nil) or (n <= nForcePrint) do
  424.                 print( tostring( tResults[n + 1] ) )
  425.                 n = n + 1
  426.             end
  427.         else
  428.             printError( tResults[2] )
  429.         end
  430.     else
  431.         printError( e )
  432.     end
  433.    
  434. end
  435.  
  436. -- cat code
  437.  
  438. local function cat(_Tfiles)
  439.     _,y=term.getSize()
  440.     local out={}
  441.    
  442.     pcall( function()
  443.         for i,k in ipairs(_Tfiles) do
  444.             local path=shell.resolve(k)
  445.             if not fs.exists(path) then print("File "..k.." don't exists") return end
  446.             if fs.isDir(path) then print(k.." is a directory") return end
  447.             local file = fs.open(path, "r")
  448.             table.insert(out,file.readAll())
  449.             file.close()
  450.         end
  451.         textutils.pagedPrint(table.concat(out))
  452.     end)
  453. end
  454.  
  455. -- find code
  456.  
  457. local function findfile(target,patt)
  458.     if target=="" then print("No target specified") return end
  459.     local matches={}
  460.     target=string.lower(target)
  461.    
  462.     local function process(path)
  463.         local T=fs.list(path)
  464.         for _,k in ipairs(T) do
  465.             local cpath=path.."/"..k
  466.             if string.find(string.lower(k), target , 1 , not patt) then table.insert(matches,cpath) end
  467.             if fs.isDir(cpath) then process(cpath) end
  468.         end
  469.     end
  470.    
  471.     local function list(_Tlist)
  472.         local tFiles = {}
  473.         local tVFiles = {}
  474.         local tDirs = {}
  475.         local tVDirs = {}
  476.         for n, sItem in pairs(_Tlist) do
  477.             if fs.isDir( sItem ) then
  478.                 table.insert( tDirs, sItem )
  479.             else
  480.                 table.insert( tFiles, sItem )
  481.             end
  482.         end
  483.         table.sort( tDirs ) table.sort( tVDirs ) table.sort( tFiles ) table.sort( tVFiles )
  484.         if term.isColour() then
  485.             textutils.pagedTabulate(colors.green , tDirs , colors.white, tFiles)
  486.         else
  487.             textutils.pagedTabulate(tDirs , tFiles)
  488.         end
  489.     end
  490.    
  491.     pcall( function()
  492.         process("")
  493.         list(matches)
  494.     end)
  495. end
  496.  
  497. -- read with tab-completition code
  498.  
  499. local function readtab( _sReplaceChar, _tHistory , _sPrefix)
  500.     term.setCursorBlink( true )
  501.  
  502.     local sLine = _sPrefix or ""
  503.     local nHistoryPos = nil
  504.     local nPos = string.len(sLine)
  505.     if _sReplaceChar then
  506.         _sReplaceChar = string.sub( _sReplaceChar, 1, 1 )
  507.     end
  508.    
  509.     local w, h = term.getSize()
  510.     local sx, sy = term.getCursorPos() 
  511.     local clocvis
  512.    
  513.     local function shellclock(_clear)
  514.         if  bClock and not _clear and sx+#sLine <= w-7 then
  515.             clocvis=true
  516.             term.setCursorBlink( false )
  517.             local state=textutils.formatTime(os.time(),true)
  518.             while #state<5 do state=" "..state end
  519.             local x=term.getCursorPos()
  520.             term.setCursorPos( w-5, sy )
  521.             term.setTextColour( promptColour )
  522.             write(state)
  523.             term.setTextColour( textColour )
  524.             term.setCursorPos( x, sy )
  525.             term.setCursorBlink( true )
  526.         else
  527.             if clocvis then
  528.                 local x=term.getCursorPos()
  529.                 term.setCursorBlink( false )
  530.                 term.setCursorPos( w-5, sy )
  531.                 write("     ")
  532.                 term.setCursorPos( x, sy )
  533.                 term.setCursorBlink( true )
  534.                 clocvis=false          
  535.             end
  536.         end    
  537.     end
  538.    
  539.     local function redraw( _sCustomReplaceChar )
  540.         local nScroll = 0
  541.         if sx + nPos >= w then
  542.             nScroll = (sx + nPos) - w
  543.         end
  544.         shellclock()
  545.         term.setCursorPos( sx, sy )
  546.         local sReplace = _sCustomReplaceChar or _sReplaceChar
  547.         if sReplace then
  548.             term.write( string.rep( sReplace, string.len(sLine) - nScroll ) )
  549.         else
  550.             term.write( string.sub( sLine, nScroll + 1 ) )
  551.         end
  552.         term.setCursorPos( sx + nPos - nScroll, sy )
  553.     end
  554.  
  555.     local function list(_Tlist,_Spath,_Bhide)
  556.         shellclock(true)
  557.         local tFiles = {}
  558.         local tVFiles = {}
  559.         local tDirs = {}
  560.         local tVDirs = {}
  561.         for n, sItem in pairs(_Tlist) do
  562.             if not (_Bhide and string.sub( sItem, 1, 1 )==".") then
  563.                 local sPath = fs.combine( _Spath, sItem )
  564.                 if fs.isDir( sPath ) then
  565.                     table.insert( tDirs, sItem )
  566.                 else
  567.                     table.insert( tFiles, sItem )
  568.                 end
  569.             end
  570.         end
  571.         table.sort( tDirs ) table.sort( tVDirs ) table.sort( tFiles ) table.sort( tVFiles )
  572.         print()
  573.         if term.isColour() then
  574.             pcall( function()
  575.             textutils.pagedTabulate( colors.green , tDirs , colors.white, tFiles)
  576.             end)
  577.         else
  578.             pcall( function()
  579.             textutils.pagedTabulate( tDirs , tFiles)
  580.             end)
  581.         end
  582.         term.setTextColour( promptColour )
  583.         write( shell.dir() .. "> " )
  584.         term.setTextColour( textColour )
  585.         sx, sy = term.getCursorPos()
  586.     end
  587.    
  588.     local function common(_String,_Min,_Cur)
  589.         if not _Cur then return _String end
  590.         local long=_Cur
  591.         for i=math.min(#_String,#_Cur),_Min,-1 do
  592.             local a,b=string.sub (_String,1,i),string.sub(_Cur,1,i)
  593.             if a==b then return a end      
  594.         end
  595.         return string.sub (_Cur,1,_Min)
  596.     end
  597.    
  598.     local tick
  599.     if bClock then tick=os.startTimer(1) end
  600.     redraw()
  601.     while true do
  602.         local sEvent, param = os.pullEvent()
  603.         if sEvent =="timer" and param==tick then shellclock() tick=os.startTimer(1)
  604.         elseif sEvent == "char" then
  605.             sLine = string.sub( sLine, 1, nPos ) .. param .. string.sub( sLine, nPos + 1 )
  606.             nPos = nPos + 1
  607.             redraw()
  608.            
  609.         elseif sEvent == "key" then
  610.             if param == keys.enter then
  611.                 --[[ Enter --]]
  612.                 shellclock(true)
  613.                 break
  614.                
  615.             elseif param == keys.left then
  616.                 --[[ Left --]]
  617.                 if nPos > 0 then
  618.                     redraw(" ")
  619.                     nPos = nPos - 1
  620.                     redraw()
  621.                 end
  622.                
  623.             elseif param == keys.right then
  624.                 --[[ Right --]]            
  625.                 if nPos < string.len(sLine) then
  626.                     redraw(" ")
  627.                     nPos = nPos + 1
  628.                     redraw()
  629.                 end
  630.            
  631.             elseif param == keys.up or param == keys.down then
  632.                 --[[ Up or down --]]
  633.                 if _tHistory then
  634.                     redraw(" ")
  635.                     if param == keys.up then
  636.                         --[[ Up --]]
  637.                         if nHistoryPos == nil then
  638.                             if #_tHistory > 0 then
  639.                                 nHistoryPos = #_tHistory
  640.                             end
  641.                         elseif nHistoryPos > 1 then
  642.                             nHistoryPos = nHistoryPos - 1
  643.                         end
  644.                     else
  645.                         --[[ Down --]]
  646.                         if nHistoryPos == #_tHistory then
  647.                             nHistoryPos = nil
  648.                         elseif nHistoryPos ~= nil then
  649.                             nHistoryPos = nHistoryPos + 1
  650.                         end                    
  651.                     end
  652.                     if nHistoryPos then
  653.                         sLine = _tHistory[nHistoryPos]
  654.                         nPos = string.len( sLine )
  655.                     else
  656.                         sLine = ""
  657.                         nPos = 0
  658.                     end
  659.                     redraw()
  660.                 end
  661.             elseif param == keys.backspace then
  662.                 --[[ Backspace --]]
  663.                 if nPos > 0 then
  664.                     redraw(" ")
  665.                     sLine = string.sub( sLine, 1, nPos - 1 ) .. string.sub( sLine, nPos + 1 )
  666.                     nPos = nPos - 1                
  667.                     redraw()
  668.                 end
  669.             elseif param == keys.home then
  670.                 --[[ Home --]]
  671.                 redraw(" ")
  672.                 nPos = 0
  673.                 redraw()       
  674.             elseif param == keys.delete then
  675.                 if nPos < string.len(sLine) then
  676.                     redraw(" ")
  677.                     sLine = string.sub( sLine, 1, nPos ) .. string.sub( sLine, nPos + 2 )              
  678.                     redraw()
  679.                 end
  680.             elseif param == keys["end"] then
  681.                 --[[ End --]]
  682.                 redraw(" ")
  683.                 nPos = string.len(sLine)
  684.                 redraw()
  685.             elseif param == keys.tab and (string.sub( sLine, nPos+1 ,nPos+1)==" " or nPos==string.len(sLine)) then
  686.                 --[[ Tab  --]]
  687.                 local need=string.match(string.sub(sLine,1,nPos),"[ ]?([^ ]-)$")
  688.                 if need=="" or string.match(need,"[\\/]$") then
  689.  
  690.                     --[[looking at root or /,\ at end mean folder --]]
  691.                     local path=shell.resolve(need or "")
  692.                     if fs.exists(path) and fs.isDir(path) then list(fs.list(path),path,true) end
  693.                    
  694.                 else
  695.                     --[[have part of name --]]                 
  696.                     --[[get absolute path and target name --]]
  697.                     local path=""
  698.                     local targ=""
  699.                     if need=="." or string.match(need,"[\\/]%.$") then --[[lokking for hidden in folder --]]
  700.                         path=shell.resolve(need).."/"
  701.                         targ="."
  702.                     else --[[lokking for normal file --]]
  703.                         path=shell.resolve(need)
  704.                         targ=fs.getName(path)
  705.                     end
  706.                    
  707.                     --[[get path without name and see it iteven exists --]]
  708.                     path=string.sub(path,1,#path-#targ) or ""
  709.                     if fs.exists(path) and fs.isDir(path) then
  710.                        
  711.                         --[[find files in path matching part --]]
  712.                         --[[at same time look for longest common leading substing --]]
  713.                         local out={}
  714.                         local long=nil
  715.                         local lim=#targ
  716.                          --[[Sanitizing magic for pattern needs ^$()%.[]*+-?--]]
  717.                         local pattern="^"..string.gsub(string.lower(targ),"[%^%$%(%)%%%.%[%]%*%+%-%?]",function(A) return "%"..A end)
  718.                         for i,k in pairs(fs.list(path)) do
  719.                             local tempS=string.lower(k)
  720.                             if string.match(tempS,pattern) then
  721.                                 table.insert(out,k)
  722.                                 long=common(tempS,lim,long)
  723.                             end
  724.                         end
  725.                         if #out==1 then
  726.                             if fs.isDir(fs.combine(path,out[1])) then out[1]=out[1].."/" end
  727.                             sLine=string.sub(sLine,1,nPos-#targ)..out[1]..string.sub( sLine, nPos + 1 )
  728.                             nPos = nPos-#targ+#out[1]
  729.                         elseif #out>1 then
  730.                             sLine=string.sub( sLine, 1, nPos-#targ )..long..string.sub( sLine, nPos + 1 )
  731.                             nPos = nPos-#targ+#long
  732.                             list(out,path)                         
  733.                         end
  734.                     end
  735.                 end
  736.                 redraw()
  737.             end
  738.         end
  739.     end
  740.    
  741.     term.setCursorBlink( false )
  742.     term.setCursorPos( w + 1, sy )
  743.     print()
  744.    
  745.     return sLine
  746. end
  747.  
  748. --[[ Back to standard shell with extended modyfications  --]]
  749.    
  750. term.setBackgroundColor( bgColour )
  751. term.setTextColour( promptColour )
  752. print( os.version(), " Extended v 2.1" )
  753. term.setTextColour( textColour )
  754.  
  755. --[[ If this is the toplevel shell, run the startup programs  --]]
  756. if parentShell == nil then
  757.     --[[ Run the startup from the ROM first  --]]
  758.     local sRomStartup = shell.resolveProgram( "/rom/startup" )
  759.     if sRomStartup then
  760.         shell.run( sRomStartup )
  761.     end
  762.    
  763.     --[[ Then run the user created startup, from the disks or the root  --]]
  764.     local sUserStartup = shell.resolveProgram( "/startup" )
  765.     for n,sSide in pairs( peripheral.getNames() ) do
  766.         if disk.isPresent( sSide ) and disk.hasData( sSide ) then
  767.             local sDiskStartup = shell.resolveProgram( fs.combine(disk.getMountPath( sSide ), "startup") )
  768.             if sDiskStartup then
  769.                 sUserStartup = sDiskStartup
  770.                 break
  771.             end
  772.         end
  773.     end
  774.    
  775.     if sUserStartup then
  776.         shell.run( sUserStartup )
  777.     end
  778. end
  779.  
  780. --[[ Run any programs passed in as arguments  --]]
  781. local tArgs = { ... }
  782. if #tArgs > 0 then
  783.     shell.run( ... )
  784. end
  785.  
  786. --[[ Read commands and execute them -- Most of modyfications happend here  --]]
  787. local tCommandHistoryfile=fs.combine(saveDir,".shellhistory.log")
  788. local tCommandHistory = {}
  789. if fs.exists(tCommandHistoryfile) then tCommandHistory = getT(tCommandHistoryfile) or {} end
  790. local nextprefix = ""
  791. while not bExit do
  792.     local a=term.getCursorPos() --making sure it will not go out of screen
  793.     if a>1 then print() end
  794.    
  795.     term.setBackgroundColor( bgColour )
  796.     term.setTextColour( promptColour )
  797.     write( shell.dir() .. "> " )
  798.     term.setTextColour( textColour )
  799.    
  800.     local sLine = readtab( nil, tCommandHistory ,nextprefix)
  801.     nextprefix = ""
  802.     if sLine~="" then
  803.    
  804.         if sLine ~= tCommandHistory[#tCommandHistory] then
  805.             table.insert( tCommandHistory, sLine )
  806.             if tCommandHistory[101] then table.remove(tCommandHistory,1) end
  807.             saveT(tCommandHistoryfile,tCommandHistory)
  808.         end
  809.        
  810.         local tWords = {}
  811.             for match in string.gmatch( sLine, "[^ \t]+" ) do
  812.             table.insert( tWords, match )
  813.         end
  814.         local sCommand = table.remove(tWords,1)
  815.        
  816.         if sCommand == "lua<" then --[[ Lua Execution --]]
  817.             runlua( string.sub( sLine, 6) )
  818.             nextprefix = "lua< "
  819.         elseif sCommand == "lua#" then --[[ Lua Clear --]]
  820.             LuatEnv = {} setmetatable( LuatEnv, { __index = getfenv() } ) print("Shell Lua Enviroment Cleared")
  821.         elseif sCommand == "cat<" then  --[[ Cat Execution --]]
  822.             cat(tWords)
  823.         elseif sCommand == "clock<" then    --[[ Clock Toggle --]]
  824.             bClock=not bClock print("Clock ",(bClock and "On")or"Off")
  825.         elseif sCommand == "find<" then --[[ Find Execution --]]
  826.             findfile(string.sub( sLine, 7))
  827.         elseif sCommand == "findpattern<" then  --[[ Find with pattern Execution --]]
  828.             findfile(string.sub( sLine, 14),true)
  829.         elseif sCommand == "plogin<" then   --[[ pastebin login --]]
  830.             pastelogin()
  831.         elseif sCommand == "plogout<" then  --[[ pastebin plogout --]]
  832.             pastelogout()
  833.         elseif sCommand == "pget<" then --[[  pastebin get one file --]]
  834.             local tar=string.sub( sLine, 7)
  835.             pasteget(shell.resolve(tar))
  836.         elseif sCommand == "plist<" then    --[[  pastebin get x files--]]
  837.             pasteget()
  838.         elseif sCommand == "pwho<" then --[[ pastebin get user info --]]
  839.             pastewho()
  840.         elseif sCommand == "help<" or sCommand == "?<"  then    --[[ Help Execution --]]
  841.             print("List of extra shell commands: lua<,lua#,cat<,clock<,find<,findpattern<,help<",(http and ",plogin<,plogout<,pget<,plist<" or ""))
  842.         elseif sCommand then --[[ Standard Execution --]]
  843.             run( sCommand, unpack( tWords ) )
  844.         end
  845.     end
  846. end
  847.  
  848. --[[ If this is the toplevel shell, run the shutdown program  --]]
  849. if parentShell == nil then
  850.     if shell.resolveProgram( "shutdown" ) then
  851.         shell.run( "shutdown" )
  852.     end
  853.     os.shutdown() --[[ just in case  --]]
  854. end
  855.  
  856. term.setTextColour( promptColour )
  857. print("Extended Shell Terminated.")
  858. print("Have a Nice Day!")
  859. term.setTextColour( textColour )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement