NASutils Knowledge BaseUnofficialPolski

BASIC · projectWhere did the space go? A bar chart in yabasic

This program answers the question a full volume asks: which folder takes the space? It runs du on a folder you name, sorts the folders one level down from the largest and draws a bar for each, with its size and its share of the total.

Programming on the NAS › BASIC in 30 lessons

 ╔═ Where did the space go ═══════════════════════════ /share/CACHEDEV1_DATA ═╗
 ║ Multimedia     ██████████████████████████████████████   134.7 GiB   47.9 % ║
 ║ Download       ██████████████████▌                       65.7 GiB   23.3 % ║
 ║ Backup         ██████████▌                               37.2 GiB   13.2 % ║
 ║ homes          █████                                     17.4 GiB    6.2 % ║
 ║ Container      ███▌                                      12.3 GiB    4.4 % ║
 ║ .qpkg          ██                                         7.3 GiB    2.6 % ║
 ║ Public         █                                          3.9 GiB    1.4 % ║
 ║ Web            ▌                                          1.8 GiB    0.6 % ║
 ║ @Recycle       ▌                                          1.2 GiB    0.4 % ║
 ╟────────────────────────────────────────────────────────────────────────────╢
 ║ Total          9 folders                                281.5 GiB  100.0 % ║
 ╚════════════════════════════════════════════════════════════════════════════╝
The sample: Multimedia takes 134.7 GiB, 47.9 % of 281.5 GiB, and eight more folders follow down to @Recycle with 1.2 GiB.

Download it on the NAS

curl -fsSLO https://kb.nasutils.net/projects/basic/bars.yab

bars.yab Download bars.yab

rem Where did the space go: the folders of one level, largest first, as bars with their sizes and percent.
rem Run: yabasic bars.yab /share/CACHEDEV1_DATA   (du measures the folder; that can take a while)
rem      yabasic bars.yab                         (reads the sample folder-sizes.txt, made by du -k -d 1)

folder$ = peek$("argument")
if folder$ = "" then
  if not open(#1, "folder-sizes.txt", "r") then print "folder-sizes.txt is missing. Give a folder to measure." : exit 1 : endif
  while not eof(#1) : line input #1 a$ : list$ = list$ + a$ + chr$(10) : wend
  close #1
else
  if len(folder$) > 1 and right$(folder$, 1) = "/" folder$ = left$(folder$, len(folder$) - 1)
  list$ = system$("du -k -d 1 '" + folder$ + "' 2>/dev/null")
endif

rem du writes "size<TAB>path" a line, the folder itself last: that line is the total
dim line$(1), f$(2), size(100), name$(100)
n = token(list$, line$(), chr$(10))
if n < 2 then print "du found no folders in ", folder$ : exit 1 : endif
m = token(line$(n), f$(), chr$(9)) : total = val(f$(1)) : top$ = f$(2)
for i = 1 to n - 1
  if token(line$(i), f$(), chr$(9)) = 2 and count < 100 then
    count = count + 1 : size(count) = val(f$(1)) : name$(count) = mid$(f$(2), len(top$) + 2)
  endif
next i

rem Largest first (insertion sort: each folder moves up past the smaller ones)
for i = 2 to count
  s = size(i) : s$ = name$(i) : j = i - 1
  while j >= 1 and size(j) < s
    size(j + 1) = size(j) : name$(j + 1) = name$(j) : j = j - 1
  wend
  size(j + 1) = s : name$(j + 1) = s$
next i

print
if len(top$) > 40 top$ = "..." + right$(top$, 37)
title$ = " Where did the space go "
print e$("33") + " ╔═" + e$("1") + title$ + e$("0;33") + rep$("═", 72 - width(title$) - width(top$)) + e$("1") + " " + top$ + " " + e$("0;33") + "═╗"
for i = 1 to min(count, 18)
  c$ = "33" : if i = 1 c$ = "1;33"
  print e$("33") + " ║ " + e$("0") + pad$(name$(i), 15) + e$(c$) + bar$(size(i) / size(1), 38) + e$("0") + size$(size(i)) + str$(size(i) / total * 100, "%7.1f") + " %" + e$("33") + " ║"
next i
print e$("33") + " ╟" + rep$("─", 76) + "╢"
print " ║ " + e$("1") + pad$("Total", 15) + e$("0") + pad$(str$(count) + " folders", 38) + size$(total) + "  100.0 %" + e$("33") + " ║"
print " ╚" + rep$("═", 76) + "╝" + e$("0")
print

rem A bar in half steps: full blocks, then a half block when the rest is at least half a cell
sub bar$(part, cells)
  local halves, s$
  halves = int(part * cells * 2 + 0.5)
  s$ = rep$("█", int(halves / 2))
  if mod(halves, 2) s$ = s$ + "▌"
  return s$ + rep$(" ", cells - width(s$))
end sub

rem A size in KiB as text, in the largest unit that keeps it at 1 or more
sub size$(kb)
  local u$, i
  u$ = "KiBMiBGiBTiB" : i = 1
  while kb >= 1024 and i < 4 : kb = kb / 1024 : i = i + 1 : wend
  return str$(kb, "%8.1f") + " " + mid$(u$, i * 3 - 2, 3)
end sub

sub width(s$)
  local i, n, c
  for i = 1 to len(s$)
    c = asc(mid$(s$, i, 1))
    if c < 128 or c >= 192 n = n + 1
  next i
  return n
end sub

sub pad$(s$, n) : return s$ + rep$(" ", n - width(s$)) : end sub
sub rep$(s$, n) : local r$, i : for i = 1 to n : r$ = r$ + s$ : next i : return r$ : end sub
sub e$(code$) : return chr$(27) + "[" + code$ + "m" : end sub

What you learn

  • system$("du -k -d 1 '...'") runs a command and returns all its output as one string.
  • token(line$, f$(), chr$(9)) splits a line of du at the tab, so a folder name with spaces stays whole.
  • Insertion sort moves each folder up past the smaller ones and keeps its name and its size together.
  • A bar in half steps: ▌ stands for the last half cell, so small folders still show a mark.

How it works

With a folder as argument, the program runs du -k -d 1 on it: one line per folder one level down, then the folder itself, each as "size in KiB, tab, path". Without an argument, it reads the same lines from folder-sizes.txt, a sample made from a data volume of a NAS.

The last line is the total. The program keeps the other lines in two arrays, size and name$, and cuts the path of the parent folder off each name. Insertion sort then puts the largest folder first.

Each bar is drawn against the largest folder, so the first one fills all 38 cells. The size beside it comes from size$ in the largest unit that keeps the number at 1 or more, and the percentage is the share of the total. A last line under a divider gives the total and the count of folders.

Notes to the lines

  • 5peek$("argument") returns the first word after the program's name, or "" without one.
  • 7open() returns 0 when the file is missing; the program says so and ends with code 1.
  • 11–12A slash at the end of the folder is cut off, so the paths of du match the parent's. 2>/dev/null hides the folders that du cannot read.
  • 19The last line of du is the folder itself: its size is the total and its path is cut off the other names.
  • 22mid$(f$(2), len(top$) + 2) leaves only the name after "parent path/".
  • 27–33Insertion sort: the folder i waits in s and s$ while the larger ones above it move down one place.
  • 36A long parent path keeps its last 37 characters, so the title still fits in the top edge.
  • 40The largest folder gets bold amber, the others plain amber.
  • 41Name, bar, size and percentage in one line; the bar shows the size against the largest folder.
  • 51–53The number of half cells, rounded: full blocks for each pair and ▌ for an odd one left over.

Run it

yabasic bars.yab /share/CACHEDEV1_DATA

du reads every file under the folder, so a large volume takes a while. yabasic bars.yab without a folder draws the sample from folder-sizes.txt.

Try it yourself

Draw the empty part of each bar with ░ instead of spaces on line 54. Show only the five largest folders by changing the 18 on line 39 to 5.

More projects in BASIC