fix fs widget notification alignment

This commit is contained in:
Neville Li 2020-05-23 09:17:15 -04:00 committed by Luca Capezzuto
parent 0775f97b42
commit e0bffc0056
1 changed files with 10 additions and 17 deletions

View File

@ -69,11 +69,10 @@ local function factory(args)
end end
function fs.update() function fs.update()
local notifytable = { [1] = string.format("%-10s %4s\t%6s\t%6s\t\n", "path", "used", "free", "size") }
local pathlen = 10 local pathlen = 10
local maxpathidx = 1
fs_now = {} fs_now = {}
local notifypaths = {}
for _, mount in ipairs(Gio.unix_mounts_get()) do for _, mount in ipairs(Gio.unix_mounts_get()) do
local path = Gio.unix_mount_get_mount_path(mount) local path = Gio.unix_mount_get_mount_path(mount)
local root = Gio.File.new_for_path(path) local root = Gio.File.new_for_path(path)
@ -90,19 +89,16 @@ local function factory(args)
fs_now[path] = { fs_now[path] = {
units = fs.units[units], units = fs.units[units],
percentage = math.floor(100 * used / size), -- used percentage percentage = math.floor(100 * used / size), -- used percentage
size = size / math.pow(1024, math.floor(units)), size = size / math.pow(1024, units),
used = used / math.pow(1024, math.floor(units)), used = used / math.pow(1024, units),
free = free / math.pow(1024, math.floor(units)) free = free / math.pow(1024, units)
} }
if fs_now[path].percentage > 0 then -- don't notify unused file systems if fs_now[path].percentage > 0 then -- don't notify unused file systems
notifytable[#notifytable+1] = string.format("\n%-10s %3s%%\t%6.2f\t%6.2f\t%s", path, notifypaths[#notifypaths+1] = path
math.floor(fs_now[path].percentage), fs_now[path].free, fs_now[path].size,
fs_now[path].units)
if #path > pathlen then if #path > pathlen then
pathlen = #path pathlen = #path
maxpathidx = #notifytable
end end
end end
end end
@ -125,14 +121,11 @@ local function factory(args)
end end
end end
if pathlen > 10 then -- if are there paths longer than 10 chars, reformat first column accordingly local fmt = "%-" .. tostring(pathlen) .. "s %4s\t%6s\t%6s\n"
local pathspaces local notifytable = { [1] = string.format(fmt, "path", "used", "free", "size") }
for i = 1, #notifytable do fmt = "\n%-" .. tostring(pathlen) .. "s %3s%%\t%6.2f\t%6.2f %s"
pathspaces = notifytable[i]:match("[ ]+") for _, path in ipairs(notifypaths) do
if i ~= maxpathidx and pathspaces then notifytable[#notifytable+1] = string.format(fmt, path, fs_now[path].percentage, fs_now[path].free, fs_now[path].size, fs_now[path].units)
notifytable[i] = notifytable[i]:gsub(pathspaces, pathspaces .. string.rep(" ", pathlen - 10))
end
end
end end
fs.notification_preset.text = tconcat(notifytable) fs.notification_preset.text = tconcat(notifytable)