Guest User

startup

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