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.

128 lines
3.2KB

  1. local component = require "component"
  2. local fs = require "fs"
  3. local shell = require "shell"
  4. local ed = require "ed"
  5. local doc = require "doc"
  6. local shutil = {}
  7. shutil.ed = ed.interactive
  8. shutil.vi = ed.visual
  9. shutil.buffers = ed.ifunc.buffers
  10. local function wrapUnits(n)
  11. local scale = {"K","M","G","T","P"}
  12. local count = 0
  13. while n >= 1024 do
  14. count = count + 1
  15. if not scale[count] then return "inf" end
  16. n = n / 1024
  17. end
  18. return tostring(math.floor(n))..(scale[count] or "")
  19. end
  20. function shutil.import(lib) -- string -- boolean -- Imports the functions from library *lib* into the shell environment.
  21. local cE = os.getenv("INCLUDE") or shell.include
  22. local nE = {}
  23. for k,v in pairs(cE) do
  24. nE[#nE+1] = v
  25. end
  26. require(lib)
  27. nE[#nE+1] = lib
  28. os.setenv("INCLUDE",nE)
  29. return true
  30. end
  31. function shutil.unimport(lib) -- string -- boolean -- Removes the functions from *lib* from the shell environment.
  32. local cE = os.getenv("INCLUDE") or shell.include
  33. local nE = {}
  34. for k,v in pairs(cE) do
  35. if v ~= lib then
  36. nE[#nE+1] = v
  37. end
  38. end
  39. os.setenv("INCLUDE",nE)
  40. return true
  41. end
  42. function shutil.ls(...) -- string -- -- Prints contents of directories specified as *...*.
  43. local tA = {...}
  44. if not tA[1] then tA[1] = "." end
  45. for _,d in ipairs(tA) do
  46. if #tA > 1 then
  47. print(d..":")
  48. end
  49. for _,f in ipairs(fs.list(d)) do
  50. print(" "..f)
  51. end
  52. end
  53. end
  54. function shutil.cat(...) -- string -- -- Outputs the contents of files specified in *...* to the standard output.
  55. for _,fn in ipairs({...}) do
  56. local f = io.open(fn,"rb")
  57. io.write(f:read("*a"))
  58. f:close()
  59. end
  60. end
  61. function shutil.ps() -- Prints the processes running on the system.
  62. print("PID# Parent | Name")
  63. for k,v in pairs(os.tasks()) do
  64. local t = os.taskInfo(v)
  65. print(string.format("%4d %4d | %s",v,t.parent,t.name))
  66. end
  67. end
  68. function shutil.df() -- Prints free disk space.
  69. local mt = fs.mounts()
  70. local ml = 0
  71. for k,v in pairs(mt) do
  72. if v:len() > ml then
  73. ml = v:len()
  74. end
  75. end
  76. local fstr = "%-"..tostring(ml).."s %5s %5s"
  77. print("fs"..(" "):rep(ml-2).." size used")
  78. for k,v in pairs(mt) do
  79. local st, su = fs.spaceTotal(v), fs.spaceUsed(v)
  80. print(string.format(fstr,v,wrapUnits(st),wrapUnits(su)))
  81. end
  82. end
  83. function shutil.mount(addr,path) -- string string -- boolean string -- Mounts filesystem component with address *addr* to *path* in the filesystem.
  84. if not addr then
  85. local mt = fs.mounts()
  86. for k,v in pairs(mt) do
  87. v = "/"..table.concat(fs.segments(v),"/")
  88. print(tostring(fs.address(v)).." on "..tostring(v).." type "..fs.type(v))
  89. end
  90. else
  91. local fscomp = component.list("filesystem")
  92. if not fscomp[addr] then
  93. for k,v in pairs(fscomp) do
  94. if k:find(addr) then
  95. print(tostring(addr).." not found, assuming you meant "..k)
  96. addr = k
  97. break
  98. end
  99. end
  100. end
  101. local proxy = component.proxy(addr)
  102. if not proxy then
  103. return false, "no such filesystem component"
  104. end
  105. print(fs.mount(path,proxy))
  106. end
  107. end
  108. function shutil.free() -- Displays used and free memory.
  109. print("Total Used Free")
  110. print(string.format("%5s %5s %5s",wrapUnits(computer.totalMemory()),wrapUnits(computer.totalMemory()-computer.freeMemory()),wrapUnits(computer.freeMemory())))
  111. end
  112. shutil.cd = os.chdir
  113. shutil.mkdir = fs.makeDirectory
  114. shutil.cp = fs.copy
  115. shutil.rm = fs.remove
  116. return shutil