Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- run with:
- -- pastebin run Bvdx6gCn
- -- returns number of bytes
- local function du_singular(path)
- if fs.getDrive(path) == "rom" then
- return 0
- end
- if not fs.isDir(path) then
- -- just a file
- return fs.getSize(path)
- end
- local l = fs.list(path)
- local size = 0
- for i,entry in ipairs(l) do
- local subpath = fs.combine(path,entry)
- size = size + du_singular(subpath)
- end
- return size
- end
- local suffixes = {" B","kB","MB","GB","TB","PB","EB"}
- local function format_size(size)
- local unit = 1
- while size > 999 do
- size = size / 1000
- unit = unit + 1
- end
- size = string.format("%f",size)
- size = string.format("%.4s",size)
- size = (size:gsub("%.$"," "))
- return string.format("%s %s", size, suffixes[unit])
- end
- local workdir = shell.dir()
- local listing = {}
- for _,file in ipairs(fs.list(workdir)) do
- local path = fs.combine(workdir, file)
- if fs.isDir(path) then
- file = file.."/"
- end
- table.insert(listing, {
- file = file,
- size = du_singular(path)
- })
- -- print(format_size(du_singular(path))..": "..entry)
- end
- table.sort(listing, function (a, b)
- if a.size ~= b.size then
- return a.size > b.size
- end
- return a.file < b.file
- end)
- local result = ""
- for _,entry in ipairs(listing) do
- result = result .. format_size(entry.size)..": "..entry.file .. "\n"
- end
- result = result:gsub("\n$","")
- local _, y = term.getCursorPos()
- -- FIXME: this pager sucks, I'd like a better one
- textutils.pagedPrint(result, y-2)
Advertisement
Add Comment
Please, Sign In to add comment