Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: add icons module #39

Merged
merged 4 commits into from
Jul 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 11 additions & 69 deletions lua/battery/battery.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,48 +7,14 @@ local powersupply = require('battery.powersupply')
local acpi = require('battery.acpi')
local config = require('battery.config')
local file = require('util.file')
local icons = require('battery.icons')

-- TODO check for icons and if not available fallback to text
-- TODO allow user to select no icons
-- TODO maybe autodetect icons?

local log = L.new({ plugin = 'battery' })

-- https://www.nerdfonts.com/cheat-sheet
local no_battery_icon = '󰇅' -- "󰟀"
-- local charging_battery_icons = {
-- { "󰂆", 20 },
-- { "󰂇", 30 },
-- { "󰂈", 40 },
-- { "󰂉", 60 },
-- { "󰂊", 80 },
-- { "󰂋", 90 },
-- { "󰂅", 100 },
-- }

local horizontal_battery_icons = {
{ '', 5 },
{ '', 25 },
{ '', 50 },
{ '', 75 },
{ '', 100 },
}

local plugged_icon = '󰚥'
local unplugged_icon = '󰚦'
local discharging_battery_icons = {
{ '󰁺', 10 },
{ '󰁻', 20 },
{ '󰁼', 30 },
{ '󰁽', 40 },
{ '󰁾', 50 },
{ '󰁿', 60 },
{ '󰂀', 70 },
{ '󰂁', 80 },
{ '󰂂', 90 },
{ '󰁹', 100 },
}

-- TODO maybe store the update time here?
local battery_status = {
percent_charge_remaining = nil,
Expand All @@ -59,7 +25,7 @@ local battery_status = {

-- Gets the last updated battery information
-- TODO may add the ability to ask for it to be updated right now
local function get_battery_status()
function M.get_battery_status()
return battery_status
end

Expand Down Expand Up @@ -89,7 +55,7 @@ local function select_job()
end

-- This is used for the health check
local function get_method()
function M.get_method()
local method = battery_status.method
if method == nil then
_, method = select_job()
Expand Down Expand Up @@ -142,7 +108,7 @@ local function start_timer()
log.debug('start timer seq no ' .. timer)
end

local function setup(user_opts)
function M.setup(user_opts)
config.from_user_opts(user_opts)

local config_update_rate_seconds = tonumber(config.current.update_rate_seconds)
Expand All @@ -155,33 +121,13 @@ local function setup(user_opts)
start_timer()
end

-- Convert percentage charge to icon given a table of icons
-- and max charge for that icon
local function icon_for_percentage(p, icon_table)
for _, icon in ipairs(icon_table) do
if tonumber(p) <= tonumber(icon[2]) then
return icon[1]
end
end
vim.notify('No icon found for percentage ' .. p)
return '!'
end

local function discharging_battery_icon_for_percent(p)
return icon_for_percentage(p, discharging_battery_icons)
end

local function horizontal_battery_icon_for_percent(p)
return icon_for_percentage(p, horizontal_battery_icons)
end

local function get_status_line()
function M.get_status_line()
if battery_status.battery_count == nil then
return '󰂑'
return icons.specific.unknown
else
if battery_status.battery_count == 0 then
if config.current.show_status_when_no_battery == true then
return no_battery_icon
return icons.specific.no_battery
else
return ''
end
Expand All @@ -191,9 +137,9 @@ local function get_status_line()

local plug_icon = ''
if ac_power and config.current.show_plugged_icon then
plug_icon = plugged_icon
plug_icon = icons.specific.plugged
elseif not ac_power and config.current.show_unplugged_icon then
plug_icon = unplugged_icon
plug_icon = icons.specific.unplugged
end

local percent = ''
Expand All @@ -203,18 +149,14 @@ local function get_status_line()

local icon
if config.current.vertical_icons == true then
icon = discharging_battery_icon_for_percent(battery_percent)
icon = icons.discharging_battery_icon_for_percent(battery_percent)
else
icon = horizontal_battery_icon_for_percent(battery_percent)
icon = icons.horizontal_battery_icon_for_percent(battery_percent)
end

return icon .. plug_icon .. percent
end
end
end

M.setup = setup
M.get_battery_status = get_battery_status
M.get_status_line = get_status_line
M.get_method = get_method
return M
115 changes: 115 additions & 0 deletions lua/battery/icons.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
-- https://www.nerdfonts.com/cheat-sheet?q=battery

local M = {}

---@alias IconSet { [1]: string, [2]: integer }[]

---@type table<string, IconSet>
M.sets = {
plain = {
{ '󰁺', 10 }, -- nf-md-battery_10.
{ '󰁻', 20 }, -- nf-md-battery_20
{ '󰁼', 30 }, -- nf-md-battery_30
{ '󰁽', 40 }, -- nf-md-battery_40
{ '󰁾', 50 }, -- nf-md-battery_50
{ '󰁿', 60 }, -- nf-md-battery_60
{ '󰂀', 70 }, -- nf-md-battery_70
{ '󰂁', 80 }, -- nf-md-battery_80
{ '󰂂', 90 }, -- nf-md-battery_90
{ '󰁹', 100 }, -- nf-md-battery
},

plain_charging = {
{ '󰢜', 10 }, -- nf-md-battery_charging_10
{ '󰂆', 20 }, -- nf-md-battery_charging_20
{ '󰂇', 30 }, -- nf-md-battery_charging_30
{ '󰂈', 40 }, -- nf-md-battery_charging_40
{ '󰢝', 50 }, -- nf-md-battery_charging_50
{ '󰂉', 60 }, -- nf-md-battery_charging_60
{ '󰢞', 70 }, -- nf-md-battery_charging_70
{ '󰂊', 80 }, -- nf-md-battery_charging_80
{ '󰂋', 90 }, -- nf-md-battery_charging_90
{ '󰂅', 100 }, -- nf-md-battery_charging_100
},

plain_charging_wireless = {
{ '󰠒', 5 }, -- nf-md-battery_charging_wireless_outline
{ '󰠈', 10 }, -- nf-md-battery_charging_wireless_10
{ '󰠉', 20 }, -- nf-md-battery_charging_wireless_20
{ '󰠊', 30 }, -- nf-md-battery_charging_wireless_30
{ '󰠋', 40 }, -- nf-md-battery_charging_wireless_40
{ '󰠌', 50 }, -- nf-md-battery_charging_wireless_50
{ '󰠍', 60 }, -- nf-md-battery_charging_wireless_60
{ '󰠎', 70 }, -- nf-md-battery_charging_wireless_70
{ '󰠏', 80 }, -- nf-md-battery_charging_wireless_80
{ '󰠐', 90 }, -- nf-md-battery_charging_wireless_90
{ '󰠇', 100 }, -- nf-md-battery_charging_wireless
},

plain_bluetooth = {
{ '󰤾', 10 }, -- nf-md-battery_10_bluetooth
{ '󰤿', 20 }, -- nf-md-battery_20_bluetooth
{ '󰥀', 30 }, -- nf-md-battery_30_bluetooth
{ '󰥁', 40 }, -- nf-md-battery_40_bluetooth
{ '󰥂', 50 }, -- nf-md-battery_50_bluetooth
{ '󰥃', 60 }, -- nf-md-battery_60_bluetooth
{ '󰥄', 70 }, -- nf-md-battery_70_bluetooth
{ '󰥅', 80 }, -- nf-md-battery_80_bluetooth
{ '󰥆', 90 }, -- nf-md-battery_90_bluetooth
{ '󰥈', 100 }, -- nf-md-battery_bluetooth
},

horizontal = {
{ '', 5 }, -- nf-fa-battery_0
{ '', 25 }, -- nf-fa-battery_1
{ '', 50 }, -- nf-fa-battery_2
{ '', 75 }, -- nf-fa-battery_3
{ '', 100 }, -- nf-fa-battery_4
},

bars = {
{ '󱊡', 33 }, -- nf-md-battery_low
{ '󱊢', 66 }, -- nf-md-battery_medium
{ '󱊣', 100 }, -- nf-md-battery_high
},

bars_charging = {
{ '󱊤', 33 }, -- nf-md-battery_charging_low
{ '󱊥', 66 }, -- nf-md-battery_charging_medium
{ '󱊦', 100 }, -- nf-md-battery_charging_high
},
}

M.specific = {
plugged = '󰚥',
unplugged = '󰚦',
no_battery = '󰇅',
no_battery_classic = '󰟀',
unknown = '󰂑',
}

---Convert percentage charge to icon given a table of icons
---and max charge for that icon

---@param p integer
---@param icon_table IconSet
---@return string
function M.icon_for_percentage(p, icon_table)
for _, icon in ipairs(icon_table) do
if tonumber(p) <= tonumber(icon[2]) then
return icon[1]
end
end
vim.notify('No icon found for percentage ' .. p)
return '!'
end

function M.discharging_battery_icon_for_percent(p)
return M.icon_for_percentage(p, M.sets.plain)
end

function M.horizontal_battery_icon_for_percent(p)
return M.icon_for_percentage(p, M.sets.horizontal)
end

return M
Loading