Operating system for OpenComputers
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

48 lines
1.1KB

  1. local serial = require "serialization"
  2. local shell = {}
  3. shell.include = {"shutil"}
  4. local function shindex(self,k)
  5. if rawget(self,k) then return rawget(self,k) end
  6. for _,v in pairs(os.getenv("INCLUDE") or shell.include) do
  7. if require(v)[k] then return require(v)[k] end
  8. end
  9. if package.loaded[k] then return package.loaded[k] end
  10. return _G[k]
  11. end
  12. local function formatValue(v)
  13. if type(v) == "table" then
  14. local w, rs = pcall(serial.serialize,v,true)
  15. if w then return rs end
  16. end
  17. return tostring(v)
  18. end
  19. function shell.interactive()
  20. local shenv = setmetatable({}, {__index=shindex})
  21. local run = true
  22. function shenv.quit()
  23. run = false
  24. end
  25. while run do
  26. io.write(string.format("\27[32m%s:%s>\27[0m ",os.getenv("HOSTNAME") or "localhost",(os.getenv("PWD") or _VERSION)))
  27. local input = io.read()
  28. if input:sub(1,1) == "=" then
  29. input = "return "..input:sub(2)
  30. end
  31. local f, r = load(input, "shell", "t", shenv)
  32. if not f then
  33. print("\27[31m"..r)
  34. else
  35. local rt = {pcall(f)}
  36. local rs = table.remove(rt,1)
  37. if not rs then io.write("\27[31m") end
  38. for k,v in pairs(rt) do
  39. print(formatValue(v))
  40. end
  41. end
  42. end
  43. end
  44. return shell