Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local du = "du --max-depth=1 -ba "
- local ls = "ls --group-directories-first --time=ctime --color --time-style=+%s -Lla "
- local format = {
- du = {"(%d+)","(.+)",},
- ls = {"([^%s]+)","%d+","%w+","%w+","%d+","(%d+)","(.+)"}
- }
- local separator = "‧"
- local cols = io.popen("tput cols"):read()
- local function bar(part,width)
- return ("1"):rep(math.floor(part*width))..
- (part*width-math.floor(part*width) >= 0.5 and "2" or "")
- end
- local function form(n)
- if tonumber(n) >= 1024^3 then
- return (math.floor((n/1024^3)*100+0.5)/100).." GB"
- elseif tonumber(n) >= 1024^2 then
- return (math.floor((n/1024^2)*100+0.5)/100).." MB"
- elseif tonumber(n) >= 1024 then
- return (math.floor((n/1024)*100+0.5)/100).." kB"
- else
- return n.." B"
- end
- end
- local function colorscale(value,max)
- return ("\027[%sm"):format(({
- "1;32","0;32","0;33","0;31","1;31"
- })[math.min(math.floor(value/(max/5))+1,5)])
- end
- local function formtime(n)
- local d,h,m,s = os.date("!%j %H %M %S",n):match("(.+)%s(.+)%s(.+)%s(.+)")
- d,h,m,s = d - 1,tonumber(h),tonumber(m),tonumber(s)
- if d > 0 then
- if d >= 7 then
- local w = math.floor(d/7+0.5)
- return w.." week"..(w == 1 and "" or "s")
- else
- return d.." day"..(d == 1 and "" or "s")
- end
- elseif h > 0 then
- return h.." hour"..(h == 1 and "" or "s")
- elseif m > 0 then
- return m.." minute"..(m == 1 and "" or "s")
- else
- return s.." second"..(s == 1 and "" or "s")
- end
- end
- local dir,all = "."
- do -- Process arguments
- for i = 1,#arg do
- local flag = arg[i]
- if not flag:match("-") then
- dir = flag:gsub("~",os.getenv("HOME"))
- elseif flag == "-a" then
- all = true
- end
- end
- end
- local info = {ls = {},du = {}}
- do -- Populate information table
- for line in io.popen(du..dir):lines() do
- local size,name = line:match(table.concat(format.du,"%s+"))
- name = name:gsub(dir.."/","") == "" and dir or name:gsub(dir.."/","")
- info.du[name] = size
- end
- for line in io.popen(ls..dir):lines() do
- local perm,time,name = line:match(table.concat(format.ls,"%s+"))
- if name then
- local name = name:gsub("\027%[0m","")
- local color = name:match("\027%[[%d;]+m") or ""
- local name = name:gsub("\027%[[%d;]+m","")
- local hidden = name:sub(1,1) == "."
- local implied = name == ".." or name == "."
- if (not implied) and ((all and hidden) or not hidden) then
- table.insert(info.ls,{perm,time,name,color})
- end
- end
- end
- end
- local total,max,last = 0,0,0
- do -- Get total size of all lines shown
- for _,ls in pairs(info.ls) do
- total = total + (info.du[ls[3]] or 0)
- max = math.max(max,info.du[ls[3]] or 0)
- end
- end
- do -- Get newest update time
- for _,ls in pairs(info.ls) do
- last = math.max(last,tonumber(ls[2]))
- end
- end
- do -- Print column headings
- local size = form(total)
- local last = formtime(os.time() - last)
- last = last:gsub(" ",(" "):rep(11-last:len()))
- io.write(" \027[1;30m│↷➘➙➚➘➙➚➘➙➚│∣")
- io.write(last.."\027[1;30m∣")
- io.write((" "):rep(10-size:len())..size.."\027[1;30m∣")
- io.write("\n")
- end
- for _,ls in pairs(info.ls) do
- local perm,date,name,color = unpack(ls)
- local format,filesize,timemod,barf
- local bar = bar(info.du[name]/max,20)
- bar = string.reverse(bar == "" and 2 or bar)
- do -- color format
- format = perm:gsub("%+","") -- Take care of Cygwin's extra '+'
- format = "├"..format:gsub("-","─").."┤"
- for a,b in pairs{r = "2",w = "3",x = "1",d = "2"} do
- format = "\027[1;30m"..format:gsub(a,"\027[1;3"..b.."m%1\027[1;30m")
- end
- end
- do -- size format
- if info.du[name] then
- local form = form(info.du[name])
- local size,sp,unit = form:match("([%d%.]+)(%s+)(%a+)")
- filesize = (
- (" "):rep(10-form:len())..
- "\027[1;33m"..size..
- sp..("\027[1;3%sm"):format(({B=4,kB = 2,MB = 1,GB = 5})[unit])..
- unit)
- else
- filesize = " "
- end
- end
- do -- bar format
- barf = bar:gsub("1","█"):gsub("2","▐")
- end
- do -- time format
- local time = formtime(os.time() - date)
- local number,unit = time:match("(%d+)%s(%a+)")
- timemod = ("\027[1;3%sm%s%s%s"):format(
- ({sec=5,min=2,hou=3,day=4,wee=7,})[unit:sub(1,3)],
- number,
- (" "):rep(11-time:len()),
- unit)
- end
- do -- name format
- namef = (color == "" and "\027[39m" or color)..name.."\027[0m"
- end
- io.write(" ".. format.. "\027[1;30m∣")
- io.write( timemod.. "\027[1;30m∣")
- io.write( filesize.. "\027[1;30m∣")
- io.write( namef.. (" "):rep((cols-36-name:len())-bar:len()))
- io.write(colorscale(info.du[name],max)..barf)
- --date,name,info.du[name]
- io.write("\n")
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement