From 6c9565ccb247b94fe016a9622a4e66392ae46f27 Mon Sep 17 00:00:00 2001 From: chenyc Date: Fri, 3 Mar 2023 17:28:14 +0800 Subject: [PATCH] =?UTF-8?q?feat=20=E6=8F=90=E5=8D=87=E7=94=B5=E6=B1=A0?= =?UTF-8?q?=E7=94=B5=E9=87=8F=E8=84=9A=E6=9C=AC=E5=85=BC=E5=AE=B9=E6=80=A7?= =?UTF-8?q?=20acpi=20=E6=88=96=20upower=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DEF/statusbar/packages/bat.sh | 29 ++++++++++++++++++++--------- 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/DEF/statusbar/packages/bat.sh b/DEF/statusbar/packages/bat.sh index 13683821..a92d0da4 100644 --- a/DEF/statusbar/packages/bat.sh +++ b/DEF/statusbar/packages/bat.sh @@ -1,5 +1,6 @@ #! /bin/bash # 电池电量 +# 需要安装acpi或者upower tempfile=$(cd $(dirname $0);cd ..;pwd)/temp @@ -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=""; @@ -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() {