Advertisement
Derek1017

VPS

Feb 27th, 2016
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.06 KB | None | 0 0
  1. -- Setup paths
  2. local sPath = ".:/VPS/programs"
  3. if term.isColor() then
  4.     sPath = sPath..":/VPS/programs/advanced"
  5. end
  6. if turtle then
  7.     sPath = sPath..":/VPS/programs/turtle"
  8. else
  9.     sPath = sPath..":/VPS/programs/rednet:/VPS/programs/fun"
  10.     if term.isColor() then
  11.         sPath = sPath..":/VPS/programs/fun/advanced"
  12.     end
  13. end
  14. if pocket then
  15.     sPath = sPath..":/VPS/programs/pocket"
  16. end
  17. if commands then
  18.     sPath = sPath..":/VPS/programs/command"
  19. end
  20. if http then
  21.     sPath = sPath..":/VPS/programs/http"
  22. end
  23. shell.setPath( sPath )
  24. help.setPath( "/VPS/help" )
  25.  
  26. -- Setup aliases
  27. shell.setAlias( "ls", "list" )
  28. shell.setAlias( "dir", "list" )
  29. shell.setAlias( "cp", "copy" )
  30. shell.setAlias( "mv", "move" )
  31. shell.setAlias( "rm", "delete" )
  32. shell.setAlias( "clr", "clear" )
  33. shell.setAlias( "rs", "redstone" )
  34. shell.setAlias( "sh", "shell" )
  35. if term.isColor() then
  36.     shell.setAlias( "background", "bg" )
  37.     shell.setAlias( "foreground", "fg" )
  38. end
  39.  
  40. -- Setup completion functions
  41. local function completeMultipleChoice( sText, tOptions, bAddSpaces )
  42.     local tResults = {}
  43.     for n=1,#tOptions do
  44.         local sOption = tOptions[n]
  45.         if #sOption + (bAddSpaces and 1 or 0) > #sText and string.sub( sOption, 1, #sText ) == sText then
  46.             local sResult = string.sub( sOption, #sText + 1 )
  47.             if bAddSpaces then
  48.                 table.insert( tResults, sResult .. " " )
  49.             else
  50.                 table.insert( tResults, sResult )
  51.             end
  52.         end
  53.     end
  54.     return tResults
  55. end
  56. local function completePeripheralName( sText, bAddSpaces )
  57.     return completeMultipleChoice( sText, peripheral.getNames(), bAddSpaces )
  58. end
  59. local tRedstoneSides = redstone.getSides()
  60. local function completeSide( sText, bAddSpaces )
  61.     return completeMultipleChoice( sText, tRedstoneSides, bAddSpaces )
  62. end
  63. local function completeFile( shell, nIndex, sText, tPreviousText )
  64.     if nIndex == 1 then
  65.         return fs.complete( sText, shell.dir(), true, false )
  66.     end
  67. end
  68. local function completeDir( shell, nIndex, sText, tPreviousText )
  69.     if nIndex == 1 then
  70.         return fs.complete( sText, shell.dir(), false, true )
  71.     end
  72. end
  73. local function completeEither( shell, nIndex, sText, tPreviousText )
  74.     if nIndex == 1 then
  75.         return fs.complete( sText, shell.dir(), true, true )
  76.     end
  77. end
  78. local function completeEitherEither( shell, nIndex, sText, tPreviousText )
  79.     if nIndex == 1 then
  80.         local tResults = fs.complete( sText, shell.dir(), true, true )
  81.         for n=1,#tResults do
  82.             local sResult = tResults[n]
  83.             if string.sub( sResult, #sResult, #sResult ) ~= "/" then
  84.                 tResults[n] = sResult .. " "
  85.             end
  86.         end
  87.         return tResults
  88.     elseif nIndex == 2 then
  89.         return fs.complete( sText, shell.dir(), true, true )
  90.     end
  91. end
  92. local function completeProgram( shell, nIndex, sText, tPreviousText )
  93.     if nIndex == 1 then
  94.         return shell.completeProgram( sText )
  95.     end
  96. end
  97. local function completeHelp( shell, nIndex, sText, tPreviousText )
  98.     if nIndex == 1 then
  99.         return help.completeTopic( sText )
  100.     end
  101. end
  102. local function completeAlias( shell, nIndex, sText, tPreviousText )
  103.     if nIndex == 2 then
  104.         return shell.completeProgram( sText )
  105.     end
  106. end
  107. local function completePeripheral( shell, nIndex, sText, tPreviousText )
  108.     if nIndex == 1 then
  109.         return completePeripheralName( sText )
  110.     end
  111. end
  112. local tGPSOptions = { "host", "host ", "locate" }
  113. local function completeGPS( shell, nIndex, sText, tPreviousText )
  114.     if nIndex == 1 then
  115.         return completeMultipleChoice( sText, tGPSOptions )
  116.     end
  117. end
  118. local tLabelOptions = { "get", "get ", "set ", "clear", "clear " }
  119. local function completeLabel( shell, nIndex, sText, tPreviousText )
  120.     if nIndex == 1 then
  121.         return completeMultipleChoice( sText, tLabelOptions )
  122.     elseif nIndex == 2 then
  123.         return completePeripheralName( sText )
  124.     end
  125. end
  126. local function completeMonitor( shell, nIndex, sText, tPreviousText )
  127.     if nIndex == 1 then
  128.         return completePeripheralName( sText, true )
  129.     elseif nIndex == 2 then
  130.         return shell.completeProgram( sText )
  131.     end
  132. end
  133. local tRedstoneOptions = { "probe", "set ", "pulse " }
  134. local function completeRedstone( shell, nIndex, sText, tPreviousText )
  135.     if nIndex == 1 then
  136.         return completeMultipleChoice( sText, tRedstoneOptions )
  137.     elseif nIndex == 2 then
  138.         return completeSide( sText )
  139.     end
  140. end
  141. local tDJOptions = { "play", "play ", "stop " }
  142. local function completeDJ( shell, nIndex, sText, tPreviousText )
  143.     if nIndex == 1 then
  144.         return completeMultipleChoice( sText, tDJOptions )
  145.     elseif nIndex == 2 then
  146.         return completePeripheralName( sText )
  147.     end
  148. end
  149. local tPastebinOptions = { "put ", "get ", "run " }
  150. local function completePastebin( shell, nIndex, sText, tPreviousText )
  151.     if nIndex == 1 then
  152.         return completeMultipleChoice( sText, tPastebinOptions )
  153.     elseif nIndex == 2 then
  154.         if tPreviousText[2] == "put" then
  155.             return fs.complete( sText, shell.dir(), true, false )
  156.         end
  157.     end
  158. end
  159. local tChatOptions = { "host ", "join " }
  160. local function completeChat( shell, nIndex, sText, tPreviousText )
  161.     if nIndex == 1 then
  162.         return completeMultipleChoice( sText, tChatOptions )
  163.     end
  164. end
  165. shell.setCompletionFunction( "rom/programs/alias", completeAlias )
  166. shell.setCompletionFunction( "rom/programs/cd", completeDir )
  167. shell.setCompletionFunction( "rom/programs/copy", completeEitherEither )
  168. shell.setCompletionFunction( "rom/programs/delete", completeEither )
  169. shell.setCompletionFunction( "rom/programs/drive", completeDir )
  170. shell.setCompletionFunction( "rom/programs/edit", completeFile )
  171. shell.setCompletionFunction( "rom/programs/eject", completePeripheral )
  172. shell.setCompletionFunction( "rom/programs/gps", completeGPS )
  173. shell.setCompletionFunction( "rom/programs/help", completeHelp )
  174. shell.setCompletionFunction( "rom/programs/id", completePeripheral )
  175. shell.setCompletionFunction( "rom/programs/label", completeLabel )
  176. shell.setCompletionFunction( "rom/programs/list", completeDir )
  177. shell.setCompletionFunction( "rom/programs/mkdir", completeFile )
  178. shell.setCompletionFunction( "rom/programs/monitor", completeMonitor )
  179. shell.setCompletionFunction( "rom/programs/move", completeEitherEither )
  180. shell.setCompletionFunction( "rom/programs/redstone", completeRedstone )
  181. shell.setCompletionFunction( "rom/programs/rename", completeEitherEither )
  182. shell.setCompletionFunction( "rom/programs/shell", completeProgram )
  183. shell.setCompletionFunction( "rom/programs/type", completeEither )
  184. shell.setCompletionFunction( "rom/programs/advanced/bg", completeProgram )
  185. shell.setCompletionFunction( "rom/programs/advanced/fg", completeProgram )
  186. shell.setCompletionFunction( "rom/programs/fun/dj", completeDJ )
  187. shell.setCompletionFunction( "rom/programs/fun/advanced/paint", completeFile )
  188. shell.setCompletionFunction( "rom/programs/http/pastebin", completePastebin )
  189. shell.setCompletionFunction( "rom/programs/rednet/chat", completeChat )
  190.  
  191. -- Run autorun files
  192. if fs.exists( "/VPS/autorun" ) and fs.isDir( "/VPS/autorun" ) then
  193.     local tFiles = fs.list( "/VPS/autorun" )
  194.     table.sort( tFiles )
  195.     for n, sFile in ipairs( tFiles ) do
  196.         if string.sub( sFile, 1, 1 ) ~= "." then
  197.             local sPath = "/VPS/autorun/"..sFile
  198.             if not fs.isDir( sPath ) then
  199.                 shell.run( sPath )
  200.             end
  201.         end
  202.     end
  203. end
  204.  
  205. -- Run the user created startup, either from disk drives or the root
  206. local sUserStartup = shell.resolveProgram( "/startup" )
  207. local bEnableDiskStartup = (commands == nil)
  208. if bEnableDiskStartup then
  209.     for n,sName in pairs( peripheral.getNames() ) do
  210.         if disk.isPresent( sName ) and disk.hasData( sName ) then
  211.             local sDiskStartup = shell.resolveProgram( "/" .. disk.getMountPath( sName ) .. "/startup" )
  212.             if sDiskStartup then
  213.                 sUserStartup = sDiskStartup
  214.                 break
  215.             end
  216.         end
  217.     end
  218. end
  219. if sUserStartup then
  220.     shell.run( sUserStartup )
  221. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement