Skip to content

Commit

Permalink
merge latest
Browse files Browse the repository at this point in the history
  • Loading branch information
justinhj-td committed Jul 1, 2024
2 parents 5c8ef0a + dca736d commit aa2f8ca
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 0 deletions.
5 changes: 5 additions & 0 deletions lua/battery/battery.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ 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")

-- TODO check for icons and if not available fallback to text
-- TODO allow user to select no icons
Expand Down Expand Up @@ -74,6 +76,9 @@ local function select_job()
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'
Expand Down
85 changes: 85 additions & 0 deletions lua/battery/powersupply.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
-- Get battery info using /sys/class/power_supply/* files. Requires Linux

local J = require("plenary.job")
local L = require("plenary.log")
local BC = require("util.chooser")
local config = require("battery.config")
local log = L.new({ plugin = "battery" })

-- Convert lowercase status from `/sys/class/power_supply/BAT?/status`
-- to whether AC power is connected
local status_to_ac_power = {
["full"] = true,
["charging"] = true,
["discharging"] = false,
["unknown"] = false, -- We don't know, so assume false
}

-- Parse the response from the battery info job and update
-- the battery status
local function parse_powersupply_battery_info(battery_paths, battery_status)
local path_count = #battery_paths
local battery_count = 0

if path_count > 0 then
-- Read capacities of each battery
local percents = {}
for _, path in ipairs(battery_paths) do
local f = io.open(path .. "/capacity", "r")
if f then
local charge = f:read("n")
if charge then
battery_count = battery_count + 1
table.insert(percents, charge)
end
f:close()
end
end

-- Read status file of first battery
local f = io.open(battery_paths[1] .. "/status", "r")
local status
if f then
-- Read line (without newline character, with a default of unknown), to lowercase
status = (f:read("l") or "unknown"):lower()
f:close()
else
status = "unknown"
end
battery_status.ac_power = status_to_ac_power[status]
-- Choose a percent
local chosen_percent = BC.battery_chooser(percents, config.current.multiple_battery_selection)
battery_status.percent_charge_remaining = chosen_percent
-- Set battery count
battery_status.battery_count = battery_count
else
battery_status.ac_power = true
battery_status.percent_charge_remaining = 100
battery_status.battery_count = path_count
end
end

local function get_battery_info_job(battery_status)
return J:new({
-- Find symbolic links in /sys/class/power_supply that start with BAT
-- These are the directories containing information files for each battery
command = "find",
args = {
"/sys/class/power_supply/",
"-type", "l",
"-name", "BAT*",
},
on_exit = function (j, return_value)
if return_value == 0 then
parse_powersupply_battery_info(j:result(), battery_status)
log.debug(vim.inspect(battery_status))
else
log.error(vim.inspect(j:result()))
end
end
})
end

return {
get_battery_info_job = get_battery_info_job
}
10 changes: 10 additions & 0 deletions lua/util/file.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
local bit = require('bit')

local function is_readable_directory(file)
local s = vim.loop.fs_stat(file)
return s ~= nil and s.type == 'directory' and bit.band(s.mode, 4) == 4
end

return {
is_readable_directory = is_readable_directory,
}

0 comments on commit aa2f8ca

Please sign in to comment.