From 5d58396742dfed626240d98f7e73d54e962cfa3a Mon Sep 17 00:00:00 2001 From: "Eamon Burns (Laptop WSL)" Date: Tue, 2 Jul 2024 17:34:31 -0700 Subject: [PATCH 1/4] fix: change battery.lua to be consistent with other modules --- lua/battery/battery.lua | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/lua/battery/battery.lua b/lua/battery/battery.lua index 04a3fda..27ccf27 100644 --- a/lua/battery/battery.lua +++ b/lua/battery/battery.lua @@ -14,7 +14,7 @@ local file = require('util.file') local log = L.new({ plugin = 'battery' }) --- https://www.nerdfonts.com/cheat-sheet +-- https://www.nerdfonts.com/cheat-sheet?q=battery local no_battery_icon = '󰇅' -- "󰟀" -- local charging_battery_icons = { -- { "󰂆", 20 }, @@ -59,7 +59,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 @@ -89,7 +89,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() @@ -142,7 +142,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) @@ -175,7 +175,7 @@ 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 '󰂑' else @@ -213,8 +213,4 @@ local function get_status_line() 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 From c715920498f4425126bf8da1b1749650f77f7b37 Mon Sep 17 00:00:00 2001 From: "Eamon Burns (Laptop WSL)" Date: Wed, 3 Jul 2024 09:44:17 -0700 Subject: [PATCH 2/4] feat(icons): add icons module with icon sets --- lua/battery/battery.lua | 23 ++++++----- lua/battery/icons.lua | 89 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 101 insertions(+), 11 deletions(-) create mode 100644 lua/battery/icons.lua diff --git a/lua/battery/battery.lua b/lua/battery/battery.lua index 27ccf27..443f406 100644 --- a/lua/battery/battery.lua +++ b/lua/battery/battery.lua @@ -7,6 +7,7 @@ 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 @@ -15,7 +16,7 @@ local file = require('util.file') local log = L.new({ plugin = 'battery' }) -- https://www.nerdfonts.com/cheat-sheet?q=battery -local no_battery_icon = '󰇅' -- "󰟀" +local _no_battery_icon = '󰇅' -- "󰟀" -- local charging_battery_icons = { -- { "󰂆", 20 }, -- { "󰂇", 30 }, @@ -26,7 +27,7 @@ local no_battery_icon = '󰇅' -- "󰟀" -- { "󰂅", 100 }, -- } -local horizontal_battery_icons = { +local _horizontal_battery_icons = { { '', 5 }, { '', 25 }, { '', 50 }, @@ -34,9 +35,9 @@ local horizontal_battery_icons = { { '', 100 }, } -local plugged_icon = '󰚥' -local unplugged_icon = '󰚦' -local discharging_battery_icons = { +local _plugged_icon = '󰚥' +local _unplugged_icon = '󰚦' +local _discharging_battery_icons = { { '󰁺', 10 }, { '󰁻', 20 }, { '󰁼', 30 }, @@ -168,20 +169,20 @@ local function icon_for_percentage(p, icon_table) end local function discharging_battery_icon_for_percent(p) - return icon_for_percentage(p, discharging_battery_icons) + return icon_for_percentage(p, icons.icon_sets.plain) end local function horizontal_battery_icon_for_percent(p) - return icon_for_percentage(p, horizontal_battery_icons) + return icon_for_percentage(p, icons.icon_sets.horizontal) end function M.get_status_line() if battery_status.battery_count == nil then - return '󰂑' + return icons.specific_icons.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_icons.no_battery else return '' end @@ -191,9 +192,9 @@ function M.get_status_line() local plug_icon = '' if ac_power and config.current.show_plugged_icon then - plug_icon = plugged_icon + plug_icon = icons.specific_icons.plugged elseif not ac_power and config.current.show_unplugged_icon then - plug_icon = unplugged_icon + plug_icon = icons.specific_icons.unplugged end local percent = '' diff --git a/lua/battery/icons.lua b/lua/battery/icons.lua new file mode 100644 index 0000000..49c2490 --- /dev/null +++ b/lua/battery/icons.lua @@ -0,0 +1,89 @@ +local M = {} + +---@alias IconSet { [1]: string, [2]: integer }[] + +---@type table +M.icon_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_icons = { + plugged = '󰚥', + unplugged = '󰚦', + no_battery = '󰇅', + no_battery_classic = '󰟀', + unknown = '󰂑', +} + +return M From f6bce2201b8f198eef1deb7ff6ec3180277e3f71 Mon Sep 17 00:00:00 2001 From: "Eamon Burns (Laptop WSL)" Date: Wed, 3 Jul 2024 11:03:55 -0700 Subject: [PATCH 3/4] feat(icons): move icon-related functions to icons module --- lua/battery/battery.lua | 59 ++--------------------------------------- lua/battery/icons.lua | 26 ++++++++++++++++++ 2 files changed, 28 insertions(+), 57 deletions(-) diff --git a/lua/battery/battery.lua b/lua/battery/battery.lua index 443f406..9958d96 100644 --- a/lua/battery/battery.lua +++ b/lua/battery/battery.lua @@ -15,41 +15,6 @@ local icons = require('battery.icons') local log = L.new({ plugin = 'battery' }) --- https://www.nerdfonts.com/cheat-sheet?q=battery -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, @@ -156,26 +121,6 @@ function M.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, icons.icon_sets.plain) -end - -local function horizontal_battery_icon_for_percent(p) - return icon_for_percentage(p, icons.icon_sets.horizontal) -end - function M.get_status_line() if battery_status.battery_count == nil then return icons.specific_icons.unknown @@ -204,9 +149,9 @@ function M.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 diff --git a/lua/battery/icons.lua b/lua/battery/icons.lua index 49c2490..599ece7 100644 --- a/lua/battery/icons.lua +++ b/lua/battery/icons.lua @@ -1,3 +1,5 @@ +-- https://www.nerdfonts.com/cheat-sheet?q=battery + local M = {} ---@alias IconSet { [1]: string, [2]: integer }[] @@ -86,4 +88,28 @@ M.specific_icons = { 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.icon_sets.plain) +end + +function M.horizontal_battery_icon_for_percent(p) + return M.icon_for_percentage(p, M.icon_sets.horizontal) +end + return M From 5c75011ab6267a3ceff177d77d1ea03493590c86 Mon Sep 17 00:00:00 2001 From: "Eamon Burns (Laptop WSL)" Date: Wed, 3 Jul 2024 11:09:26 -0700 Subject: [PATCH 4/4] fix(icons): rename icon tables to be less redundant --- lua/battery/battery.lua | 8 ++++---- lua/battery/icons.lua | 8 ++++---- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/lua/battery/battery.lua b/lua/battery/battery.lua index 9958d96..59287bd 100644 --- a/lua/battery/battery.lua +++ b/lua/battery/battery.lua @@ -123,11 +123,11 @@ end function M.get_status_line() if battery_status.battery_count == nil then - return icons.specific_icons.unknown + return icons.specific.unknown else if battery_status.battery_count == 0 then if config.current.show_status_when_no_battery == true then - return icons.specific_icons.no_battery + return icons.specific.no_battery else return '' end @@ -137,9 +137,9 @@ function M.get_status_line() local plug_icon = '' if ac_power and config.current.show_plugged_icon then - plug_icon = icons.specific_icons.plugged + plug_icon = icons.specific.plugged elseif not ac_power and config.current.show_unplugged_icon then - plug_icon = icons.specific_icons.unplugged + plug_icon = icons.specific.unplugged end local percent = '' diff --git a/lua/battery/icons.lua b/lua/battery/icons.lua index 599ece7..bb3e466 100644 --- a/lua/battery/icons.lua +++ b/lua/battery/icons.lua @@ -5,7 +5,7 @@ local M = {} ---@alias IconSet { [1]: string, [2]: integer }[] ---@type table -M.icon_sets = { +M.sets = { plain = { { '󰁺', 10 }, -- nf-md-battery_10. { '󰁻', 20 }, -- nf-md-battery_20 @@ -80,7 +80,7 @@ M.icon_sets = { }, } -M.specific_icons = { +M.specific = { plugged = '󰚥', unplugged = '󰚦', no_battery = '󰇅', @@ -105,11 +105,11 @@ function M.icon_for_percentage(p, icon_table) end function M.discharging_battery_icon_for_percent(p) - return M.icon_for_percentage(p, M.icon_sets.plain) + 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.icon_sets.horizontal) + return M.icon_for_percentage(p, M.sets.horizontal) end return M