justinhj / battery.nvim

Neovim plugin to detect and view battery information
MIT License
59 stars 9 forks source link

Why is the plugin using `acpi` instead of the `/sys/class/power_supply/` files? #22

Closed Agent-E11 closed 3 months ago

Agent-E11 commented 3 months ago

I have tried installing acpid on WSL, but it doesn't seem to work. I know there are files in /sys/class/power_supply/ which supply all the data that the plugin needs. Is there a reason why the plugin depends on acpi instead of using these files? (Someone mentioned in #1 that these files are supplied by the Linux kernel, so they should work for all distributions). I think this is a more sensible default than requiring acpi as a dependency.

Agent-E11 commented 3 months ago

Possible implementation details using files:

Get number of batteries:

-- Length of this table:
local battery_paths = vim.split(vim.fn.glob("/sys/class/power_supply/BAT*"), "\n")

Get percentage (of the first battery):

local f = io.open(battery_paths[0] .. "/capacity", "r")
if f == nil then
    -- file not present
end

-- This is a number from 0 to 100 (or nil if it wasn't a number, which is probably impossible?)
local percentage = f:read("*number")
Agent-E11 commented 3 months ago

There is also a file called /sys/class/power_supply/BAT?/status, which has multiple different possible values. So far I have seen Charging, Discharging, Full, and Unknown, but I don't know if there are other possible statuses.

justinhj commented 3 months ago

Thanks. Some good points. I may look into switching to using /sys/class/power_supply/BAT?/status at some point. Support for the linux kernel is not completely accurate in WSL and acpid does not work but /sys/class/power_supply/BAT?/status also did not work 2 years ago.

In the meantime I am open to PRs.

Agent-E11 commented 3 months ago

but /sys/class/power_supply/BAT?/status also did not work 2 years ago

Could you explain this? How did it not work?

Currently in WSL, all of the examples I posted work, so it has likely been fixed unless you have some other cases which I did not come across.

justinhj commented 3 months ago

From memory it reported no batteries in WSL