summaryrefslogtreecommitdiff
path: root/config/awesome/battery.lua
blob: 28e7033fb3d03b832f6e0618d95b49746c3f05fa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
-- TODO: switch to async IO: https://github.com/Elv13/awesome-configs/blob/master/widgets/battery.lua
local gears = require("gears")
local naughty = require("naughty")
local beautiful = require("beautiful")

function batteryInfoUpdate(adapters, widget)
    spacer = " "

    local total_cur = 0.0
    local total_max = 0.0
    local status = ''

    for i = 1, #adapters do
        local fcap = io.open("/sys/class/power_supply/"..adapters[i].."/capacity")
        local fsta = io.open("/sys/class/power_supply/"..adapters[i].."/status")
        local cap = fcap:read()
        local sta = fsta:read()
        fcap:close()
        fsta:close()

        if sta:match("Charging") then
            icon = "▴"
        elseif sta:match("Discharging") then
            icon =  "▾"
        else
            icon =  "‐"
        end

        status = status .. cap..'%'..icon
        if i < #adapters then
            status = status ..','
        end
        total_cur = total_cur + tonumber(cap)
        total_max = total_max + 1.0
    end

    local cap = total_cur/total_max

    if cap < 10 then
        local bg = beautiful.bg_focus
        local fg = beautiful.fg_focus
        if cap <= 5 then
           bg = beautiful.bg_urgent
           fg = beautiful.fg_urgent
        end

        naughty.notify({ title      = "Battery Warning",
                         text       = "Battery low at "..cap.."%!",
                         timeout    = 15,
                         position   = "top_right",
                         fg         = fg,
                         bg         = bg
                       })
    end

    local color=nil
    if tonumber(cap) < 10 then
        color='red'
    elseif tonumber(cap) < 20 then
        color='orange'
    end

    if color then
        status = "<span color='"..color.."'>"..status..'</span>'
    end

    widget:set_markup(spacer..status..spacer)
end

local wibox = require("wibox")

battery_widget = wibox.widget.textbox()
battery_widget:set_align("right")

battery_timer = gears.timer({timeout = 20,
                            call_now = true,
                            autostart = true,
                            callback = function()
                              batteryInfoUpdate( {"BAT0", "BAT1"}, battery_widget)
                            end,
                            })
-- vim:set softtabstop=4:ts=4:shiftwidth=4:et=1: