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

79 lines
2.2 KiB
Lua
Raw Normal View History

2017-12-11 23:08:55 +01:00
local awful = require("awful")
local watch = require("awful.widget.watch")
local wibox = require("wibox")
2017-12-27 19:26:58 +01:00
--- Main ram widget shown on wibar
2017-12-11 23:08:55 +01:00
local ramgraph_widget = wibox.widget {
border_width = 0,
colors = {
'#74aeab', '#26403f'
},
display_labels = false,
forced_width = 25,
widget = wibox.widget.piechart
}
2017-12-27 19:26:58 +01:00
--- Widget which is shown when user clicks on the ram widget
2017-12-11 23:08:55 +01:00
local w = wibox {
height = 200,
2017-12-27 19:26:58 +01:00
width = 400,
2017-12-11 23:08:55 +01:00
ontop = true,
expand = true,
bg = '#1e252c',
max_widget_size = 500
}
w:setup {
2017-12-27 19:26:58 +01:00
border_width = 0,
colors = {
'#5ea19d',
'#55918e',
'#4b817e',
2017-12-15 03:05:43 +01:00
},
2017-12-27 19:26:58 +01:00
display_labels = false,
forced_width = 25,
id = 'pie',
widget = wibox.widget.piechart
2017-12-11 23:08:55 +01:00
}
2018-04-13 15:40:10 +02:00
local total, used, free, shared, buff_cache, available, total_swap, used_swap, free_swap
2017-12-27 19:26:58 +01:00
2017-12-15 03:05:43 +01:00
local function getPercentage(value)
2018-04-13 15:40:10 +02:00
return math.floor(value / (total+total_swap) * 100 + 0.5) .. '%'
2017-12-15 03:05:43 +01:00
end
2018-04-13 15:40:10 +02:00
watch('bash -c "free | grep -z Mem.*Swap.*"', 1,
2017-12-27 19:26:58 +01:00
function(widget, stdout, stderr, exitreason, exitcode)
2018-04-15 20:21:46 +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+)')
widget.data = { used, total-used } widget.data = { used, total-used }
2017-12-27 19:26:58 +01:00
if w.visible then
w.pie.data_list = {
2018-04-15 20:21:46 +02:00
{'used ' .. getPercentage(used + used_swap), used + used_swap},
{'free ' .. getPercentage(free + free_swap), free + free_swap},
2017-12-27 19:26:58 +01:00
{'buff_cache ' .. getPercentage(buff_cache), buff_cache}
}
end
end,
ramgraph_widget
)
2017-12-11 23:08:55 +01:00
ramgraph_widget:buttons(
awful.util.table.join(
awful.button({}, 1, function()
2018-11-05 17:18:12 +01:00
awful.placement.top_right(w, { margins = {top = 25, right = 10}, parent = awful.screen.focused() })
2017-12-27 19:26:58 +01:00
w.pie.data_list = {
2018-04-15 20:21:46 +02:00
{'used ' .. getPercentage(used + used_swap), used + used_swap},
{'free ' .. getPercentage(free + free_swap), free + free_swap},
2017-12-27 19:26:58 +01:00
{'buff_cache ' .. getPercentage(buff_cache), buff_cache}
2017-12-11 23:08:55 +01:00
}
2017-12-27 19:26:58 +01:00
w.pie.display_labels = true
w.visible = not w.visible
2017-12-11 23:08:55 +01:00
end)
)
)
2018-01-21 03:58:21 +01:00
return ramgraph_widget