Advertisement
Guest User

startup

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