awesome-wm-widgets/ram-widget/ram-widget.lua

109 lines
3.4 KiB
Lua
Raw Normal View History

2017-12-11 23:08:55 +01:00
local awful = require("awful")
local beautiful = require("beautiful")
local gears = require("gears")
2017-12-11 23:08:55 +01:00
local watch = require("awful.widget.watch")
local wibox = require("wibox")
2019-09-02 21:37:45 +02:00
local ramgraph_widget = {}
2017-12-11 23:08:55 +01:00
2020-12-06 02:57:04 +01:00
local function worker(user_args)
local args = user_args or {}
2020-09-19 10:08:15 +02:00
local timeout = args.timeout or 1
2021-04-11 20:30:30 +02:00
local color_used = args.color_used or beautiful.bg_urgent
local color_free = args.color_free or beautiful.fg_normal
local color_buf = args.color_buf or beautiful.border_color_active
local widget_show_buf = args.widget_show_buf or false
local widget_height = args.widget_height or 25
local widget_width = args.widget_width or 25
2017-12-11 23:08:55 +01:00
2019-09-02 21:37:45 +02:00
--- Main ram widget shown on wibar
ramgraph_widget = wibox.widget {
border_width = 0,
colors = {
2021-04-11 20:30:30 +02:00
color_used,
color_free,
color_buf,
2019-09-02 21:37:45 +02:00
},
display_labels = false,
forced_height = widget_height,
forced_width = widget_width,
2019-09-02 21:37:45 +02:00
widget = wibox.widget.piechart
}
2017-12-27 19:26:58 +01:00
2019-09-02 21:37:45 +02:00
--- Widget which is shown when user clicks on the ram widget
local popup = awful.popup{
2020-11-30 22:51:47 +01:00
ontop = true,
visible = false,
widget = {
widget = wibox.widget.piechart,
forced_height = 200,
forced_width = 400,
colors = {
2021-04-11 20:30:30 +02:00
color_used,
color_free,
color_buf, -- buf_cache
},
},
shape = gears.shape.rounded_rect,
border_color = beautiful.border_color_active,
border_width = 1,
offset = { y = 5 },
2019-09-02 21:37:45 +02:00
}
2020-12-06 02:57:04 +01:00
--luacheck:ignore 231
2019-09-02 21:37:45 +02:00
local total, used, free, shared, buff_cache, available, total_swap, used_swap, free_swap
local function getPercentage(value)
return math.floor(value / (total+total_swap) * 100 + 0.5) .. '%'
end
2017-12-15 03:05:43 +01:00
2020-09-19 10:08:15 +02:00
watch('bash -c "LANGUAGE=en_US.UTF-8 free | grep -z Mem.*Swap.*"', timeout,
2020-12-06 02:57:04 +01:00
function(widget, stdout)
2019-09-02 21:37:45 +02:00
total, used, free, shared, buff_cache, available, total_swap, used_swap, free_swap =
stdout:match('(%d+)%s*(%d+)%s*(%d+)%s*(%d+)%s*(%d+)%s*(%d+)%s*Swap:%s*(%d+)%s*(%d+)%s*(%d+)')
2018-04-15 20:21:46 +02:00
2021-04-11 20:30:30 +02:00
if widget_show_buf then
widget.data = { used, free, buff_cache }
else
widget.data = { used, total-used }
end
2017-12-27 19:26:58 +01:00
if popup.visible then
popup:get_widget().data_list = {
{'used ' .. getPercentage(used + used_swap), used + used_swap},
{'free ' .. getPercentage(free + free_swap), free + free_swap},
{'buff_cache ' .. getPercentage(buff_cache), buff_cache}
2019-09-02 21:37:45 +02:00
}
end
end,
ramgraph_widget
)
2017-12-27 19:26:58 +01:00
2019-09-02 21:37:45 +02:00
ramgraph_widget:buttons(
awful.util.table.join(
awful.button({}, 1, function()
popup:get_widget().data_list = {
2019-09-02 21:37:45 +02:00
{'used ' .. getPercentage(used + used_swap), used + used_swap},
{'free ' .. getPercentage(free + free_swap), free + free_swap},
{'buff_cache ' .. getPercentage(buff_cache), buff_cache}
}
2020-12-06 02:57:04 +01:00
if popup.visible then
popup.visible = not popup.visible
else
popup:move_next_to(mouse.current_widget_geometry)
end
2019-09-02 21:37:45 +02:00
end)
)
2017-12-11 23:08:55 +01:00
)
2019-09-02 21:37:45 +02:00
return ramgraph_widget
end
2019-09-02 21:37:45 +02:00
return setmetatable(ramgraph_widget, { __call = function(_, ...)
return worker(...)
end })