Skip to content

Commit dca736d

Browse files
committed
rebase and add readable directory predicate
1 parent dfe2ff5 commit dca736d

File tree

5 files changed

+45
-25
lines changed

5 files changed

+45
-25
lines changed

.luacheckrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
globals = { "vim" }
1+
globals = { "vim", "_" }

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,9 +122,9 @@ If something breaks you should see a standard Vim error telling you what the pro
122122

123123
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.
124124

125-
`PLENARY_DEBUG=true nvim`
125+
`DEBUG_PLENARY=true nvim`
126126

127127
## Notes
128128
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).
129129

130-
Copyright (c) 2022 Justin Heyes-Jones
130+
Copyright (c) 2022-2024 Justin Heyes-Jones

lua/battery/battery.lua

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ local pmset = require("battery.pmset")
66
local powersupply = require("battery.powersupply")
77
local acpi = require("battery.acpi")
88
local config = require("battery.config")
9+
local file = require("util.file")
910

1011
-- TODO check for icons and if not available fallback to text
1112
-- TODO allow user to select no icons
@@ -53,6 +54,7 @@ local battery_status = {
5354
percent_charge_remaining = nil,
5455
battery_count = nil,
5556
ac_power = nil,
57+
method = nil,
5658
}
5759

5860
-- Gets the last updated battery information
@@ -70,28 +72,37 @@ local timer = nil
7072
local function select_job()
7173
if vim.fn.has("win32") and vim.fn.executable("powershell") == 1 then
7274
log.debug("windows powershell battery job")
73-
return powershell.get_battery_info_job
75+
return powershell.get_battery_info_job, 'powershell'
7476
elseif vim.fn.executable("pmset") == 1 then
7577
log.debug("pmset battery job")
76-
return pmset.get_battery_info_job
77-
-- Ensure directory exists (isdirectory() == 1) and is readable ($(ls) doesn't contain "ls: cannot")
78-
elseif vim.fn.isdirectory("/sys/class/power_supply/") == 1 and
79-
-- TODO: Find better way to check for readability (exit code of `ls`?)
80-
not vim.fn.system("ls /sys/class/power_supply/"):find("ls: cannot") then
81-
log.debug("/sys/class/power_supply/ battery job")
82-
return powersupply.get_battery_info_job
78+
return pmset.get_battery_info_job, 'pmset'
79+
elseif file.is_readable_directory("/sys/class/power_supply/") then
80+
log.debug("power_supply battery job")
81+
return powersupply.get_battery_info_job, 'powersupply'
8382
elseif vim.fn.executable("acpi") == 1 then
8483
log.debug("acpi battery job")
85-
return acpi.get_battery_info_job
84+
return acpi.get_battery_info_job, 'acpi'
8685
else
8786
log.debug("no battery job")
87+
return nil, 'none'
8888
end
8989
end
9090

91+
-- This is used for the health check
92+
local function get_method()
93+
local method = battery_status.method
94+
if method == nil then
95+
_, method = select_job()
96+
end
97+
return method
98+
end
99+
91100
local function timer_loop()
92101
vim.defer_fn(function()
93102
log.debug(timer .. " is running now")
94-
local job_function = select_job()
103+
local job_function, method = select_job()
104+
battery_status.method = method
105+
log.debug("using method " .. method)
95106

96107
if job_function then
97108
job_function(battery_status):start()
@@ -119,7 +130,9 @@ local function start_timer()
119130
timer = require("util.timers").get_next()
120131

121132
-- Always call the job immediately before starting the timed loop
122-
local job_function = select_job()
133+
local job_function, method = select_job()
134+
battery_status.method = method
135+
log.debug("using method " .. method)
123136

124137
if job_function then
125138
job_function(battery_status):start()
@@ -203,4 +216,5 @@ end
203216
M.setup = setup
204217
M.get_battery_status = get_battery_status
205218
M.get_status_line = get_status_line
219+
M.get_method = get_method
206220
return M

lua/battery/health.lua

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,19 @@ local start = vim.health.start or vim.health.report_start
44
local ok = vim.health.ok or vim.health.report_ok
55
local error = vim.health.error or vim.health.report_error
66

7-
-- TODO should check if the plugin is initialized and the setup is valid
8-
-- TODO should check what method is being used for battery information
7+
local B = require("battery.battery")
98

10-
local function check_setup()
11-
return true
9+
local function check_method()
10+
return B.get_method() ~= nil
1211
end
1312

1413
M.check = function()
15-
start("battery report")
16-
-- make sure setup function parameters are ok
17-
if check_setup() then
18-
ok("Setup function is correct")
14+
start("Checking method to get battery status")
15+
if check_method() then
16+
ok("Using " .. B.get_method() .. "")
1917
else
20-
error("Setup function is incorrect")
18+
error("No method found.")
2119
end
22-
-- do some more checking
23-
-- ...
2420
end
2521

2622
return M

lua/util/file.lua

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
local bit = require('bit')
2+
3+
local function is_readable_directory(file)
4+
local s = vim.loop.fs_stat(file)
5+
return s ~= nil and s.type == 'directory' and bit.band(s.mode, 4) == 4
6+
end
7+
8+
return {
9+
is_readable_directory = is_readable_directory,
10+
}

0 commit comments

Comments
 (0)