Add battery percentage widget

This commit is contained in:
Alice Gaudon 2019-10-07 12:13:11 +02:00
parent b8aff62b38
commit 3c6e2d3843
5 changed files with 87 additions and 21 deletions

View File

@ -9,6 +9,7 @@ By default, Simple Awesome uses
- `rofi` - launch bar / window switcher - `rofi` - launch bar / window switcher
- `flameshot` - screenshot utility - `flameshot` - screenshot utility
- `alsa-utils` - amixer command provider (media keys volume control)
## Optional dependencies ## Optional dependencies
@ -18,7 +19,7 @@ By default, Simple Awesome uses
## To do ## To do
- Add a sound level control widget - Add a sound volume control widget
- Add a media control widget - Add a media control widget
- Manage workspaces - Manage workspaces
- Improve "leave menu"'s design - Improve "leave menu"'s design

View File

@ -131,7 +131,17 @@ config.widgets = {
-- Enable/disable the system resources monitoring widget -- Enable/disable the system resources monitoring widget
system_resources = { system_resources = {
bars = {
enabled = true, enabled = true,
screens = { 1, 2 },
refresh_interval = 1.5, -- In seconds
},
battery = {
enabled = true,
screens = { 1, 2 },
refresh_interval = 30, -- In seconds
precision = 0, -- How many decimals
},
}, },
} }
@ -164,10 +174,6 @@ 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_screens = { 1, 2 }
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

@ -68,6 +68,7 @@ theme.system_resources_widget_bar_margin = dpi(2)
theme.system_resources_widget_bar_shape = function(cr, width, height) theme.system_resources_widget_bar_shape = function(cr, width, height)
return gears.shape.rounded_rect (cr, width, height, 2) return gears.shape.rounded_rect (cr, width, height, 2)
end end
theme.system_resources_widget_battery_margin = theme.system_resources_widget_bar_width
-- There are other variable sets -- There are other variable sets
-- overriding the default one when -- overriding the default one when

View File

@ -110,10 +110,7 @@ hotkey.registerKeys(media_control.getKeys(config.keys.global_keys.media_control)
-- --
-- System resources -- System resources
local system_resources_widget local system_resources_widget = require("simple/widgets/system_resources")
if config.widgets.system_resources.enabled then
system_resources_widget = require("simple/widgets/system_resources")
end
-- Keyboard map indicator and switcher -- Keyboard map indicator and switcher
local keyboard_layout = require("simple/widgets/keyboard_layout") local keyboard_layout = require("simple/widgets/keyboard_layout")
@ -322,7 +319,8 @@ awful.screen.connect_for_each_screen(function(s)
{ {
-- Right widgets -- Right widgets
layout = wibox.layout.fixed.horizontal, layout = wibox.layout.fixed.horizontal,
matchesScreen(config.system_resources_widget_screens) and system_resources_widget.widget or nil, matchesScreen(config.widgets.system_resources.bars.screens) and system_resources_widget.bars or nil,
matchesScreen(config.widgets.system_resources.battery.screens) and system_resources_widget.battery or nil,
keyboard_layout_widget, keyboard_layout_widget,
wibox.widget.systray(), wibox.widget.systray(),
mytextclock, mytextclock,

View File

@ -3,7 +3,7 @@ local gears = require("gears")
local naughty = require("naughty") local naughty = require("naughty")
local wibox = require("wibox") local wibox = require("wibox")
local config = require("config") local config = require("config").widgets.system_resources
local inspect = require("simple/debug/inspect") local inspect = require("simple/debug/inspect")
@ -27,14 +27,13 @@ local bar = {
widget = wibox.container.margin, widget = wibox.container.margin,
} }
local widget = wibox.widget { local bars = wibox.widget {
bar, bar,
bar, bar,
bar, bar,
layout = wibox.layout.flex.horizontal layout = wibox.layout.flex.horizontal
} }
function readCommand(command) function readCommand(command)
local handle = io.popen(command, "r") local handle = io.popen(command, "r")
local r = handle:read("*a") local r = handle:read("*a")
@ -43,7 +42,7 @@ function readCommand(command)
end end
local cpuBar = widget:get_children()[1]:get_children()[1]:get_children()[1] local cpuBar = bars:get_children()[1]:get_children()[1]:get_children()[1]
function getCpuData() function getCpuData()
local raw = readCommand("cat /proc/stat | head -n 1") local raw = readCommand("cat /proc/stat | head -n 1")
local data = {raw:match((raw:gsub("[^ ]* ", "([^ ]*) ")))} local data = {raw:match((raw:gsub("[^ ]* ", "([^ ]*) ")))}
@ -63,7 +62,7 @@ local lastCpuTime = -1
local lastCpuIdle = -1 local lastCpuIdle = -1
gears.timer { gears.timer {
timeout = config.system_resources_widget_refresh_interval, timeout = config.bars.refresh_interval,
call_now = true, call_now = true,
autostart = true, autostart = true,
callback = function() callback = function()
@ -83,7 +82,7 @@ gears.timer {
} }
local memoryBar = widget:get_children()[2]:get_children()[1]:get_children()[1] local memoryBar = bars:get_children()[2]:get_children()[1]:get_children()[1]
function getTotalMemory() function getTotalMemory()
return readCommand("cat /proc/meminfo | grep MemTotal | awk '{print $2}'") return readCommand("cat /proc/meminfo | grep MemTotal | awk '{print $2}'")
end end
@ -95,7 +94,7 @@ function getFreeMemory()
end end
gears.timer { gears.timer {
timeout = config.system_resources_widget_refresh_interval, timeout = config.bars.refresh_interval,
call_now = true, call_now = true,
autostart = true, autostart = true,
callback = function() callback = function()
@ -104,7 +103,7 @@ gears.timer {
} }
local swapBar = widget:get_children()[3]:get_children()[1]:get_children()[1] local swapBar = bars:get_children()[3]:get_children()[1]:get_children()[1]
function getTotalSwap() function getTotalSwap()
return readCommand("cat /proc/meminfo | grep SwapTotal | awk '{print $2}'") return readCommand("cat /proc/meminfo | grep SwapTotal | awk '{print $2}'")
end end
@ -113,7 +112,7 @@ function getFreeSwap()
end end
gears.timer { gears.timer {
timeout = config.system_resources_widget_refresh_interval, timeout = config.bars.refresh_interval,
call_now = true, call_now = true,
autostart = true, autostart = true,
callback = function() callback = function()
@ -123,6 +122,67 @@ gears.timer {
return {
widget = widget local battery = wibox.widget {
{
markup = "-- %",
align = "center",
valign = "center",
widget = wibox.widget.textbox,
},
margins = beautiful.system_resources_widget_battery_margin,
widget = wibox.container.margin,
}
local batteryText = battery:get_children()[1]
local rawBatteries = readCommand("ls -A1 /sys/class/power_supply")
function getBatteries()
return rawBatteries:gmatch("([^\n]+)")
end
function hasBattery()
return rawBatteries:len() > 0
end
function getBatteryCapacity()
local charge = 0
local capacity = 0
for b in getBatteries() do
if readCommand("cat /sys/class/power_supply/" .. b .. "/type"):match("Battery") then
charge = charge + readCommand("cat /sys/class/power_supply/" .. b .. "/energy_now")
capacity = capacity + readCommand("cat /sys/class/power_supply/" .. b .. "/energy_full")
end
end
return charge / capacity
end
function isCharging()
for b in getBatteries() do
if readCommand("cat /sys/class/power_supply/" .. b .. "/type"):match("Battery") then
return readCommand("cat /sys/class/power_supply/" .. b .. "/status") ~= "Discharging"
end
end
return nil
end
if hasBattery() then
gears.timer {
timeout = config.battery.refresh_interval,
call_now = true,
autostart = true,
callback = function()
batteryText.markup = string.format("%." .. config.battery.precision .. "f", getBatteryCapacity() * 100) .. "%"
end
}
else
battery = nil
end
return {
bars = bars,
battery = battery,
} }