Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[batteryarc] Add support for reading multiple batteries in one batteryarc instance #419

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions batteryarc-widget/batteryarc.lua
Original file line number Diff line number Diff line change
Expand Up @@ -88,18 +88,30 @@ local function worker(user_args)

local function update_widget(widget, stdout)
local charge = 0
local status
local batteries = 0
local status = 'Unknown'
for s in stdout:gmatch("[^\r\n]+") do
local cur_status, charge_str, _ = string.match(s, '.+: ([%a%s]+), (%d?%d?%d)%%,?(.*)')
if cur_status ~= nil and charge_str ~=nil then
local cur_charge = tonumber(charge_str)
if cur_charge > charge then
status = cur_status
charge = cur_charge
if cur_status ~= 'Unknown' then
if status == 'Charging' or cur_status == 'Charging' then
status = 'Charging'
else
status = cur_status
end
charge = charge + cur_charge
batteries = batteries + 1
end
end
end

if batteries > 0 then
charge = charge // batteries
else
charge = 0
end

widget.value = charge

if status == 'Charging' then
Expand Down
14 changes: 13 additions & 1 deletion mpdarc-widget/mpdarc.lua
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ local mpdarc_icon_widget = wibox.container.mirror(mpdarc, { horizontal = true })
local mpdarc_current_song_widget = wibox.widget {
id = 'current_song',
widget = wibox.widget.textbox,
font = 'Play 9'
font = beautiful.font or 'Play 9'
}

local update_graphic = function(widget, stdout, _, _, _)
Expand Down Expand Up @@ -87,13 +87,20 @@ mpdarc:connect_signal("button::press", function(_, _, _, button)
elseif (button == 5) then awful.spawn(PREV_MPD_CMD, false) -- scroll down
end

local reset_garbage_collector = 0
spawn.easy_async(GET_MPD_CMD, function(stdout, stderr, exitreason, exitcode)
update_graphic(mpdarc, stdout, stderr, exitreason, exitcode)
reset_garbage_collector = reset_garbage_collector + 1
if (reset_garbage_collector > 10) then
collectgarbage()
reset_garbage_collector = 0
end
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you explain how does this work?

end)
end)

local notification
local function show_MPD_status()
local reset_garbage_collector = 0
spawn.easy_async(GET_MPD_CMD,
function(stdout, _, _, _)
notification = naughty.notify {
Expand All @@ -103,6 +110,11 @@ local function show_MPD_status()
hover_timeout = 0.5,
width = 600,
}
reset_garbage_collector = reset_garbage_collector + 1
if (reset_garbage_collector > 10) then
collectgarbage()
reset_garbage_collector = 0
end
end)
end

Expand Down