Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
O
oh-my-zsh
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
JIRA
JIRA
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
github
oh-my-zsh
Commits
dc83d0b7
Unverified
Commit
dc83d0b7
authored
Oct 07, 2021
by
Ryan Voots
Committed by
GitHub
Oct 07, 2021
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix(battery): support `acpitool` and multiple batteries under Linux (#9609)
Co-authored-by:
Marc Cornellà
<
hello@mcornella.com
>
parent
607d2f1e
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
30 additions
and
20 deletions
+30
-20
battery.plugin.zsh
plugins/battery/battery.plugin.zsh
+30
-20
No files found.
plugins/battery/battery.plugin.zsh
View file @
dc83d0b7
...
@@ -12,15 +12,12 @@
...
@@ -12,15 +12,12 @@
###########################################
###########################################
if
[[
"
$OSTYPE
"
=
darwin
*
]]
;
then
if
[[
"
$OSTYPE
"
=
darwin
*
]]
;
then
function
battery_is_charging
()
{
function
battery_is_charging
()
{
ioreg
-rc
AppleSmartBattery |
command grep
-q
'^.*"ExternalConnected"\ =\ Yes'
ioreg
-rc
AppleSmartBattery |
command grep
-q
'^.*"ExternalConnected"\ =\ Yes'
}
}
function
battery_pct
()
{
function
battery_pct
()
{
pmset
-g
batt |
grep
-Eo
"
\d
+%"
|
cut
-d
%
-f1
pmset
-g
batt |
grep
-Eo
"
\d
+%"
|
cut
-d
%
-f1
}
}
function
battery_pct_remaining
()
{
function
battery_pct_remaining
()
{
if
battery_is_charging
;
then
if
battery_is_charging
;
then
echo
"External Power"
echo
"External Power"
...
@@ -28,7 +25,6 @@ if [[ "$OSTYPE" = darwin* ]]; then
...
@@ -28,7 +25,6 @@ if [[ "$OSTYPE" = darwin* ]]; then
battery_pct
battery_pct
fi
fi
}
}
function
battery_time_remaining
()
{
function
battery_time_remaining
()
{
local
smart_battery_status
=
"
$(
ioreg
-rc
"AppleSmartBattery"
)
"
local
smart_battery_status
=
"
$(
ioreg
-rc
"AppleSmartBattery"
)
"
if
[[
$(
echo
$smart_battery_status
|
command grep
-c
'^.*"ExternalConnected"\ =\ No'
)
-eq
1
]]
;
then
if
[[
$(
echo
$smart_battery_status
|
command grep
-c
'^.*"ExternalConnected"\ =\ No'
)
-eq
1
]]
;
then
...
@@ -42,7 +38,6 @@ if [[ "$OSTYPE" = darwin* ]]; then
...
@@ -42,7 +38,6 @@ if [[ "$OSTYPE" = darwin* ]]; then
echo
"∞"
echo
"∞"
fi
fi
}
}
function
battery_pct_prompt
()
{
function
battery_pct_prompt
()
{
local
battery_pct color
local
battery_pct color
if
ioreg
-rc
AppleSmartBattery |
command grep
-q
'^.*"ExternalConnected"\ =\ No'
;
then
if
ioreg
-rc
AppleSmartBattery |
command grep
-q
'^.*"ExternalConnected"\ =\ No'
;
then
...
@@ -61,17 +56,14 @@ if [[ "$OSTYPE" = darwin* ]]; then
...
@@ -61,17 +56,14 @@ if [[ "$OSTYPE" = darwin* ]]; then
}
}
elif
[[
"
$OSTYPE
"
=
freebsd
*
]]
;
then
elif
[[
"
$OSTYPE
"
=
freebsd
*
]]
;
then
function
battery_is_charging
()
{
function
battery_is_charging
()
{
[[
$(
sysctl
-n
hw.acpi.battery.state
)
-eq
2
]]
[[
$(
sysctl
-n
hw.acpi.battery.state
)
-eq
2
]]
}
}
function
battery_pct
()
{
function
battery_pct
()
{
if
((
$+
commands[sysctl]
))
;
then
if
((
$+
commands[sysctl]
))
;
then
sysctl
-n
hw.acpi.battery.life
sysctl
-n
hw.acpi.battery.life
fi
fi
}
}
function
battery_pct_remaining
()
{
function
battery_pct_remaining
()
{
if
!
battery_is_charging
;
then
if
!
battery_is_charging
;
then
battery_pct
battery_pct
...
@@ -79,7 +71,6 @@ elif [[ "$OSTYPE" = freebsd* ]]; then
...
@@ -79,7 +71,6 @@ elif [[ "$OSTYPE" = freebsd* ]]; then
echo
"External Power"
echo
"External Power"
fi
fi
}
}
function
battery_time_remaining
()
{
function
battery_time_remaining
()
{
local
remaining_time
local
remaining_time
remaining_time
=
$(
sysctl
-n
hw.acpi.battery.time
)
remaining_time
=
$(
sysctl
-n
hw.acpi.battery.time
)
...
@@ -89,7 +80,6 @@ elif [[ "$OSTYPE" = freebsd* ]]; then
...
@@ -89,7 +80,6 @@ elif [[ "$OSTYPE" = freebsd* ]]; then
printf
%02d:%02d
$hour
$minute
printf
%02d:%02d
$hour
$minute
fi
fi
}
}
function
battery_pct_prompt
()
{
function
battery_pct_prompt
()
{
local
battery_pct color
local
battery_pct color
battery_pct
=
$(
battery_pct_remaining
)
battery_pct
=
$(
battery_pct_remaining
)
...
@@ -106,19 +96,42 @@ elif [[ "$OSTYPE" = freebsd* ]]; then
...
@@ -106,19 +96,42 @@ elif [[ "$OSTYPE" = freebsd* ]]; then
echo
"%{
$fg
[
$color
]%}
${
battery_pct
}
%%%{
$reset_color
%}"
echo
"%{
$fg
[
$color
]%}
${
battery_pct
}
%%%{
$reset_color
%}"
fi
fi
}
}
elif
[[
"
$OSTYPE
"
=
linux
*
]]
;
then
elif
[[
"
$OSTYPE
"
=
linux
*
]]
;
then
function
battery_is_charging
()
{
function
battery_is_charging
()
{
!
acpi 2>/dev/null |
command grep
-v
"rate information unavailable"
|
command grep
-q
'^Battery.*Discharging'
if
((
$+
commands[acpitool]
))
;
then
!
acpitool 2>/dev/null |
command grep
-qE
'^\s+Battery.*Discharging'
elif
((
$+
commands[acpi]
))
;
then
!
acpi 2>/dev/null |
command grep
-v
"rate information unavailable"
|
command grep
-q
'^Battery.*Discharging'
fi
}
}
function
battery_pct
()
{
function
battery_pct
()
{
if
((
$+
commands[acpi]
))
;
then
if
((
$+
commands[acpitool]
))
;
then
acpi 2>/dev/null |
command grep
-v
"rate information unavailable"
|
command grep
-E
'^Battery.*(Full|(Disc|C)harging)'
|
cut
-f2
-d
','
|
tr
-cd
'[:digit:]'
# Sample output:
# Battery #1 : Unknown, 99.55%
# Battery #2 : Discharging, 49.58%, 01:12:05
# All batteries : 62.60%, 02:03:03
acpitool 2>/dev/null |
command awk
-F
,
'
/^\s+All batteries/ {
gsub(/[^0-9.]/, "", $1)
pct=$1
exit
}
!pct && /^\s+Battery/ {
gsub(/[^0-9.]/, "", $2)
pct=$2
}
END { print pct }
'
elif
((
$+
commands[acpi]
))
;
then
# Sample output:
# Battery 0: Discharging, 0%, rate information unavailable
# Battery 1: Full, 100%
acpi 2>/dev/null |
command awk
-F
,
'
/rate information unavailable/ { next }
/^Battery.*: /{ gsub(/[^0-9]/, "", $2); print $2; exit }
'
fi
fi
}
}
function
battery_pct_remaining
()
{
function
battery_pct_remaining
()
{
if
!
battery_is_charging
;
then
if
!
battery_is_charging
;
then
battery_pct
battery_pct
...
@@ -126,13 +139,11 @@ elif [[ "$OSTYPE" = linux* ]]; then
...
@@ -126,13 +139,11 @@ elif [[ "$OSTYPE" = linux* ]]; then
echo
"External Power"
echo
"External Power"
fi
fi
}
}
function
battery_time_remaining
()
{
function
battery_time_remaining
()
{
if
!
battery_is_charging
;
then
if
!
battery_is_charging
;
then
acpi 2>/dev/null |
command grep
-v
"rate information unavailable"
|
cut
-f3
-d
','
acpi 2>/dev/null |
command grep
-v
"rate information unavailable"
|
cut
-f3
-d
','
fi
fi
}
}
function
battery_pct_prompt
()
{
function
battery_pct_prompt
()
{
local
battery_pct color
local
battery_pct color
battery_pct
=
$(
battery_pct_remaining
)
battery_pct
=
$(
battery_pct_remaining
)
...
@@ -149,7 +160,6 @@ elif [[ "$OSTYPE" = linux* ]]; then
...
@@ -149,7 +160,6 @@ elif [[ "$OSTYPE" = linux* ]]; then
echo
"%{
$fg
[
$color
]%}
${
battery_pct
}
%%%{
$reset_color
%}"
echo
"%{
$fg
[
$color
]%}
${
battery_pct
}
%%%{
$reset_color
%}"
fi
fi
}
}
else
else
# Empty functions so we don't cause errors in prompts
# Empty functions so we don't cause errors in prompts
function
battery_is_charging
{
false
}
function
battery_is_charging
{
false
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment