Skip to content

Commit

Permalink
Attempt to fix WSL Support (#56)
Browse files Browse the repository at this point in the history
* attempt to fix wsl

* attempt to fix wsl; added missing comma

* attempt to fix wsl; added -Command

* attempt to fix wsl; added -Command

* attempt to fix wsl; added -Command
  • Loading branch information
artjsalina5 authored Jan 6, 2025
1 parent 5b0fc97 commit 59f8c0a
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
test/vendor/*/
doc/tags
.vs/
1 change: 1 addition & 0 deletions lua/battery/parsers/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ M.parsers = {
powersupply = require('battery.parsers.powersupply'),
acpi = require('battery.parsers.acpi'),
termux_api = require('battery.parsers.termux-api'),
wsl = require('battery.parsers.wsl'),
}

return M
2 changes: 1 addition & 1 deletion lua/battery/parsers/powersupply.lua
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ end
---Check if this parser would work in the current environment
---@return boolean
function M.check()
return file.is_readable_directory('/sys/class/power_supply/')
return file.is_readable_directory('/sys/class/power_supply/') and vim.fn.has('wsl') == 0
end

return M
54 changes: 54 additions & 0 deletions lua/battery/parsers/wsl.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
-- Get battery info using PowerShell in WSL. Requires Windows Subsystem for Linux (WSL)

local M = {}

local J = require('plenary.job')
local L = require('plenary.log')
local log = L.new({ plugin = 'battery' })

local get_battery_info_powershell_command = {
'Get-WmiObject -Class Win32_Battery | Select-Object -ExpandProperty EstimatedChargeRemaining',
}

---Parse the response from the battery info job and update
---the battery status
---@param result string[]
---@param battery_status BatteryStatus
local function parse_wsl_battery_info(result, battery_status)
log.debug("WSL Battery Info Result: ", vim.inspect(result))
local battery_info = result[1] and result[1]:match('%d+')
if battery_info then
battery_status.percent_charge_remaining = tonumber(battery_info)
battery_status.ac_power = false
battery_status.battery_count = 1
else
battery_status.percent_charge_remaining = 100
battery_status.ac_power = true
battery_status.battery_count = 0
end
end

---@param battery_status BatteryStatus
---@return unknown # Plenary job
function M.get_battery_info_job(battery_status)
return J:new({
command = '/mnt/c/Windows/System32/WindowsPowerShell/v1.0/powershell.exe',
args = { '-Command', table.concat(get_battery_info_powershell_command, ' ') },
on_exit = function(j, return_value)
if return_value == 0 then
parse_wsl_battery_info(j:result(), battery_status)
log.debug(vim.inspect(battery_status))
else
log.error(vim.inspect(j:result()))
end
end,
})
end

---Check if this parser would work in the current environment
---@return boolean
function M.check()
return vim.fn.has 'wsl' == 1
end

return M

0 comments on commit 59f8c0a

Please sign in to comment.