Operating system for OpenComputers
25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

123 satır
2.6KB

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