Add system resources monitor widget on screen n°

This commit is contained in:
Alice Gaudon 2019-08-11 23:06:22 +02:00
parent 635d4822b4
commit 64d7e9a7b0
4 changed files with 146 additions and 0 deletions

View File

@ -32,6 +32,9 @@ config.windowSwitcher = "rofi -show window -config " .. awful.util.getdir("confi
-- Media control -- Media control
config.volume_osd_timeout = 2 config.volume_osd_timeout = 2
-- System resources widget
config.system_resources_widget_refresh_interval = 1.5 -- In seconds
-- Awesome tweaks -- Awesome tweaks
naughty.config.defaults.timeout = 15 naughty.config.defaults.timeout = 15

View File

@ -77,6 +77,16 @@ theme.volume_osd_icon_1 = simple_awesome_path .. "/icons/volume.svg"
theme.volume_osd_icon_2 = simple_awesome_path .. "/icons/volume-1.svg" theme.volume_osd_icon_2 = simple_awesome_path .. "/icons/volume-1.svg"
theme.volume_osd_icon_3 = simple_awesome_path .. "/icons/volume-2.svg" theme.volume_osd_icon_3 = simple_awesome_path .. "/icons/volume-2.svg"
-- System resources widget
theme.system_resources_widget_bar_bg = theme.bg_focus
theme.system_resources_widget_bar_color = "#ffffff"
theme.system_resources_widget_border_color = theme.fg_normal
theme.system_resources_widget_border_width = 1
theme.system_resources_widget_bar_width = dpi(8)
theme.system_resources_widget_bar_margin = dpi(2)
theme.system_resources_widget_bar_shape = function(cr, width, height)
return gears.shape.rounded_rect (cr, width, height, 2)
end
-- There are other variable sets -- There are other variable sets
-- overriding the default one when -- overriding the default one when

5
rc.lua
View File

@ -59,6 +59,9 @@ beautiful.init(config.theme)
-- Media control -- Media control
local media_control = require("media_control") local media_control = require("media_control")
-- System Resources widget
local system_resources_widget = require("widgets/system_resources/system_resources")
-- Tiling -- Tiling
local tiling = require("tiling") local tiling = require("tiling")
@ -74,6 +77,7 @@ if config.screenshot_utility_auto_start then
awful.spawn(config.screenshot_utility_auto_start_command) awful.spawn(config.screenshot_utility_auto_start_command)
end end
-- This is used later as the default terminal and editor to run. -- This is used later as the default terminal and editor to run.
editor = os.getenv("EDITOR") or "nano" editor = os.getenv("EDITOR") or "nano"
editor_cmd = config.terminal .. " -e " .. editor editor_cmd = config.terminal .. " -e " .. editor
@ -307,6 +311,7 @@ awful.screen.connect_for_each_screen(function(s)
}, },
s.mytasklist, -- Middle widget s.mytasklist, -- Middle widget
{ -- Right widgets { -- Right widgets
s.index == 2 and system_resources_widget.widget or nil,
layout = wibox.layout.fixed.horizontal, layout = wibox.layout.fixed.horizontal,
mykeyboardlayout, mykeyboardlayout,
wibox.widget.systray(), wibox.widget.systray(),

View File

@ -0,0 +1,128 @@
local beautiful = require("beautiful")
local gears = require("gears")
local naughty = require("naughty")
local wibox = require("wibox")
local config = require("../../config")
local inspect = require("../../debug/inspect")
local bar = {
{
{
max_value = 1,
value = 0,
widget = wibox.widget.progressbar,
color = beautiful.system_resources_widget_bar_color,
background_color = beautiful.system_resources_widget_bar_bg,
shape = beautiful.system_resources_widget_bar_shape,
border_color = beautiful.system_resources_widget_border_color,
border_width = beautiful.system_resources_widget_border_width,
},
forced_width = beautiful.system_resources_widget_bar_width,
direction = "east",
layout = wibox.container.rotate,
},
margins = beautiful.system_resources_widget_bar_margin,
widget = wibox.container.margin,
}
local widget = wibox.widget {
bar,
bar,
bar,
layout = wibox.layout.flex.horizontal
}
function readCommand(command)
local handle = io.popen(command, "r")
local r = handle:read("*a")
handle:close()
return r
end
local cpuBar = widget:get_children()[1]:get_children()[1]:get_children()[1]
function getCpuData()
local raw = readCommand("cat /proc/stat | head -n 1")
local data = {raw:match((raw:gsub("[^ ]* ", "([^ ]*) ")))}
return {
user = data[3],
nice = data[4],
system = data[5],
idle = data[6],
iowait = data[7],
irq = data[8],
softirq = data[9],
steal = data[10],
}
end
local lastCpuTime = -1
local lastCpuIdle = -1
gears.timer {
timeout = config.system_resources_widget_refresh_interval,
call_now = true,
autostart = true,
callback = function()
local data = getCpuData()
local currentCpuTime = data.user + data.nice + data.system + data.idle + data.iowait + data.irq + data.softirq + data.steal
local currentCpuIdle = data.idle + data.iowait
if(lastCpuTime >= 0) then
cpuBar.value = 1 - (currentCpuIdle - lastCpuIdle) / (currentCpuTime - lastCpuTime)
end
lastCpuTime = currentCpuTime
lastCpuIdle = currentCpuIdle
end
}
local memoryBar = widget:get_children()[2]:get_children()[1]:get_children()[1]
function getTotalMemory()
return readCommand("cat /proc/meminfo | grep MemTotal | awk '{print $2}'")
end
function getAvailableMemory()
return readCommand("cat /proc/meminfo | grep MemAvailable | awk '{print $2}'")
end
function getFreeMemory()
return readCommand("cat /proc/meminfo | grep MemFree | awk '{print $2}'")
end
gears.timer {
timeout = config.system_resources_widget_refresh_interval,
call_now = true,
autostart = true,
callback = function()
memoryBar.value = 1 - getAvailableMemory() / getTotalMemory()
end
}
local swapBar = widget:get_children()[3]:get_children()[1]:get_children()[1]
function getTotalSwap()
return readCommand("cat /proc/meminfo | grep SwapTotal | awk '{print $2}'")
end
function getFreeSwap()
return readCommand("cat /proc/meminfo | grep SwapFree | awk '{print $2}'")
end
gears.timer {
timeout = config.system_resources_widget_refresh_interval,
call_now = true,
autostart = true,
callback = function()
swapBar.value = 1 - getFreeSwap() / getTotalSwap()
end
}
return {
widget = widget
}