Advertisement
Wojbie

Shell Utility Extended v3.0

Sep 17th, 2017 (edited)
395
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 22.48 KB | None | 0 0
  1. --# Shell Utility Extended v3.0 - Program to extend/modify Computercraft autocompletion system.
  2. --# Made By Wojbie
  3. --# http://pastebin.com/MbcnJRAU
  4.  
  5. --   Copyright (c) 2017-2021 Wojbie (wojbie@wojbie.net)
  6. --   Redistribution and use in source and binary forms, with or without modification, are permitted (subject to the limitations in the disclaimer below) provided that the following conditions are met:
  7. --   1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
  8. --   2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
  9. --   3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
  10. --   4. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
  11. --   5. The origin of this software must not be misrepresented; you must not claim that you wrote the original software.
  12. --   NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. YOU ACKNOWLEDGE THAT THIS SOFTWARE IS NOT DESIGNED, LICENSED OR INTENDED FOR USE IN THE DESIGN, CONSTRUCTION, OPERATION OR MAINTENANCE OF ANY NUCLEAR FACILITY.
  13.  
  14. --# New settings system usage functions. Allows auto correction of common errors.
  15. local defaultSettings = {
  16. ["shellUtil.use_ghost"] = true,
  17. ["shellUtil.chat_names"] = "",
  18. ["shellUtil.pastebin_names"] = "",
  19. }
  20.  
  21. --Restore cleared settings
  22. for i,k in pairs(defaultSettings) do
  23.     if settings.get(i) == nil then
  24.         settings.set(i,k)
  25.     end
  26. end
  27.  
  28. local validSettingsTypes = {
  29. ["shellUtil.use_ghost"] = {["boolean"]=true},
  30. ["shellUtil.chat_names"] = {["string"]=true},
  31. ["shellUtil.pastebin_names"] = {["string"]=true},
  32. }
  33.  
  34. local validSettingsTest = {
  35. }
  36.  
  37. local getSetting = function(A)
  38.     local data = settings.get(A)
  39.     if  (not validSettingsTypes[A] or validSettingsTypes[A][type(data)]) and --See if setting type matches (when defined).
  40.         (not validSettingsTest[A] or validSettingsTest[A](data)) --See if testing function agrees (when defined).
  41.     then --All tests OK
  42.         return data
  43.     else --Any of test Failed. Reset to default setting.
  44.         data = defaultSettings[A]
  45.         settings.set(A,data)
  46.         return data
  47.     end
  48. end
  49.  
  50. --# Basic file operators.
  51. local function append(A,B) local file = fs.open(tostring(A),"a") if not file then return false end file.write(B) file.close() return true end
  52.  
  53. local function save(A,B) local file = fs.open(tostring(A),"w") if not file then return false end file.write(B) file.close() return true end
  54. local function saveT(A,B) return save(A,textutils.serialize(B)) end
  55. local function saveTL(A,B) return save(A,string.gsub(textutils.serialize(B),"\n%s*","")) end
  56. local function saveLines(A,B) local file = fs.open(tostring(A),"w") if not file then return false end for i=1,#B,1 do file.writeLine(B[i]) end file.close() return true end
  57. local function saveBin(A,B) local file = fs.open(tostring(A),"wb") if not file then return false end for i=1,#B,1 do file.write(B[i]) end file.close() return true end
  58. local function saveDump(A,B) return saveBin(A,{string.byte(B,1,#B)}) end
  59.  
  60. 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
  61. local function getT(A) local data = get(A) if data then data = textutils.unserialize(data) end if data then return data end end
  62. local function getLines(A) local file = fs.open(tostring(A),"r") if not file then return false end local data = {} for k in file.readLine do data[#data+1] = k end file.close() return data end
  63. local function getBin(A) local file = fs.open(tostring(A),"rb") if not file then return false end local data = {} local b = file.read() while b do table.insert(data,b) b = file.read() end file.close() if data then return data end end
  64. local function getDump(A) local file = getBin(A) if not file then return false end local data = string.char(table.unpack(file)) if data then return data end end
  65. local function getHttp(A,B,C) if not http.checkURL(A) then return false end local file = B and http.post(A,B,C) or http.get(A,C) if not file then return false end local data = file.readAll() file.close() if data then return data end end
  66.  
  67. local function makeLog(A) local file = fs.open(tostring(A),"a") if not file then return false end local on = true return function(m) if not on then return false end file.writeLine(m) file.flush() return true end,function() on = false file.close() end end
  68. local function makePrintLog(A) local logfile,logstop = makeLog(A) if not logfile then return false end return function(m) print(m) logfile(m) end,logstop,logfile end
  69.  
  70. --# Useafull functions
  71.  
  72. local function completeMultipleChoice( sText, tOptions, bAddSpaces, tOptionsGhosts ) --Copy of function cause its usefull
  73.     local tResults = {}
  74.     local tGhosts = {}
  75.     for n=1,#tOptions do
  76.         local sOption = tOptions[n]
  77.         if #sOption + (bAddSpaces and 1 or 0) > #sText and string.sub( sOption, 1, #sText ) == sText then
  78.             local sResult = string.sub( sOption, #sText + 1 )
  79.             if bAddSpaces then
  80.                 table.insert( tResults, sResult .. " " )
  81.             else
  82.                 table.insert( tResults, sResult )
  83.             end
  84.             if tOptionsGhosts then
  85.                 if bAddSpaces then
  86.                     table.insert( tGhosts, tOptionsGhosts[n] or "")
  87.                 else
  88.                     table.insert( tGhosts, (tOptionsGhosts[n] and " "..tOptionsGhosts[n]) or "")
  89.                 end
  90.                
  91.             end
  92.         end
  93.     end
  94.     return tResults,tGhosts
  95. end
  96.  
  97. local function peripherallook(sType,fTest) --Fast way to make table of peripheral names.
  98.     local tNames={}
  99.     peripheral.find(sType,function(sName,tObject) if ( not fTest ) or fTest(sName,tObject) then table.insert(tNames,sName) end return false end)
  100.     return tNames
  101. end
  102.  
  103.  
  104. local function hostnameslook(sProtocol,nTime) --Program to lookup hostnames that are in set sProtocol. nTime is time it will look. Defaults to 0,5.
  105.     -- Build list of host IDs
  106.     local tResults = {}
  107.     local close=false
  108.    
  109.     if not rednet.isOpen() then
  110.         for i,k in pairs(rs.getSides()) do
  111.             if peripheral.getType( k ) == "modem" then
  112.                 rednet.open(k)
  113.                 close=k
  114.                 break
  115.             end
  116.         end
  117.         if not close then return tResults end
  118.     end
  119.  
  120.     -- Broadcast a lookup packet
  121.     rednet.broadcast( {
  122.         sType = "lookup",
  123.         sProtocol = sProtocol,
  124.         sHostname = sHostname,
  125.     }, "dns" )
  126.  
  127.     -- Start a timer
  128.     local timer = os.startTimer( nTime or 0.5 )
  129.  
  130.     -- Wait for events
  131.     while true do
  132.         local event, p1, p2, p3 = os.pullEvent()
  133.         if event == "rednet_message" then
  134.             -- Got a rednet message, check if it's the response to our request
  135.             local nSenderID, tMessage, sMessageProtocol = p1, p2, p3
  136.             if sMessageProtocol == "dns" and tMessage.sType == "lookup response" then
  137.                 if tMessage.sProtocol == sProtocol then
  138.                         table.insert( tResults, tMessage.sHostname )
  139.                 end
  140.             end
  141.         else
  142.             -- Got a timer event, check it's the end of our timeout
  143.             if p1 == timer then
  144.                 break
  145.             end
  146.         end
  147.     end
  148.  
  149.     if close then
  150.         rednet.close(close)
  151.     end
  152.    
  153.     return tResults
  154. end
  155.  
  156. --## Main Program Parts ##--
  157.  
  158. --# Read overwrite to add ghosting and change to few vanilla auto-completitions to add ghost capabilities.
  159.  
  160. if getSetting("shellUtil.use_ghost") then --This setting is only tested once at moment program is run.
  161.  
  162.     -- Overwriting read with one that supports 2nd Ghost table.
  163.     function _G.read( _sReplaceChar, _tHistory, _fnComplete, _sDefault )
  164.         if _sReplaceChar ~= nil and type( _sReplaceChar ) ~= "string" then
  165.             error( "bad argument #1 (expected string, got " .. type( _sReplaceChar ) .. ")", 2 )
  166.         end
  167.         if _tHistory ~= nil and type( _tHistory ) ~= "table" then
  168.             error( "bad argument #2 (expected table, got " .. type( _tHistory ) .. ")", 2 )
  169.         end
  170.         if _fnComplete ~= nil and type( _fnComplete ) ~= "function" then
  171.             error( "bad argument #3 (expected function, got " .. type( _fnComplete ) .. ")", 2 )
  172.         end
  173.         if _sDefault ~= nil and type( _sDefault ) ~= "string" then
  174.             error( "bad argument #4 (expected string, got " .. type( _sDefault ) .. ")", 2 )
  175.         end
  176.         term.setCursorBlink( true )
  177.  
  178.         local sLine = ""
  179.         if type( _sDefault ) == "string" then
  180.             sLine = _sDefault
  181.         else
  182.             sLine = ""
  183.         end
  184.         local nHistoryPos
  185.         local nPos = #sLine
  186.         if _sReplaceChar then
  187.             _sReplaceChar = string.sub( _sReplaceChar, 1, 1 )
  188.         end
  189.  
  190.         local tCompletions
  191.         local nCompletion
  192.         local tGhosts --#
  193.         local function recomplete()
  194.             if _fnComplete and nPos == string.len(sLine) then
  195.                 tCompletions,tGhosts = _fnComplete( sLine ) --#
  196.                 if tCompletions and #tCompletions > 0 then
  197.                     nCompletion = 1
  198.                     tGhosts = tGhosts or {} --#
  199.                 else
  200.                     nCompletion = nil
  201.                 end
  202.             else
  203.                 tCompletions = nil
  204.                 nCompletion = nil
  205.             end
  206.         end
  207.  
  208.         local function uncomplete()
  209.             tCompletions = nil
  210.             nCompletion = nil
  211.             tGhosts = nil --#
  212.         end
  213.  
  214.         local w = term.getSize()
  215.         local sx = term.getCursorPos()
  216.  
  217.         local function redraw( _bClear )
  218.             local nScroll = 0
  219.             if sx + nPos >= w then
  220.                 nScroll = (sx + nPos) - w
  221.             end
  222.  
  223.             local cx,cy = term.getCursorPos()
  224.             term.setCursorPos( sx, cy )
  225.             local sReplace = (_bClear and " ") or _sReplaceChar
  226.             if sReplace then
  227.                 term.write( string.rep( sReplace, math.max( string.len(sLine) - nScroll, 0 ) ) )
  228.             else
  229.                 term.write( string.sub( sLine, nScroll + 1 ) )
  230.             end
  231.  
  232.             if nCompletion then
  233.                 local sCompletion = tCompletions[ nCompletion ]
  234.                 local sGhost = tGhosts[ nCompletion ] --#
  235.                 local oldText, oldBg
  236.                 if not _bClear then
  237.                     oldText = term.getTextColor()
  238.                     oldBg = term.getBackgroundColor()
  239.                     term.setTextColor( colors.white )
  240.                     term.setBackgroundColor( colors.gray )
  241.                 end
  242.                 if sReplace then
  243.                     term.write( string.rep( sReplace, string.len( sCompletion ) ) )
  244.                 else
  245.                     term.write( sCompletion )
  246.                 end
  247.                 --#
  248.                 if sGhost then
  249.                     if not _bClear then
  250.                         term.setTextColor( colors.lightGray )
  251.                         --term.setBackgroundColor( colors.gray )
  252.                     end
  253.                     if sReplace then
  254.                         term.write( string.rep( sReplace, string.len( sGhost ) ) )
  255.                     else
  256.                         term.write( sGhost )
  257.                     end
  258.                 end
  259.                 --#
  260.                 if not _bClear then
  261.                     term.setTextColor( oldText )
  262.                     term.setBackgroundColor( oldBg )
  263.                 end
  264.             end
  265.  
  266.             term.setCursorPos( sx + nPos - nScroll, cy )
  267.         end
  268.        
  269.         local function clear()
  270.             redraw( true )
  271.         end
  272.  
  273.         recomplete()
  274.         redraw()
  275.  
  276.         local function acceptCompletion()
  277.             if nCompletion then
  278.                 -- Clear
  279.                 clear()
  280.  
  281.                 -- Find the common prefix of all the other suggestions which start with the same letter as the current one
  282.                 local sCompletion = tCompletions[ nCompletion ]
  283.                 sLine = sLine .. sCompletion
  284.                 nPos = string.len( sLine )
  285.  
  286.                 -- Redraw
  287.                 recomplete()
  288.                 redraw()
  289.             end
  290.         end
  291.         while true do
  292.             local sEvent, param = os.pullEvent()
  293.             if sEvent == "char" then
  294.                 -- Typed key
  295.                 clear()
  296.                 sLine = string.sub( sLine, 1, nPos ) .. param .. string.sub( sLine, nPos + 1 )
  297.                 nPos = nPos + 1
  298.                 recomplete()
  299.                 redraw()
  300.  
  301.             elseif sEvent == "paste" then
  302.                 -- Pasted text
  303.                 clear()
  304.                 sLine = string.sub( sLine, 1, nPos ) .. param .. string.sub( sLine, nPos + 1 )
  305.                 nPos = nPos + string.len( param )
  306.                 recomplete()
  307.                 redraw()
  308.  
  309.             elseif sEvent == "key" then
  310.                 if param == keys.enter then
  311.                     -- Enter
  312.                     if nCompletion then
  313.                         clear()
  314.                         uncomplete()
  315.                         redraw()
  316.                     end
  317.                     break
  318.                    
  319.                 elseif param == keys.left then
  320.                     -- Left
  321.                     if nPos > 0 then
  322.                         clear()
  323.                         nPos = nPos - 1
  324.                         recomplete()
  325.                         redraw()
  326.                     end
  327.                    
  328.                 elseif param == keys.right then
  329.                     -- Right                
  330.                     if nPos < string.len(sLine) then
  331.                         -- Move right
  332.                         clear()
  333.                         nPos = nPos + 1
  334.                         recomplete()
  335.                         redraw()
  336.                     else
  337.                         -- Accept autocomplete
  338.                         acceptCompletion()
  339.                     end
  340.  
  341.                 elseif param == keys.up or param == keys.down then
  342.                     -- Up or down
  343.                     if nCompletion then
  344.                         -- Cycle completions
  345.                         clear()
  346.                         if param == keys.up then
  347.                             nCompletion = nCompletion - 1
  348.                             if nCompletion < 1 then
  349.                                 nCompletion = #tCompletions
  350.                             end
  351.                         elseif param == keys.down then
  352.                             nCompletion = nCompletion + 1
  353.                             if nCompletion > #tCompletions then
  354.                                 nCompletion = 1
  355.                             end
  356.                         end
  357.                         redraw()
  358.  
  359.                     elseif _tHistory then
  360.                         -- Cycle history
  361.                         clear()
  362.                         if param == keys.up then
  363.                             -- Up
  364.                             if nHistoryPos == nil then
  365.                                 if #_tHistory > 0 then
  366.                                     nHistoryPos = #_tHistory
  367.                                 end
  368.                             elseif nHistoryPos > 1 then
  369.                                 nHistoryPos = nHistoryPos - 1
  370.                             end
  371.                         else
  372.                             -- Down
  373.                             if nHistoryPos == #_tHistory then
  374.                                 nHistoryPos = nil
  375.                             elseif nHistoryPos ~= nil then
  376.                                 nHistoryPos = nHistoryPos + 1
  377.                             end                        
  378.                         end
  379.                         if nHistoryPos then
  380.                             sLine = _tHistory[nHistoryPos]
  381.                             nPos = string.len( sLine )
  382.                         else
  383.                             sLine = ""
  384.                             nPos = 0
  385.                         end
  386.                         uncomplete()
  387.                         redraw()
  388.  
  389.                     end
  390.  
  391.                 elseif param == keys.backspace then
  392.                     -- Backspace
  393.                     if nPos > 0 then
  394.                         clear()
  395.                         sLine = string.sub( sLine, 1, nPos - 1 ) .. string.sub( sLine, nPos + 1 )
  396.                         nPos = nPos - 1
  397.                         recomplete()
  398.                         redraw()
  399.                     end
  400.  
  401.                 elseif param == keys.home then
  402.                     -- Home
  403.                     if nPos > 0 then
  404.                         clear()
  405.                         nPos = 0
  406.                         recomplete()
  407.                         redraw()
  408.                     end
  409.  
  410.                 elseif param == keys.delete then
  411.                     -- Delete
  412.                     if nPos < string.len(sLine) then
  413.                         clear()
  414.                         sLine = string.sub( sLine, 1, nPos ) .. string.sub( sLine, nPos + 2 )                
  415.                         recomplete()
  416.                         redraw()
  417.                     end
  418.  
  419.                 elseif param == keys["end"] then
  420.                     -- End
  421.                     if nPos < string.len(sLine ) then
  422.                         clear()
  423.                         nPos = string.len(sLine)
  424.                         recomplete()
  425.                         redraw()
  426.                     end
  427.  
  428.                 elseif param == keys.tab then
  429.                     -- Tab (accept autocomplete)
  430.                     acceptCompletion()
  431.  
  432.                 end
  433.  
  434.             elseif sEvent == "term_resize" then
  435.                 -- Terminal resized
  436.                 w = term.getSize()
  437.                 redraw()
  438.  
  439.             end
  440.         end
  441.  
  442.         local cx, cy = term.getCursorPos()
  443.         term.setCursorBlink( false )
  444.         term.setCursorPos( w + 1, cy )
  445.         print()
  446.        
  447.         return sLine
  448.     end
  449.    
  450.  
  451. end
  452.  
  453.  
  454. --#Generate Shell Completitions
  455.  
  456. -- /rom/programs/
  457. -- Eject --List only disk drives. Ghost drive content type and label/songname.
  458. local function completeEject( shell, nIndex, sText, tPreviousText )
  459.     if nIndex == 1 then
  460.         local tNames = peripherallook("drive")
  461.         local tGhosts = {}
  462.         for i=1,#tNames do
  463.             local sName = tNames[i]
  464.             if disk.hasData(sName) then
  465.                 tGhosts[i] = " Data "..(disk.getLabel(sName) or "")
  466.             elseif disk.hasAudio(sName) then
  467.                 tGhosts[i] = " Music "..(disk.getAudioTitle(sName) or "")
  468.             elseif disk.isPresent(sName) then
  469.                 tGhosts[i] = " Unknown"
  470.             else
  471.                 tGhosts[i] = " Empty"
  472.             end
  473.         end
  474.         return completeMultipleChoice(sText,tNames,false,tGhosts)
  475.     end
  476. end
  477. -- Gps -- Move order of options so locate is first.
  478. local tGPSOptions = {"locate" , "host", "host "}
  479. local function completeGPS( shell, nIndex, sText, tPreviousText )
  480.     if nIndex == 1 then
  481.         return completeMultipleChoice( sText, tGPSOptions )
  482.     end
  483. end
  484. -- Label -- List only disk drives.
  485. local tLabelOptions = { "get", "get ", "set ", "clear", "clear " }
  486. local function completeLabel( shell, nIndex, sText, tPreviousText )
  487.     if nIndex == 1 then
  488.         return completeMultipleChoice( sText, tLabelOptions )
  489.     elseif nIndex == 2 then
  490.         return completeMultipleChoice(sText,peripherallook("drive"))
  491.     end
  492. end
  493. -- Monitor -- List only monitors. Ghost current size of selected monitor.
  494. local function completeMonitor( shell, nIndex, sText, tPreviousText )
  495.     if nIndex == 1 then
  496.         local tNames = peripherallook("monitor")
  497.         local tGhosts = {}
  498.         for i=1,#tNames do
  499.             local x,y = peripheral.call(tNames[i],"getSize")
  500.             tGhosts[i]= x.."x"..y
  501.         end
  502.         return completeMultipleChoice(sText,peripherallook("monitor"), true ,tGhosts)
  503.     elseif nIndex == 2 then
  504.         return shell.completeProgram( sText )
  505.     end
  506. end
  507. -- Set -- Ghost the current setting (if table say [table])
  508. local function completeSet( shell, nIndex, sText, tPreviousText )
  509.     if nIndex == 1 then
  510.         local tNames = settings.getNames()
  511.         local tGhosts = {}
  512.         for i=1,#tNames do
  513.             local data = settings.get(tNames[i])
  514.             tGhosts[i] = type(data) == "table" and "Table Detected" or tostring(data)
  515.         end
  516.         return completeMultipleChoice( sText, tNames, true , tGhosts)
  517.     end
  518. end
  519.  
  520. --/rom/programs/fun
  521. -- DJ -- List only disk drives with music. Ghosts dong names.
  522. local tDJOptions = { "play", "play ", "stop" }
  523. local function Audiotest(sName,tObject)
  524.     return tObject.hasAudio()
  525. end
  526. local function completeDJ( shell, nIndex, sText, tPreviousText )
  527.     if nIndex == 1 then
  528.         return completeMultipleChoice( sText, tDJOptions )
  529.     elseif nIndex == 2 and tPreviousText[2] == "play" then
  530.         local tNames = peripherallook("drive",Audiotest)
  531.         local tGhosts = {}
  532.         for i=1,#tNames do
  533.             tGhosts[i] = disk.getAudioTitle(tNames[i])
  534.         end
  535.         return completeMultipleChoice(sText,tNames,false,tGhosts)
  536.     end
  537. end
  538.  
  539. --rom/programs/http/
  540. --Pastebin -- List pastes from "shellUtil.pastebin_names" user(s), Ghost paste names. Suggest File Names based of pasteName?
  541.  
  542. --get website https://pastebin.com/u/..name
  543. --<td><img src="/i/t.gif" class="i_p0" title="Public paste, anybody can see this paste." alt="" /> <a href="/DW3LCC3L">Monitor Mirror v2.1</a></td>
  544. --local tPastes={"DW3LCC3L"}
  545. --local tPasteNames={"Monitor Mirror v2.1"}
  546. --local tPasteSuggestNames = {["DW3LCC3L"] = "Monitor_Mirror_v2.1}
  547.  
  548. --Make table of usernames from settings
  549. local tPastebinUserNames = {}
  550. for sName in string.gmatch( getSetting("shellUtil.pastebin_names"), "[^,]+" ) do table.insert(tPastebinUserNames,sName) end
  551.  
  552. --Code to get all the public pastes on Ext-util load.
  553. local tPastes = {}
  554. local tPasteNames = {}
  555. local tPasteSuggestNames = {}
  556. for _,name in pairs(tPastebinUserNames) do
  557.     local site = "https://pastebin.com/u/"..textutils.urlEncode( name )
  558.     if http.checkURL(site) then
  559.         local data = getHttp(site)
  560.         if data then
  561.             --get pastes from data here.
  562.             for i,k in string.gmatch (data, '<td><img src="/i/t.gif" class="i_p0" title="Public paste, anybody can see this paste." alt="" /> <a href="/(%w+)">(.-)</a></td>') do
  563.                 table.insert(tPastes,i)
  564.                 table.insert(tPasteNames,k)
  565.                 tPasteSuggestNames[i] = string.gsub(k,"%s","_")
  566.             end
  567.         end
  568.     end
  569. end
  570. local tPastebinOptions = { "get ", "run ", "put" }
  571. local function completePastebin( shell, nIndex, sText, tPreviousText )
  572.     if nIndex == 1 then
  573.         return completeMultipleChoice( sText, tPastebinOptions )
  574.     elseif nIndex == 2 then
  575.         if tPreviousText[2] == "put" then
  576.             return fs.complete( sText, shell.dir(), true, false )
  577.         elseif tPreviousText[2] == "get" then
  578.             return completeMultipleChoice( sText, tPastes, true,tPasteNames)
  579.         elseif tPreviousText[2] == "run" then
  580.             return completeMultipleChoice( sText, tPastes, true,tPasteNames)
  581.         end
  582.     elseif nIndex == 3 then
  583.         if tPreviousText[2] == "get" and tPasteSuggestNames[tPreviousText[3]]  then
  584.             return completeMultipleChoice( sText, {tPasteSuggestNames[tPreviousText[3]]} )
  585.         end
  586.     end
  587. end
  588.  
  589. --rom/programs/rednet/
  590. -- Chat -- On join allow for duble tap of Tab to scan area for chat servers. Automaticly suggest name(s) from "shellUtil.chat_names"
  591. --##FIND WAY TO EXTRACT ANY SERVER INFO DATA FROM CHAT SERVER WITHOUT LOGGING INTO IT
  592. local tChatOptions = {"join ", "host "}
  593. local tServers = {}
  594. local nLasttab = 0
  595. local function completeChat( shell, nIndex, sText, tPreviousText )
  596.     if nIndex == 1 then
  597.         return completeMultipleChoice( sText, tChatOptions )
  598.     elseif nIndex == 2 and tPreviousText[2] == "join" then
  599.         local tGhosts = {}
  600.         if sText =="" then --Act only is sText field is empty.
  601.             local nTime=os.clock()
  602.             if (nTime-nLasttab) < 0 then --Still Blocked from last scan.
  603.                 --do nothing           
  604.             elseif (nTime-nLasttab) < 0.5 then
  605.                 tServers = hostnameslook("chat") --When 2 empty inputs in 0.5 sec range do a rednet scan, if not empty dont re-scan.
  606.                 if #tServers == 0 then
  607.                     tServers = {""}
  608.                     tGhosts  =  {"[No Servers Found. Re-Tap to Re-Scan]"}
  609.                     nLasttab = os.clock()
  610.                 else
  611.                     nLasttab = os.clock() + 30 -- Block scanning for 30 sec so it won't scan over and over again in row.
  612.                 end
  613.             else
  614.                 tServers = {""}
  615.                 tGhosts =  {"[Double-Tap Tab to Scan]"}
  616.                 nLasttab = os.clock()
  617.             end
  618.         end
  619.         return completeMultipleChoice( sText, tServers , true ,tGhosts)
  620.     elseif nIndex == 3 and tPreviousText[2] == "join" then
  621.         local tNames = {}
  622.         for sName in string.gmatch( getSetting("shellUtil.chat_names"), "[^,]+" ) do table.insert(tNames,sName) end
  623.         return completeMultipleChoice( sText, tNames)
  624.     end
  625. end
  626.  
  627. --# Apply said functions
  628. shell.setCompletionFunction( "rom/programs/eject.lua", completeEject )
  629. shell.setCompletionFunction( "rom/programs/gps.lua", completeGPS )
  630. shell.setCompletionFunction( "rom/programs/label.lua", completeLabel )
  631. shell.setCompletionFunction( "rom/programs/monitor.lua", completeMonitor )
  632. shell.setCompletionFunction( "rom/programs/set.lua", completeSet )
  633.  
  634. shell.setCompletionFunction( "rom/programs/fun/dj.lua", completeDJ )
  635.  
  636. shell.setCompletionFunction( "rom/programs/http/pastebin.lua", completePastebin )
  637.  
  638. shell.setCompletionFunction( "rom/programs/rednet/chat.lua", completeChat )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement