Advertisement
Guest User

ldu

a guest
Aug 22nd, 2014
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.58 KB | None | 0 0
  1. local du = "du --max-depth=1 -ba "
  2. local ls = "ls --group-directories-first --time=ctime --color --time-style=+%s -Lla "
  3. local format = {
  4.     du = {"(%d+)","(.+)",},
  5.     ls = {"([^%s]+)","%d+","%w+","%w+","%d+","(%d+)","(.+)"}
  6. }
  7.  
  8. local separator = "‧"
  9.  
  10. local cols = io.popen("tput cols"):read()
  11.  
  12. local function bar(part,width)
  13.     return ("1"):rep(math.floor(part*width))..
  14.         (part*width-math.floor(part*width) >= 0.5 and "2" or "")
  15. end
  16.  
  17. local function form(n)
  18.     if tonumber(n) >= 1024^3 then
  19.         return (math.floor((n/1024^3)*100+0.5)/100).." GB"
  20.     elseif tonumber(n) >= 1024^2 then
  21.         return (math.floor((n/1024^2)*100+0.5)/100).." MB"
  22.     elseif tonumber(n) >= 1024 then
  23.         return (math.floor((n/1024)*100+0.5)/100).." kB"
  24.     else
  25.         return n.."  B"
  26.     end
  27. end
  28.  
  29. local function colorscale(value,max)
  30.     return ("\027[%sm"):format(({
  31.         "1;32","0;32","0;33","0;31","1;31"
  32.     })[math.min(math.floor(value/(max/5))+1,5)])
  33. end
  34.  
  35. local function formtime(n)
  36.     local d,h,m,s = os.date("!%j %H %M %S",n):match("(.+)%s(.+)%s(.+)%s(.+)")
  37.     d,h,m,s = d - 1,tonumber(h),tonumber(m),tonumber(s)
  38.     if d > 0 then
  39.         if d >= 7 then
  40.             local w = math.floor(d/7+0.5)
  41.             return w.." week"..(w == 1 and "" or "s")
  42.         else
  43.             return d.." day"..(d == 1 and "" or "s")
  44.         end
  45.     elseif h > 0 then
  46.         return h.." hour"..(h == 1 and "" or "s")
  47.     elseif m > 0 then
  48.         return m.." minute"..(m == 1 and "" or "s")
  49.     else
  50.         return s.." second"..(s == 1 and "" or "s")
  51.     end
  52. end
  53.  
  54. local dir,all = "."
  55.  
  56. do  --  Process arguments
  57.     for i = 1,#arg do
  58.         local flag = arg[i]
  59.         if not flag:match("-") then
  60.             dir = flag:gsub("~",os.getenv("HOME"))
  61.         elseif flag == "-a" then
  62.             all = true
  63.         end
  64.     end
  65. end
  66.  
  67. local info = {ls = {},du = {}}
  68.  
  69. do --  Populate information table
  70.     for line in io.popen(du..dir):lines() do
  71.         local size,name = line:match(table.concat(format.du,"%s+"))
  72.         name = name:gsub(dir.."/","") == "" and dir or name:gsub(dir.."/","")
  73.         info.du[name] = size
  74.     end
  75.     for line in io.popen(ls..dir):lines() do
  76.         local perm,time,name = line:match(table.concat(format.ls,"%s+"))
  77.         if name then
  78.             local name = name:gsub("\027%[0m","")
  79.             local color = name:match("\027%[[%d;]+m") or ""
  80.             local name = name:gsub("\027%[[%d;]+m","")
  81.             local hidden = name:sub(1,1) == "."
  82.             local implied = name == ".." or name == "."
  83.             if (not implied) and ((all and hidden) or not hidden) then
  84.                 table.insert(info.ls,{perm,time,name,color})
  85.             end
  86.         end
  87.     end
  88. end
  89.  
  90. local total,max,last = 0,0,0
  91.  
  92. do --  Get total size of all lines shown
  93.     for _,ls in pairs(info.ls) do
  94.         total = total + (info.du[ls[3]] or 0)
  95.         max = math.max(max,info.du[ls[3]] or 0)
  96.     end
  97. end
  98.  
  99. do --  Get newest update time
  100.     for _,ls in pairs(info.ls) do
  101.         last = math.max(last,tonumber(ls[2]))
  102.     end
  103. end
  104.  
  105. do --  Print column headings
  106.     local size = form(total)
  107.     local last = formtime(os.time() - last)
  108.     last = last:gsub(" ",(" "):rep(11-last:len()))
  109.     io.write(" \027[1;30m│↷➘➙➚➘➙➚➘➙➚│∣")
  110.     io.write(last.."\027[1;30m∣")
  111.     io.write((" "):rep(10-size:len())..size.."\027[1;30m∣")
  112.     io.write("\n")
  113. end
  114.  
  115. for _,ls in pairs(info.ls) do
  116.     local perm,date,name,color = unpack(ls)
  117.     local format,filesize,timemod,barf
  118.    
  119.     local bar = bar(info.du[name]/max,20)
  120.     bar = string.reverse(bar == "" and 2 or bar)
  121.    
  122.     do --  color format
  123.         format = perm:gsub("%+","") --  Take care of Cygwin's extra '+'
  124.         format = "├"..format:gsub("-","─").."┤"
  125.         for a,b in pairs{r = "2",w = "3",x = "1",d = "2"} do
  126.             format = "\027[1;30m"..format:gsub(a,"\027[1;3"..b.."m%1\027[1;30m")
  127.         end
  128.     end
  129.    
  130.     do --  size format
  131.         if info.du[name] then
  132.             local form = form(info.du[name])
  133.             local size,sp,unit = form:match("([%d%.]+)(%s+)(%a+)")
  134.             filesize = (
  135.                 (" "):rep(10-form:len())..
  136.                 "\027[1;33m"..size..
  137.                 sp..("\027[1;3%sm"):format(({B=4,kB = 2,MB = 1,GB = 5})[unit])..
  138.                 unit)
  139.         else
  140.             filesize = "          "
  141.         end
  142.     end
  143.    
  144.     do --  bar format
  145.         barf = bar:gsub("1","█"):gsub("2","▐")
  146.     end
  147.    
  148.     do --  time format
  149.         local time = formtime(os.time() - date)
  150.         local number,unit = time:match("(%d+)%s(%a+)")
  151.         timemod = ("\027[1;3%sm%s%s%s"):format(
  152.             ({sec=5,min=2,hou=3,day=4,wee=7,})[unit:sub(1,3)],
  153.             number,
  154.             (" "):rep(11-time:len()),
  155.             unit)
  156.     end
  157.    
  158.     do --  name format
  159.         namef = (color == "" and "\027[39m" or color)..name.."\027[0m"
  160.     end
  161.    
  162.     io.write(" "..  format..    "\027[1;30m∣")
  163.     io.write(       timemod..   "\027[1;30m∣")
  164.     io.write(       filesize..  "\027[1;30m∣")
  165.     io.write(       namef..     (" "):rep((cols-36-name:len())-bar:len()))
  166.     io.write(colorscale(info.du[name],max)..barf)
  167.    
  168.     --date,name,info.du[name]
  169.     io.write("\n")
  170. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement