Skip to content

Commit

Permalink
Better suspend reporting
Browse files Browse the repository at this point in the history
  • Loading branch information
ojv committed Mar 8, 2017
1 parent 6733d17 commit a06abde
Showing 1 changed file with 26 additions and 13 deletions.
39 changes: 26 additions & 13 deletions widgets/battery.lua
Original file line number Diff line number Diff line change
@@ -1,52 +1,65 @@
-- INIT -- {{{1
local beautiful = require("beautiful")
local naughty = require("naughty")
local awful = require("awful")

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

local function htime(seconds)
local function htime(seconds) -- {{{1
local hours = math.floor(tonumber(seconds) / 3600)
return string.format("%d:%02d", hours, (seconds - (3600 * hours)) / 60)
end

local function round(perc)
local function round(perc) -- {{{1
return string.format("%03d", math.floor(string.sub(perc, 1, -2) / 20) * 20)
end

local suspend_initiated = false

local function suspend()
local function suspend() -- {{{1
if suspend_initiated then
awful.spawn(settings.suspend_cmd)
end
suspend_notification = nil
end

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

suspend_initiated = true
naughty.notify({
suspend_notification = 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",
"Click this notification to suspend immediately",
timeout = 60,
destroy = suspend
fg = beautiful.fg_urgent,
bg = beautiful.bg_urgent,
destroy = suspend,
})
end

local current_icon = nil
local function maybe_cancel_suspend() -- {{{1
if suspend_initiated == false or suspend_notification == nil then
return
end
suspend_initiated = false
suspend_notification.die(naughty.notificationClosedReason.dismissedByUser)
end

-- widget constructor {{{1
return function(user_settings)
for k,v in pairs(user_settings) do
settings[k] = v
end

local current_icon = nil

return {
icon = beautiful["icon-battery-caution"],
conky = "${battery_short} ${battery_time}",
Expand All @@ -62,7 +75,7 @@ return function(user_settings)

if state == "F" then -- full
icon = icon .. "100-charging"
suspend_initiated = false
maybe_cancel_suspend()
elseif state == "E" then -- empty
icon = icon .. "000"
maybe_suspend(seconds)
Expand All @@ -73,7 +86,7 @@ return function(user_settings)
maybe_suspend(seconds)
elseif state == "C" then -- charging
vis = true
suspend_initiated = false
maybe_cancel_suspend()
time = htime(seconds)
icon = icon .. round(perc) .. "-charging"
else
Expand Down

0 comments on commit a06abde

Please sign in to comment.