Skip to content

Commit

Permalink
feat 提升电池电量脚本兼容性 acpi 或 upower支持
Browse files Browse the repository at this point in the history
  • Loading branch information
yaocccc committed Mar 3, 2023
1 parent 463c77a commit 6c9565c
Showing 1 changed file with 20 additions and 9 deletions.
29 changes: 20 additions & 9 deletions DEF/statusbar/packages/bat.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#! /bin/bash
# 电池电量
# 需要安装acpi或者upower

tempfile=$(cd $(dirname $0);cd ..;pwd)/temp

Expand All @@ -8,13 +9,27 @@ icon_color="^c#3B001B^^b#4865660x88^"
text_color="^c#3B001B^^b#4865660x99^"
signal=$(echo "^s$this^" | sed 's/_//')

# check
[ ! "$(command -v acpi)" ] && echo command not found: acpi && exit

update() {
get_by_acpi() {
[ ! "$(command -v acpi)" ] && echo command not found: acpi && return
bat_text=$(acpi -b | sed '2,$d' | awk '{print $4}' | grep -Eo "[0-9]+")
[ ! "$bat_text" ] && bat_text=$(acpi -b | sed '2,$d' | awk -F'[ %]' '{print $5}' | grep -Eo "[0-9]+")
[ ! "$(acpi -b | grep 'Battery 0' | grep Discharging)" ] && charge_icon=""
_time="可用时间: $(acpi | sed 's/^Battery 0: //g' | awk -F ',' '{print $3}' | sed 's/^[ ]//g' | awk '{print $1}')"
[ "$_time" = "可用时间: " ] && _time=""
}

get_by_upower() {
[ ! "$(command -v upower)" ] && echo command not found: upower && return
bat=$(upower -e | grep BAT)
bat_text=$(upower -i $bat | awk '/percentage/ {print $2}' | grep -Eo '[0-9]+')
charge_icon=""
[ -z "$(upower -i $bat | grep 'state:.*fully-charged')" ] && charge_icon=""
}

update() {
get_by_acpi
get_by_upower
[ -z $bat_text ] && bat_text=0
if [ "$bat_text" -ge 95 ]; then bat_icon=""; charge_icon="";
elif [ "$bat_text" -ge 90 ]; then bat_icon="";
elif [ "$bat_text" -ge 80 ]; then bat_icon="";
Expand All @@ -36,11 +51,7 @@ update() {

notify() {
update
_status="状态: $(acpi | sed 's/^Battery 0: //g' | awk -F ',' '{print $1}')"
_remaining="剩余: $(acpi | sed 's/^Battery 0: //g' | awk -F ',' '{print $2}' | sed 's/^[ ]//g')"
_time="可用时间: $(acpi | sed 's/^Battery 0: //g' | awk -F ',' '{print $3}' | sed 's/^[ ]//g' | awk '{print $1}')"
[ "$_time" = "可用时间: " ] && _time=""
notify-send "$bat_icon Battery" "\n$_status\n$_remaining\n$_time" -r 9527
notify-send "$bat_icon Battery" "\n剩余: $bat_text%\n$_time" -r 9527
}

click() {
Expand Down

0 comments on commit 6c9565c

Please sign in to comment.