awesome-memwidget/init.lua

95 lines
2.4 KiB
Lua

local longpress = require("awesome-longpress")
local wibox = require("wibox")
local gears = require("gears")
local beautiful = require ("beautiful")
local rw = {
iconSize = 96,
iconPath = gears.filesystem.get_configuration_dir() .. "/awesome-memwidget/"
}
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
function rw.new()
local stack = wibox.widget{
layout = wibox.layout.stack,
}
local bgimage = wibox.widget{
widget = wibox.widget.imagebox,
forced_width = rw.iconSize,
forced_height = rw.iconSize,
image = rw.iconPath .. "/media-memory.svg"
}
local rwidget = wibox.widget{
layout = wibox.layout.grid.horizontal,
wibox.container.constraint(stack,nil,rw.iconSize,rw.iconSize)
}
local memtext = wibox.widget{
align = 'center',
valign = 'bottom',
widget = wibox.widget.textbox
}
local memused = wibox.widget{
align = 'left',
valign = 'bottom',
visible = false,
widget = wibox.widget.textbox
}
local memfree = wibox.widget{
align = 'right',
valign = 'bottom',
visible = false,
widget = wibox.widget.textbox
}
local membar = wibox.widget{
widget = wibox.widget.progressbar,
max_value = 1,
border_width = 1,
border_color = beautiful.border_color,
color = beautiful.bg_focus,
background_color = beautiful.bg_normal,
forced_height = rw.iconSize / 6
}
function rwidget.update()
local f = io.open("/proc/meminfo","rb")
if not f then return true end
local c = f:read("*a")
local total = tonumber(c:match("MemTotal:%s+(%d+)"))
local free = tonumber(c:match("MemFree:%s+(%d+)"))
local buffers = tonumber(c:match("Buffers:%s+(%d+)"))
local cache = tonumber(c:match("Cached:%s+(%d+)"))
local totalUsed = total - free
local used = totalUsed - (buffers + cache)
memtext.text = (string.format("%3.0f%%", (used/total)*100))
memused.text = formatSize(used*1024)
memfree.text = formatSize((total-used)*1024)
membar.value = (used/total)
return true
end
stack:add(bgimage)
stack:add(wibox.container.place(membar,"center","bottom"))
stack:add(memused)
stack:add(memfree)
stack:add(memtext)
rwidget.update()
longpress.add(rwidget,function()
memtext.visible = not memtext.visible
memused.visible = not memused.visible
memfree.visible = not memfree.visible
end)
return rwidget
end
function rw.all()
return {rw.new()}
end
return rw