Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
dubyte committed Dec 12, 2023
1 parent 66bdd56 commit 54ce820
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
# micro-battery
Display battery percentage for micro
Displays battery percentage on micro
46 changes: 46 additions & 0 deletions battery.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
VERSION = "0.0.1"

local micro = import("micro")
local shell = import("micro/shell")
local config = import("micro/config")

function init()
config.MakeCommand("battery", percentage, config.NoComplete)
config.TryBindKey("F7", "lua:battery.percentage", false)
end

-- Runs command and filter one line that has what is in contains
function RunCommandAndGrep(input, contains)
local ret, err = shell.RunCommand(input)
if err ~= nil then
micro.Log("Error while running " .. input)
end

local result = ""
for s in ret:gmatch("[^\r\n]+") do
if string.find(s, contains) then
result = s
end
end
return result
end

-- percentage print the battery percentage on the infobar as message.
-- The function emulate the next cli command
-- upower -i $(upower -e | grep BAT) | grep --color=never -E "percentage"
function percentage(bp)
-- get devices and select the battery one
local device, err = RunCommandAndGrep("upower -e", "BAT")
if err ~= nil then
micro.Log("Error while running upower")
end

-- query the device BATtery and filter the line with the percentage
local batteryPercentage, err = RunCommandAndGrep("upower -i " .. device, "percentage")
if err ~= nil then
micro.Log("Error while running upower: " .. err)
end

-- display Battery percentage: x%
micro.InfoBar():Message("Battery" .. batteryPercentage)
end
16 changes: 16 additions & 0 deletions help/battery.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# battery Plugin #

The battery plugin provides the user with the ability to display
the battery percentage.

Currently it depends on upower.

To initiate the function, you can either:

Press "F7"

Or run:

```
> battery
```
10 changes: 10 additions & 0 deletions info.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"name": "battery",
"description": "Show battery percentage",
"website": "",
"install": "",
"version": "0.0.1",
"require": [
"micro >= 2.0.0"
]
}
16 changes: 16 additions & 0 deletions repo.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[{
"Name": "battery",
"Description": "Shows battery percentage on infobar",
"Tags": ["upower", "laptop"],
"Website": "https://github.com/dubyte/battery-plugin",
"Versions": [
{
"Version": "0.0.1",
"Url": "https://github.com/micro-editor/battery-plugin/archive/v0.0.1.zip",
"Require": {
"micro": ">=2.0.0"
}
}
]
}]

0 comments on commit 54ce820

Please sign in to comment.