diff --git a/README.md b/README.md index e0cf971..0601e4d 100644 --- a/README.md +++ b/README.md @@ -122,9 +122,9 @@ If something breaks you should see a standard Vim error telling you what the pro For more than just info,warn and error logging you can enable debug logs which show a more verbose behaviour of the plugin using the following command to launch nvim. -`PLENARY_DEBUG=true nvim` +`DEBUG_PLENARY=true nvim` ## Notes Inspired by [lambdalisue/battery.vim](https://github.com/lambdalisue/battery.vim), which in turn uses code from [b4b4r07/dotfiles](https://github.com/b4b4r07/dotfiles/blob/66dddda6803ada50a0ab879e5db784afea72b7be/.tmux/bin/battery#L10). -Copyright (c) 2022 Justin Heyes-Jones +Copyright (c) 2022-2024 Justin Heyes-Jones diff --git a/lua/battery/battery.lua b/lua/battery/battery.lua index 633b2de..f4f0941 100644 --- a/lua/battery/battery.lua +++ b/lua/battery/battery.lua @@ -52,6 +52,7 @@ local battery_status = { percent_charge_remaining = nil, battery_count = nil, ac_power = nil, + method = nil, } -- Gets the last updated battery information @@ -69,22 +70,34 @@ local timer = nil 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 + 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 + return pmset.get_battery_info_job, 'pmset' elseif vim.fn.executable("acpi") == 1 then log.debug("acpi battery job") - return acpi.get_battery_info_job + return acpi.get_battery_info_job, 'acpi' else log.debug("no battery job") + return nil, 'none' end end +-- This is used for the health check +local function get_method() + local method = battery_status.method + if method == nil then + _, method = select_job() + end + return battery_status.method +end + local function timer_loop() vim.defer_fn(function() log.debug(timer .. " is running now") - local job_function = select_job() + local job_function, method = select_job() + battery_status.method = method + log.debug("using method " .. method) if job_function then job_function(battery_status):start() @@ -112,7 +125,9 @@ local function start_timer() timer = require("util.timers").get_next() -- Always call the job immediately before starting the timed loop - local job_function = select_job() + local job_function, method = select_job() + battery_status.method = method + log.debug("using method " .. method) if job_function then job_function(battery_status):start() @@ -196,4 +211,5 @@ 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 diff --git a/lua/battery/health.lua b/lua/battery/health.lua index 428a370..bc08e36 100644 --- a/lua/battery/health.lua +++ b/lua/battery/health.lua @@ -4,23 +4,19 @@ local start = vim.health.start or vim.health.report_start local ok = vim.health.ok or vim.health.report_ok local error = vim.health.error or vim.health.report_error --- TODO should check if the plugin is initialized and the setup is valid --- TODO should check what method is being used for battery information +local B = require("battery.battery") -local function check_setup() - return true +local function check_method() + return B.get_method() ~= nil end M.check = function() - start("battery report") - -- make sure setup function parameters are ok - if check_setup() then - ok("Setup function is correct") + start("Checking method to get battery status") + if check_method() then + ok("Using " .. B.get_method() .. "") else - error("Setup function is incorrect") + error("No method found.") end - -- do some more checking - -- ... end return M