Luigit
repositories / dotfiles

dotfiles

bugabingas dorkfiles

owned by admin

wezterm/bugabinga/battery.lua

Raw
-- Adapted from https://github.com/rootiest/battery.wez/blob/main/plugin/init.lua
local wez = require 'wezterm'

local charging = { '󰢟 ', '󰢜 ', '󰂆 ', '󰂇 ', '󰂈 ', '󰢝 ', '󰂉 ', '󰢞 ', '󰂊 ', '󰂋 ', '󰂅 ', }
local discharging = { '󰂎 ', '󰁺 ', '󰁻 ', '󰁼 ', '󰁽 ', '󰁾 ', '󰁿 ', '󰂀 ', '󰂁 ', '󰂂 ', '󰁹 ', }

-- Match Nuguland's battery severity thresholds.
local critical_charge = 0.10
local low_charge = 0.25

local function charge_color( charge, palette )
  if charge <= critical_charge then return palette.error end
  if charge <= low_charge then return palette.warning end
  return palette.ui.accent
end

local function charge_icon( icons, charge, palette )
  local index = math.max( 1, math.min( math.ceil( charge * 10 ), #icons ) )
  return icons[index], charge_color( charge, palette )
end

local function battery_icon( battery, palette )
  local glyph, foreground = '󰂑 ', palette.ui.minor

  if battery.state == 'Full' then
    glyph, foreground = '󰁹 ', palette.ui.accent
  elseif battery.state == 'Empty' then
    glyph, foreground = '󱃍 ', palette.error
  elseif battery.state == 'Charging' then
    glyph, foreground = charge_icon( charging, battery.state_of_charge, palette )
  elseif battery.state == 'Discharging' then
    glyph, foreground = charge_icon( discharging, battery.state_of_charge, palette )
  elseif battery.state == 'Unknown' and battery.state_of_charge > 0.5 then
    glyph, foreground = '󱈑 ', palette.ui.accent
  end

  return wez.format {
    { Foreground = { Color = foreground, }, },
    { Text = glyph, },
  }
end

---@param palette table
---@return string
local function get_battery_icons( palette )
  local icons = {}
  for _, battery in ipairs( wez.battery_info() ) do
    table.insert( icons, battery_icon( battery, palette ) )
  end
  return table.concat( icons, ' | ' )
end

return {
  get_battery_icons = get_battery_icons,
}