awesome-fswidget/init.lua

127 lines
3.4 KiB
Lua

local longpress = require "awesome-longpress"
local beautiful = require "beautiful"
local posix = require "posix"
local wibox = require "wibox"
local gears = require "gears"
local fswidget = {
mounts = {},
ignoreMounts = {["/boot"]=true},
ignoreDevs = {},
iconSize = 96,
iconPath = gears.filesystem.get_configuration_dir() .. "/awesome-fswidget/"
}
local fstypes = {}
local f = io.open("/proc/filesystems")
for line in f:lines() do
if not line:match("nodev") then
fstypes[line:match("%S+")] = true
end
end
local function getMountedFilesystems()
local f = io.open("/proc/mounts")
for line in f:lines() do
local dev,mp,fst,opt = line:match("(%S+)%s+(%S+)%s+(%S+)%s+(.+)")
if fstypes[fst] and not fswidget.ignoreMounts[mp] and not fswidget.ignoreDevs[dev] then
fswidget.mounts[dev] = fswidget.mounts[dev] or {}
fswidget.mounts[dev][#fswidget.mounts[dev]+1] = mp
fswidget.mounts[dev].device = dev
if opt:match("ssd") then
fswidget.mounts[dev].ssd = true
end
end
end
end
local function chooseIcon(mount)
local icon = "drive-harddisk"
if mount.ssd then
icon = "drive-harddisk-solidstate"
end
if mount.device:match("mmcblk") then
icon = "media-memory-mmc"
end
return icon
end
local sizeTable = {[0] = "B", "K","M","G","T","P"}
local function formatSize(n)
local i = 0
while n > 1024 do
n = n / 1024
i = i + 1
end
return string.format("%0.1f%s",n,sizeTable[i])
end
local function updateWidget(self)
local stat = posix.statvfs(self.mountpoint)
if stat then
local free, used, fraction = stat.bfree * stat.bsize, (stat.blocks - stat.bfree) * stat.bsize, (stat.blocks - stat.bfree) / stat.blocks
self.fsbar.value = fraction
self.fspercent.text = string.format("%.1f%%",fraction*100)
self.fsused.text = formatSize(used)
self.fsfree.text = formatSize(free)
end
end
function fswidget.new(mount)
local fs = mount[1]
local fsbar = wibox.widget{
widget = wibox.widget.progressbar,
max_value = 1,
forced_height = fswidget.iconSize / 6,
border_color = beautiful.fg_focus,
color = beautiful.bg_focus,
background_color = beautiful.bg_normal,
}
local fspercent = wibox.widget{
widget = wibox.widget.textbox,
}
local fsused = wibox.widget{
widget = wibox.widget.textbox,
visible = false
}
local fsfree = wibox.widget{
widget = wibox.widget.textbox,
visible = false
}
local w = wibox.widget{
layout = wibox.layout.stack,
wibox.widget{
widget = wibox.widget.imagebox,
forced_width = fswidget.iconSize,
forced_height = fswidget.iconSize,
image = string.format("%s/%s.svg", fswidget.iconPath, chooseIcon(mount))
},
wibox.container.place(wibox.widget{
widget = wibox.widget.textbox,
text = fs
},"center","top"),
wibox.container.place(fsbar,"center","bottom"),
wibox.container.place(fspercent,"center","bottom"),
wibox.container.place(fsused,"left","bottom"),
wibox.container.place(fsfree,"right","bottom")
}
local c = wibox.container.constraint(w,nil,fswidget.iconSize,fswidget.iconSize)
c.update, c.mountpoint, c.fsbar, c.fspercent, c.fsused, c.fsfree = updateWidget, fs, fsbar, fspercent, fsused, fsfree
longpress.add(c, function()
fspercent.visible = not fspercent.visible
fsused.visible = not fsused.visible
fsfree.visible = not fsfree.visible
end)
c:update()
return c
end
function fswidget.all()
local r = {}
getMountedFilesystems()
for k,v in pairs(fswidget.mounts) do
r[#r+1] = fswidget.new(v)
end
return r
end
return fswidget