Actualiser .i3blocks/batterybar

This commit is contained in:
Breizh 2024-03-08 21:48:38 +01:00
parent 692b8f065f
commit f56a148735

View file

@ -1,13 +1,53 @@
#!/bin/bash
# Récupération des infos de la batterie
readarray -t batteries <<< $(acpi battery)
battery=${batteries[${BLOCK_INSTANCE:-0}]}
runtime="${XDG_RUNTIME_DIR}/i3blocks"
[[ ! -d "$runtime" ]] && mkdir -p "$runtime"
# Stockage en variables
percentage=$(echo $battery | grep -o -m1 '[0-9]\{1,3\}%' | tr -d '%')
status=$(echo $battery | grep -E -o -m1 ': [[:alpha:]]*,' | tr -cd '[:alpha:]')
remaining=$(echo $battery | grep -E -o -m1 '[0-9]{2}:[0-9]{2}')
human_time() {
local seconds="${1}"
local minutes=$(( seconds / 60 ))
local hours=$(( minutes / 60 ))
minutes=$(( minutes % 60 ))
printf "%d:%02d\n" "$hours" "$minutes"
return
}
# Récupération des infos de la batterie
source /sys/class/power_supply/BAT1/uevent
# %age réel
#percentage="$POWER_SUPPLY_CAPACITY"
# %age relatif aux 80 % limités dans le BIOS
percentage=$(( POWER_SUPPLY_CHARGE_NOW * 100 / 4450400 ))
status="$POWER_SUPPLY_STATUS"
if [[ "$status" == "Charging" && "$percentage" -eq 80 ]]
then
status="Full"
fi
time="$(date +%s)"
if [[ -f "${runtime}/bat" ]]
then
mapfile -t old < "${runtime}/bat"
fi
if [[ "${old[0]}" != "$status" ]]
then
printf "%s\n" "$status" "$time" "$POWER_SUPPLY_CHARGE_NOW" > "${runtime}/bat"
fi
if [[ "$status" == "Discharging" ]]
then
remaining_secs="$(( POWER_SUPPLY_CHARGE_NOW / ((old[2] - POWER_SUPPLY_CHARGE_NOW) / (time - old[1])) ))"
else
remaining_secs="$(( (4450400 - POWER_SUPPLY_CHARGE_NOW) / ((POWER_SUPPLY_CHARGE_NOW - old[2]) / (time - old[1])) ))"
fi
remaining="$(human_time "$remaining_secs")"
# Définition des couleurs
dis_colors=("#F2777A" "#F2777A" "#F99157" "#F99157" "#FFCC66" "#FFCC66" "#99CC99" "#99CC99")
@ -15,9 +55,21 @@ charging_color="#66CCCC"
ac_color="#6699CC"
full_color="#D3D0C8"
# Création de la barre « graphique »
percents=$(( percentage / 2 ))
(( percents > 48 )) && percents=48
# Création de la barre « graphique », 48 états affichables
## à partir du pourcentage de charge
#percents=$(( percentage / 2 ))
#(( percents > 48 )) && percents=48
## à partir des données brutes
### Relatif à 100%
#percents=$(( POWER_SUPPLY_CHARGE_NOW * 48 / POWER_SUPPLY_CHARGE_FULL ))
### Relatif à 80% (limite de charge du BIOS)
percents=$(( POWER_SUPPLY_CHARGE_NOW * 48 / 4450400 ))
full_squares=$(( percents / 8 ))
for t in $(seq 1 $full_squares)
@ -107,10 +159,9 @@ then
message="<span foreground=\"$color\">${status} ${percentage}%</span> ${remaining}"
message=${message% }
else
message="<span foreground=\"$color\" background=\"#515151\">$squares</span>"
message="<span foreground=\"$color\" background=\"#515151\">$squares</span> ${remaining}"
fi
# Affichage
echo "bat $message"
echo "bat <span foreground=\"$color\">${percentage}%</span>"