Skip to content

Commit

Permalink
feat: add support for Termux (#45)
Browse files Browse the repository at this point in the history
* feat(parsers): move all parsers to their own submodule (#43)

BREAKING CHANGE: Parser modules have been moved

Add `check` functions to all parser modules

* fix: Termux compatible `is_readable_directory` function (#42)

* feat(util): use test command to check directory

* fix: use single quotes in util/file.lua

* feat(util): add type hints and documentation, remove old function

* feat(parsers): move all parsers to their own submodule (#43)

BREAKING CHANGE: Parser modules have been moved

Add `check` functions to all parser modules

* feat(util): use old "readable directory" method when not in Termux

* feat(parsers): add termux-api parser

* fix(parsers/termux-api): use correct field on status json
  • Loading branch information
Agent-E11 authored Jul 12, 2024
1 parent 05e172a commit 0e4575f
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
1 change: 1 addition & 0 deletions lua/battery/parsers/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ M.parsers = {
pmset = require('battery.parsers.pmset'),
powersupply = require('battery.parsers.powersupply'),
acpi = require('battery.parsers.acpi'),
termux_api = require('battery.parsers.termux-api'),
}

return M
43 changes: 43 additions & 0 deletions lua/battery/parsers/termux-api.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
-- Getting battery info with termux-battery-status. Requires Termux and Termux:API.
local M = {}

local J = require('plenary.job')
local L = require('plenary.log')

local log = L.new({ plugin = 'battery' })

local function parse_termux_battery_info(result, battery_status)
local status = vim.json.decode(table.concat(result, ''))
log.debug(vim.inspect(status))

battery_status.percent_charge_remaining = status.percentage
battery_status.battery_count = 1 -- WARN: This might not always be true
battery_status.ac_power = status.plugged:find("^PLUGGED_") -- String starts with "PLUGGED_"
log.debug(vim.inspect(battery_status))
end

-- Create a plenary job to get the battery info
-- battery_status is a table to store the results in
function M.get_battery_info_job(battery_status)
return J:new({
command = 'termux-battery-status',
on_exit = function(r, return_value)
if return_value == 0 then
parse_termux_battery_info(r:result(), battery_status)
log.debug(vim.inspect(battery_status))
else
vim.schedule(function()
vim.notify('battery.nvim: Error getting battery info with termux-battery-status', vim.log.levels.ERROR)
end)
end
end,
})
end

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

return M

0 comments on commit 0e4575f

Please sign in to comment.