Skip to content

Commit

Permalink
Add auto suspend to battery widget
Browse files Browse the repository at this point in the history
  • Loading branch information
ojv committed Mar 8, 2017
1 parent 2cf683b commit 347549c
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 47 deletions.
9 changes: 4 additions & 5 deletions init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ local widget = { -- for the widget that awesome draws
}
local updater = {} -- for updating the widget
local window = {} -- for conky's own window
local public = {} -- public interface
local public = { config = {} } -- public interface


-- default conky client properties
Expand Down Expand Up @@ -225,7 +225,8 @@ end

function widget.maybe_require(t_or_str) -- {{{2
if type(t_or_str) == "string" then
t_or_str = require("conky/widgets/" .. t_or_str)
local settings = public.config[t_or_str] or {}
t_or_str = require("conky/widgets/" .. t_or_str)(settings)
end
return t_or_str
end
Expand Down Expand Up @@ -366,6 +367,4 @@ function updater.add_string(conkystr) -- {{{2
end
end

-- RETURN PUBLIC INTERFACE --- {{{1

return public
return public --- {{{1
117 changes: 75 additions & 42 deletions widgets/battery.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
local beautiful = require("beautiful")
local naughty = require("naughty")
local awful = require("awful")

local settings = {
suspend_time = 300,
suspend_cmd = "sudo /usr/sbin/pm-suspend-hybrid",
}

local function htime(seconds)
local hours = math.floor(tonumber(seconds) / 3600)
Expand All @@ -9,50 +16,76 @@ local function round(perc)
return string.format("%03d", math.floor(string.sub(perc, 1, -2) / 20) * 20)
end

local suspend_initiated = false

local function suspend()
if suspend_initiated then
awful.spawn(settings.suspend_cmd)
end
end

local function maybe_suspend(time_left)
if suspend_initiated or time_left > settings.suspend_time then
return
end

suspend_initiated = true
naughty.notify({
title = "Battery Empty!",
text = "Suspending to disk in 1 minute.\n\n" ..
"Attach power to cancel.\n\n" ..
"Close this notification to suspend immediately",
timeout = 60,
destroy = suspend
})
end

local current_icon = nil

return {
icon = beautiful["icon-battery-caution"],
conky = "${battery_short} ${battery_time}",
conkybox = { forced_width = 30, align = "center" },
updater = function(update, textbox, iconbox, _)

local iter = string.gmatch(update, "%S+")
local state, perc, seconds = iter(), iter(), iter()

-- print("u:", update)
-- print("s:", state)
-- print("%:", perc)
-- print("sec:", seconds)

local icon = "icon-battery-"
local vis = false
local time = nil

if state == "F" then -- full
icon = icon .. "100-charging"
elseif state == "E" then -- empty
icon = icon .. "000"
elseif state == "D" then -- discharging
vis = true
time = htime(seconds)
icon = icon .. round(perc)
elseif state == "C" then -- charging
vis = true
time = htime(seconds)
icon = icon .. round(perc) .. "-charging"
else
icon = icon .. "caution"
end
return function(user_settings)
for k,v in pairs(user_settings) do
settings[k] = v
end

return {
icon = beautiful["icon-battery-caution"],
conky = "${battery_short} ${battery_time}",
conkybox = { forced_width = 30, align = "center" },
updater = function(update, textbox, iconbox, _)

local iter = string.gmatch(update, "%S+")
local state, perc, seconds = iter(), iter(), iter()

-- print("text:", t)
-- print("icon:", i)
local icon = "icon-battery-"
local vis = false
local time = nil

if time then textbox:set_text(time) end
textbox.visible = vis
if icon ~= current_icon then
iconbox:set_image(beautiful[icon] or beautiful["icon-battery-caution"])
current_icon = icon
if state == "F" then -- full
icon = icon .. "100-charging"
suspend_initiated = false
elseif state == "E" then -- empty
icon = icon .. "000"
maybe_suspend(seconds)
elseif state == "D" then -- discharging
vis = true
time = htime(seconds)
icon = icon .. round(perc)
maybe_suspend(seconds)
elseif state == "C" then -- charging
vis = true
suspend_initiated = false
time = htime(seconds)
icon = icon .. round(perc) .. "-charging"
else
icon = icon .. "caution"
end

if time then textbox:set_text(time) end
textbox.visible = vis
if icon ~= current_icon then
iconbox:set_image(beautiful[icon] or beautiful["icon-battery-caution"])
current_icon = icon
end
end
end
}
}
end

0 comments on commit 347549c

Please sign in to comment.