Skip to content

Commit

Permalink
add health check that checks something
Browse files Browse the repository at this point in the history
  • Loading branch information
justinhj-td committed Jun 30, 2024
1 parent 3ec406c commit 1b8d790
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 18 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
26 changes: 21 additions & 5 deletions lua/battery/battery.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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
18 changes: 7 additions & 11 deletions lua/battery/health.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 1b8d790

Please sign in to comment.