Advertisement
Spytox

startup

Jan 21st, 2025 (edited)
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.38 KB | Gaming | 0 0
  1. local completion = require "cc.shell.completion"
  2.  
  3. -- Setup paths
  4. local sPath = ".:/rom/programs:/rom/programs/http"
  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. shell.setPath(sPath)
  23. help.setPath("/rom/help")
  24.  
  25. -- Setup aliases
  26. shell.setAlias("ls", "list")
  27. shell.setAlias("dir", "list")
  28. shell.setAlias("cp", "copy")
  29. shell.setAlias("mv", "move")
  30. shell.setAlias("rm", "delete")
  31. shell.setAlias("clr", "clear")
  32. shell.setAlias("rs", "redstone")
  33. shell.setAlias("sh", "shell")
  34. if term.isColor() then
  35.     shell.setAlias("background", "bg")
  36.     shell.setAlias("foreground", "fg")
  37. end
  38.  
  39. -- Setup completion functions
  40.  
  41. local function completePastebinPut(shell, text, previous)
  42.     if previous[2] == "put" then
  43.         return fs.complete(text, shell.dir(), true, false)
  44.     end
  45. end
  46.  
  47. shell.setCompletionFunction("rom/programs/alias.lua", completion.build(nil, completion.program))
  48. shell.setCompletionFunction("rom/programs/cd.lua", completion.build(completion.dir))
  49. shell.setCompletionFunction("rom/programs/clear.lua", completion.build({ completion.choice, { "screen", "palette", "all" } }))
  50. shell.setCompletionFunction("rom/programs/copy.lua", completion.build(
  51.     { completion.dirOrFile, true },
  52.     completion.dirOrFile
  53. ))
  54. shell.setCompletionFunction("rom/programs/delete.lua", completion.build({ completion.dirOrFile, many = true }))
  55. shell.setCompletionFunction("rom/programs/drive.lua", completion.build(completion.dir))
  56. shell.setCompletionFunction("rom/programs/edit.lua", completion.build(completion.file))
  57. shell.setCompletionFunction("rom/programs/eject.lua", completion.build(completion.peripheral))
  58. shell.setCompletionFunction("rom/programs/gps.lua", completion.build({ completion.choice, { "host", "host ", "locate" } }))
  59. shell.setCompletionFunction("rom/programs/help.lua", completion.build(completion.help))
  60. shell.setCompletionFunction("rom/programs/id.lua", completion.build(completion.peripheral))
  61. shell.setCompletionFunction("rom/programs/label.lua", completion.build(
  62.     { completion.choice, { "get", "get ", "set ", "clear", "clear " } },
  63.     completion.peripheral
  64. ))
  65. shell.setCompletionFunction("rom/programs/list.lua", completion.build(completion.dir))
  66. shell.setCompletionFunction("rom/programs/mkdir.lua", completion.build({ completion.dir, many = true }))
  67.  
  68. local complete_monitor_extra = { "scale" }
  69. shell.setCompletionFunction("rom/programs/monitor.lua", completion.build(
  70.     function(shell, text, previous)
  71.         local choices = completion.peripheral(shell, text, previous, true)
  72.         for _, option in pairs(completion.choice(shell, text, previous, complete_monitor_extra, true)) do
  73.             choices[#choices + 1] = option
  74.         end
  75.         return choices
  76.     end,
  77.     function(shell, text, previous)
  78.         if previous[2] == "scale" then
  79.             return completion.peripheral(shell, text, previous, true)
  80.         else
  81.             return completion.programWithArgs(shell, text, previous, 3)
  82.         end
  83.     end,
  84.     {
  85.         function(shell, text, previous)
  86.             if previous[2] ~= "scale" then
  87.                 return completion.programWithArgs(shell, text, previous, 3)
  88.             end
  89.         end,
  90.         many = true,
  91.     }
  92. ))
  93.  
  94. shell.setCompletionFunction("rom/programs/move.lua", completion.build(
  95.     { completion.dirOrFile, true },
  96.     completion.dirOrFile
  97. ))
  98. shell.setCompletionFunction("rom/programs/redstone.lua", completion.build(
  99.     { completion.choice, { "probe", "set ", "pulse " } },
  100.     completion.side
  101. ))
  102. shell.setCompletionFunction("rom/programs/rename.lua", completion.build(
  103.     { completion.dirOrFile, true },
  104.     completion.dirOrFile
  105. ))
  106. shell.setCompletionFunction("rom/programs/shell.lua", completion.build({ completion.programWithArgs, 2, many = true }))
  107. shell.setCompletionFunction("rom/programs/type.lua", completion.build(completion.dirOrFile))
  108. shell.setCompletionFunction("rom/programs/set.lua", completion.build({ completion.setting, true }))
  109. shell.setCompletionFunction("rom/programs/advanced/bg.lua", completion.build({ completion.programWithArgs, 2, many = true }))
  110. shell.setCompletionFunction("rom/programs/advanced/fg.lua", completion.build({ completion.programWithArgs, 2, many = true }))
  111. shell.setCompletionFunction("rom/programs/fun/dj.lua", completion.build(
  112.     { completion.choice, { "play", "play ", "stop " } },
  113.     completion.peripheral
  114. ))
  115. shell.setCompletionFunction("rom/programs/fun/speaker.lua", completion.build(
  116.     { completion.choice, { "play ", "stop " } },
  117.     function(shell, text, previous)
  118.         if previous[2] == "play" then return completion.file(shell, text, previous, true)
  119.         elseif previous[2] == "stop" then return completion.peripheral(shell, text, previous, false)
  120.         end
  121.     end,
  122.     function(shell, text, previous)
  123.         if previous[2] == "play" then return completion.peripheral(shell, text, previous, false)
  124.         end
  125.     end
  126. ))
  127. shell.setCompletionFunction("rom/programs/fun/advanced/paint.lua", completion.build(completion.file))
  128. shell.setCompletionFunction("rom/programs/http/pastebin.lua", completion.build(
  129.     { completion.choice, { "put ", "get ", "run " } },
  130.     completePastebinPut
  131. ))
  132. shell.setCompletionFunction("rom/programs/rednet/chat.lua", completion.build({ completion.choice, { "host ", "join " } }))
  133. shell.setCompletionFunction("rom/programs/command/exec.lua", completion.build(completion.command))
  134. shell.setCompletionFunction("rom/programs/http/wget.lua", completion.build({ completion.choice, { "run " } }))
  135.  
  136. if turtle then
  137.     shell.setCompletionFunction("rom/programs/turtle/go.lua", completion.build(
  138.         { completion.choice, { "left", "right", "forward", "back", "down", "up" }, true, many = true }
  139.     ))
  140.     shell.setCompletionFunction("rom/programs/turtle/turn.lua", completion.build(
  141.         { completion.choice, { "left", "right" }, true, many = true }
  142.     ))
  143.     shell.setCompletionFunction("rom/programs/turtle/equip.lua", completion.build(
  144.         nil,
  145.         { completion.choice, { "left", "right" } }
  146.     ))
  147.     shell.setCompletionFunction("rom/programs/turtle/unequip.lua", completion.build(
  148.         { completion.choice, { "left", "right" } }
  149.     ))
  150. end
  151.  
  152. -- Run autorun files
  153. if fs.exists("/rom/autorun") and fs.isDir("/rom/autorun") then
  154.     local tFiles = fs.list("/rom/autorun")
  155.     for _, sFile in ipairs(tFiles) do
  156.         if string.sub(sFile, 1, 1) ~= "." then
  157.             local sPath = "/rom/autorun/" .. sFile
  158.             if not fs.isDir(sPath) then
  159.                 shell.run(sPath)
  160.             end
  161.         end
  162.     end
  163. end
  164.  
  165. local function findStartups(sBaseDir)
  166.     local tStartups = nil
  167.     local sBasePath = "/" .. fs.combine(sBaseDir, "startup")
  168.     local sStartupNode = shell.resolveProgram(sBasePath)
  169.     if sStartupNode then
  170.         tStartups = { sStartupNode }
  171.     end
  172.     -- It's possible that there is a startup directory and a startup.lua file, so this has to be
  173.     -- executed even if a file has already been found.
  174.     if fs.isDir(sBasePath) then
  175.         if tStartups == nil then
  176.             tStartups = {}
  177.         end
  178.         for _, v in pairs(fs.list(sBasePath)) do
  179.             local sPath = "/" .. fs.combine(sBasePath, v)
  180.             if not fs.isDir(sPath) then
  181.                 tStartups[#tStartups + 1] = sPath
  182.             end
  183.         end
  184.     end
  185.     return tStartups
  186. end
  187.  
  188. -- Show MOTD
  189. if settings.get("motd.enable") then
  190.     shell.run("motd")
  191. end
  192.  
  193. -- Run the user created startup, either from disk drives or the root
  194. local tUserStartups = nil
  195. if settings.get("shell.allow_startup") then
  196.     tUserStartups = findStartups("/")
  197. end
  198. if settings.get("shell.allow_disk_startup") then
  199.     for _, sName in pairs(peripheral.getNames()) do
  200.         if disk.isPresent(sName) and disk.hasData(sName) then
  201.             local startups = findStartups(disk.getMountPath(sName))
  202.             if startups then
  203.                 tUserStartups = startups
  204.                 break
  205.             end
  206.         end
  207.     end
  208. end
  209. if tUserStartups then
  210.     for _, v in pairs(tUserStartups) do
  211.         shell.run(v)
  212.     end
  213. end
  214. shell.run("Cobble")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement