From 64d7e9a7b0ac9b83894ca9c7aaa299e5bec0ce38 Mon Sep 17 00:00:00 2001 From: Alice Gaudon Date: Sun, 11 Aug 2019 23:06:22 +0200 Subject: [PATCH] =?UTF-8?q?Add=20system=20resources=20monitor=20widget=20o?= =?UTF-8?q?n=20screen=20n=C2=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- default_config.lua | 3 + default_theme.lua | 10 ++ rc.lua | 5 + widgets/system_resources/system_resources.lua | 128 ++++++++++++++++++ 4 files changed, 146 insertions(+) create mode 100644 widgets/system_resources/system_resources.lua diff --git a/default_config.lua b/default_config.lua index 9379fb5..ffdac34 100644 --- a/default_config.lua +++ b/default_config.lua @@ -32,6 +32,9 @@ config.windowSwitcher = "rofi -show window -config " .. awful.util.getdir("confi -- Media control config.volume_osd_timeout = 2 +-- System resources widget +config.system_resources_widget_refresh_interval = 1.5 -- In seconds + -- Awesome tweaks naughty.config.defaults.timeout = 15 diff --git a/default_theme.lua b/default_theme.lua index 24460bd..c402843 100644 --- a/default_theme.lua +++ b/default_theme.lua @@ -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_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 -- overriding the default one when diff --git a/rc.lua b/rc.lua index 6b61756..7db466b 100644 --- a/rc.lua +++ b/rc.lua @@ -59,6 +59,9 @@ beautiful.init(config.theme) -- Media control local media_control = require("media_control") +-- System Resources widget +local system_resources_widget = require("widgets/system_resources/system_resources") + -- Tiling local tiling = require("tiling") @@ -74,6 +77,7 @@ if config.screenshot_utility_auto_start then awful.spawn(config.screenshot_utility_auto_start_command) end + -- This is used later as the default terminal and editor to run. editor = os.getenv("EDITOR") or "nano" editor_cmd = config.terminal .. " -e " .. editor @@ -307,6 +311,7 @@ awful.screen.connect_for_each_screen(function(s) }, s.mytasklist, -- Middle widget { -- Right widgets + s.index == 2 and system_resources_widget.widget or nil, layout = wibox.layout.fixed.horizontal, mykeyboardlayout, wibox.widget.systray(), diff --git a/widgets/system_resources/system_resources.lua b/widgets/system_resources/system_resources.lua new file mode 100644 index 0000000..576faa6 --- /dev/null +++ b/widgets/system_resources/system_resources.lua @@ -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 +} \ No newline at end of file