diff --git a/lua/battery/battery.lua b/lua/battery/battery.lua index 59287bd..d35d7c9 100644 --- a/lua/battery/battery.lua +++ b/lua/battery/battery.lua @@ -1,12 +1,8 @@ local M = {} local L = require('plenary.log') -local powershell = require('battery.powershell') -local pmset = require('battery.pmset') -local powersupply = require('battery.powersupply') -local acpi = require('battery.acpi') local config = require('battery.config') -local file = require('util.file') +local parsers = require('battery.parsers') local icons = require('battery.icons') -- TODO check for icons and if not available fallback to text @@ -33,25 +29,21 @@ end -- can reload the battery module and we can detect the old job is still running. local timer = nil --- Select the battery info job to run based on platform and what programs --- are available +---Select the battery info job to run based on platform and what programs +---are available +---@return (fun(battery_status: table): any)? +---@return string? local function select_job() - if vim.fn.has('win32') and vim.fn.executable('powershell') == 1 then - log.debug('windows powershell battery job') - return powershell.get_battery_info_job, 'powershell' - elseif vim.fn.executable('pmset') == 1 then - log.debug('pmset battery job') - return pmset.get_battery_info_job, 'pmset' - elseif file.is_readable_directory('/sys/class/power_supply/') then - log.debug('power_supply battery job') - return powersupply.get_battery_info_job, 'powersupply' - elseif vim.fn.executable('acpi') == 1 then - log.debug('acpi battery job') - return acpi.get_battery_info_job, 'acpi' - else - log.debug('no battery job') - return nil, 'none' + for method, parser_module in pairs(parsers.parsers) do + if parser_module.check() then + log.debug('using '..method..' method') + return parser_module.get_battery_info_job, method + end end + + -- No suitable parser was found. + log.debug('no parser found') + return nil, nil end -- This is used for the health check @@ -68,7 +60,7 @@ local function timer_loop() log.debug(timer .. ' is running now') local job_function, method = select_job() battery_status.method = method - log.debug('using method ' .. method) + log.debug('using method ' .. (method or 'nil')) if job_function then job_function(battery_status):start() @@ -98,7 +90,7 @@ local function start_timer() -- Always call the job immediately before starting the timed loop local job_function, method = select_job() battery_status.method = method - log.debug('using method ' .. method) + log.debug('using method: ' .. (method or 'nil')) if job_function then job_function(battery_status):start() diff --git a/lua/battery/acpi.lua b/lua/battery/parsers/acpi.lua similarity index 94% rename from lua/battery/acpi.lua rename to lua/battery/parsers/acpi.lua index 8214807..1bb1b5d 100644 --- a/lua/battery/acpi.lua +++ b/lua/battery/parsers/acpi.lua @@ -84,4 +84,10 @@ function M.get_battery_info_job(battery_status) }) end +---Check if this parser would work in the current environment +---@return boolean +function M.check() + return vim.fn.executable('acpi') == 1 +end + return M diff --git a/lua/battery/parsers/init.lua b/lua/battery/parsers/init.lua new file mode 100644 index 0000000..acab27b --- /dev/null +++ b/lua/battery/parsers/init.lua @@ -0,0 +1,15 @@ +local M = {} + +---@class ParserModule +---@field check fun(): boolean +---@field get_battery_info_job fun(battery_status: table): any + +---@type table +M.parsers = { + powershell = require('battery.parsers.powershell'), + pmset = require('battery.parsers.pmset'), + powersupply = require('battery.parsers.powersupply'), + acpi = require('battery.parsers.acpi'), +} + +return M diff --git a/lua/battery/pmset.lua b/lua/battery/parsers/pmset.lua similarity index 94% rename from lua/battery/pmset.lua rename to lua/battery/parsers/pmset.lua index aa45b0b..c530e08 100644 --- a/lua/battery/pmset.lua +++ b/lua/battery/parsers/pmset.lua @@ -81,4 +81,10 @@ function M.get_battery_info_job(battery_status) }) end +---Check if this parser would work in the current environment +---@return boolean +function M.check() + return vim.fn.executable('pmset') == 1 +end + return M diff --git a/lua/battery/powershell.lua b/lua/battery/parsers/powershell.lua similarity index 94% rename from lua/battery/powershell.lua rename to lua/battery/parsers/powershell.lua index cf09675..791b775 100644 --- a/lua/battery/powershell.lua +++ b/lua/battery/parsers/powershell.lua @@ -87,4 +87,10 @@ function M.get_battery_info_job(battery_status) }) end +---Check if this parser would work in the current environment +---@return boolean +function M.check() + return vim.fn.has('win32') and vim.fn.executable('powershell') == 1 +end + return M diff --git a/lua/battery/powersupply.lua b/lua/battery/parsers/powersupply.lua similarity index 92% rename from lua/battery/powersupply.lua rename to lua/battery/parsers/powersupply.lua index 6eb5e19..5b0fd9e 100644 --- a/lua/battery/powersupply.lua +++ b/lua/battery/parsers/powersupply.lua @@ -6,6 +6,7 @@ local J = require('plenary.job') local L = require('plenary.log') local BC = require('util.chooser') local config = require('battery.config') +local file = require('util.file') local log = L.new({ plugin = 'battery' }) -- Convert lowercase status from `/sys/class/power_supply/BAT?/status` @@ -84,4 +85,10 @@ function M.get_battery_info_job(battery_status) }) end +---Check if this parser would work in the current environment +---@return boolean +function M.check() + return file.is_readable_directory('/sys/class/power_supply/') +end + return M