about summary refs log tree commit diff
path: root/.config/luastatus/battery.lua
blob: 459599e00956eb96049bcda9551f5b6cc7497b1f (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
local function read(path)
  local f = io.open(path)
  if not f then return nil end
  local v = tonumber(f:read('*l'))
  f:close()
  return v
end

widget = {
  plugin = 'timer',
  opts = { period = 2 },
  cb = function()
    local base        = '/sys/class/power_supply/macsmc-battery/'

    local energy_now  = read(base .. 'energy_now')
    local energy_full = read(base .. 'energy_full')
    local status_f    = io.open(base .. 'status')

    if not energy_now or not energy_full or not status_f then
      return { { full_text = 'bat ?' } }
    end

    local status = status_f:read('*l')
    status_f:close()

    local capacity = math.floor(energy_now / energy_full * 100 + 0.5)

    local text_color = '#ffffff'
    local battery_symbol = ' '

    if capacity < 30 then
      text_color = '#ed8796'
      battery_symbol = '󰁼'
    elseif capacity > 70 then
      text_color = '#a6da95'
      battery_symbol = '󰂁'
    else
      text_color = '#eed49f'
      battery_symbol = '󰁾'
    end

    local symbol = ({
      Charging    = '󱐋',
      Discharging = battery_symbol,
    })[status] or ' '

    return {
      { full_text = string.format('%s %2d%%', symbol, capacity), color = text_color }
    }
  end,
  event = [[
        local t = ...
        if t.button == 1 then
            os.execute('~/.local/bin/rofi-ppd &')
        end
    ]],
}