aboutsummaryrefslogtreecommitdiff
path: root/.config/awesome/components/statusbar/battery.lua
blob: 670b2000b57c240863a53fc08c83c4618545d226 (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
local awful = require "awful"
local xresources = require "beautiful.xresources"
local dpi = xresources.apply_dpi
local vars = require "themes.prismite.vars"
local wibox = require "wibox"

local battery_inner = awful.widget.watch("cat /sys/class/power_supply/BAT0/capacity", 1, function(widget, stdout)
    local icon = ""
    local color = vars.colors.red

    if io.popen("cat /sys/class/power_supply/BAT0/status"):read("*a"):sub(0, -2) == "Charging" then
        icon = ""
        color = vars.colors.green
        widget:set_markup("<span color=\"" .. color .. "\">" .. icon .. "</span>")
        return
    end

    local percent = tonumber(stdout)

    if percent <= 5 then
        icon = ""
        color = vars.colors.red
    elseif percent <= 10 then
        icon = ""
        color = vars.colors.red
    elseif percent <= 20 then
        icon = ""
        color = vars.colors.red
    elseif percent <= 30 then
        icon = ""
        color = vars.colors.yellow
    elseif percent <= 40 then
        icon = ""
        color = vars.colors.yellow
    elseif percent <= 50 then
        icon = ""
        color = vars.colors.yellow
    elseif percent <= 60 then
        icon = ""
        color = vars.colors.yellow
    elseif percent <= 70 then
        icon = ""
        color = vars.colors.yellow
    elseif percent <= 80 then
        icon = ""
        color = vars.colors.green
    elseif percent <= 90 then
        icon = ""
        color = vars.colors.green
    elseif percent <= 100 then
        icon = ""
        color = vars.colors.green
    end

    widget:set_markup("<span color=\"" .. color .. "\">" .. icon .. "</span>")
end)

local battery = wibox.widget {
    widget = wibox.container.place,
    battery_inner
}

awful.tooltip {
    objects = { battery },
    timer_function = function()
        return io.popen("cat /sys/class/power_supply/BAT0/capacity"):read("*a"):sub(0, -2) .. "%"
    end,
    bg = vars.colors.bg,
    fg = vars.colors.fg,
    border_color = vars.colors.bright.black,
    border_width = vars.border_width,
    font = vars.font,
    shape = vars.shape,
    margin_leftright = dpi(6),
    margin_topbottom = dpi(6)
}

return battery