Actualiser .i3blocks/batterybar

This commit is contained in:
Breizh 2024-03-08 22:13:25 +01:00
parent 6498ae13c9
commit 98d40925ca

View file

@ -15,36 +15,51 @@ human_time() {
# Récupération des infos de la batterie
source /sys/class/power_supply/BAT1/uevent
time="$(date +%s)"
# Min et max hardcodés (20 et 80% ici)
MIN=1112600
MAX=4452000
# Utiliser toute la plage de la batterie
#MIN=0
#MAX="$POWER_SUPPLY_CHARGE_FULL"
# %age réel
#percentage="$POWER_SUPPLY_CAPACITY"
# %age relatif aux 80 % limités dans le BIOS
percentage=$(( POWER_SUPPLY_CHARGE_NOW * 100 / 4450400 ))
# %age relatif aux MIN/MAX
percentage=$(( (POWER_SUPPLY_CHARGE_NOW - MIN) * 100 / (MAX - MIN) ))
# État de la batterie
status="$POWER_SUPPLY_STATUS"
if [[ "$status" == "Charging" && "$percentage" -eq 80 ]]
# Indiquer la limite mise dans le BIOS, si la batterie nindique pas quelle
# est chargée delle-même
if [[ "$status" == "Charging" && "$POWER_SUPPLY_CAPACITY" -eq 80 ]]
then
status="Full"
fi
time="$(date +%s)"
if [[ -f "${runtime}/bat" ]]
then
mapfile -t old < "${runtime}/bat"
fi
# Estimation naïve du temps restant
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])) ))"
remaining_secs="$(( (POWER_SUPPLY_CHARGE_NOW - MIN) / ((old[2] - POWER_SUPPLY_CHARGE_NOW) / (time - old[1])) ))"
elif [[ "$status" == "Charging" ]]
then
remaining_secs="$(( (MAX - POWER_SUPPLY_CHARGE_NOW) / ((POWER_SUPPLY_CHARGE_NOW - old[2]) / (time - old[1])) ))"
else
remaining_secs="$(( (4450400 - POWER_SUPPLY_CHARGE_NOW) / ((POWER_SUPPLY_CHARGE_NOW - old[2]) / (time - old[1])) ))"
remaining_secs=0
fi
remaining="$(human_time "$remaining_secs")"
@ -57,18 +72,13 @@ full_color="#D3D0C8"
# Création de la barre « graphique », 48 états affichables
## à partir du pourcentage de charge
## à partir du pourcentage de calculé plus tôt
#percents=$(( percentage / 2 ))
#(( percents > 48 )) && percents=48
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 ))
# percents=$(( (POWER_SUPPLY_CHARGE_NOW - MIN) * 48 / (MAX - MIN) ))
full_squares=$(( percents / 8 ))
@ -164,4 +174,4 @@ fi
# Affichage
echo "bat $message"
echo "bat <span foreground=\"$color\">${percentage}%</span>"
echo "bat <span foreground=\"$color\">${percentage}%</span>"