dotfiles/.i3blocks/batterybar

150 lines
3.8 KiB
Plaintext
Raw Normal View History

2020-01-16 18:03:20 +01:00
#!/bin/bash
2018-03-06 18:49:41 +01:00
2024-03-08 21:48:38 +01:00
runtime="${XDG_RUNTIME_DIR}/i3blocks"
[[ ! -d "$runtime" ]] && mkdir -p "$runtime"
human_time() {
local seconds="${1}"
local minutes=$(( seconds / 60 ))
local hours=$(( minutes / 60 ))
minutes=$(( minutes % 60 ))
printf "%d:%02d\n" "$hours" "$minutes"
return
}
2020-01-16 18:03:20 +01:00
# Récupération des infos de la batterie
2018-03-06 18:49:41 +01:00
2024-03-08 21:48:38 +01:00
source /sys/class/power_supply/BAT1/uevent
2024-03-08 22:13:25 +01:00
time="$(date +%s)"
2024-03-08 23:18:57 +01:00
# Min et max personnalisés (20 et 80% ici)
MIN="$(( POWER_SUPPLY_CHARGE_FULL * 2 / 10 ))"
MAX="$(( POWER_SUPPLY_CHARGE_FULL * 8 / 10 ))"
2024-03-08 22:13:25 +01:00
# Utiliser toute la plage de la batterie
#MIN=0
#MAX="$POWER_SUPPLY_CHARGE_FULL"
2024-03-08 21:48:38 +01:00
# %age réel
#percentage="$POWER_SUPPLY_CAPACITY"
2024-03-08 22:13:25 +01:00
# %age relatif aux MIN/MAX
percentage=$(( (POWER_SUPPLY_CHARGE_NOW - MIN) * 100 / (MAX - MIN) ))
# État de la batterie
2024-03-08 21:48:38 +01:00
status="$POWER_SUPPLY_STATUS"
2024-03-08 22:13:25 +01:00
# 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 ]]
2024-03-08 21:48:38 +01:00
then
status="Full"
fi
if [[ -f "${runtime}/bat" ]]
then
mapfile -t old < "${runtime}/bat"
fi
2024-03-08 22:13:25 +01:00
# Estimation naïve du temps restant
2024-03-08 21:48:38 +01:00
if [[ "${old[0]}" != "$status" ]]
then
printf "%s\n" "$status" "$time" "$POWER_SUPPLY_CHARGE_NOW" > "${runtime}/bat"
fi
if [[ "$status" == "Discharging" ]]
then
2024-03-08 22:13:25 +01:00
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])) ))"
2024-03-08 21:48:38 +01:00
else
2024-03-08 22:13:25 +01:00
remaining_secs=0
2024-03-08 21:48:38 +01:00
fi
2024-03-08 23:18:57 +01:00
[[ "$remaining_secs" -lt 0 ]] && remaining_secs=0
2024-03-08 21:48:38 +01:00
remaining="$(human_time "$remaining_secs")"
2018-11-10 05:22:08 +01:00
2020-01-16 18:03:20 +01:00
# Définition des couleurs
dis_colors=("#F2777A" "#F2777A" "#F99157" "#F99157" "#FFCC66" "#FFCC66" "#99CC99" "#99CC99")
2018-03-06 18:49:41 +01:00
charging_color="#66CCCC"
ac_color="#6699CC"
2020-01-16 18:03:20 +01:00
full_color="#D3D0C8"
2018-03-06 18:49:41 +01:00
2024-03-08 21:48:38 +01:00
# Création de la barre « graphique », 48 états affichables
2024-03-08 22:13:25 +01:00
## à partir du pourcentage de calculé plus tôt
2024-03-08 21:48:38 +01:00
2024-03-08 22:13:25 +01:00
percents=$(( percentage / 2 ))
(( percents > 48 )) && percents=48
2024-03-08 21:48:38 +01:00
## à partir des données brutes
2024-03-08 22:13:25 +01:00
# percents=$(( (POWER_SUPPLY_CHARGE_NOW - MIN) * 48 / (MAX - MIN) ))
2024-03-08 21:48:38 +01:00
2024-03-11 12:36:54 +01:00
# Définition des symboles
chars=("█" "░" "░" "▒" "▒" "▓" "▓" "█")
#chars=("█" "▏" "▎" "▍" "▌" "▋" "▊" "▉")
#chars=("█" "▁" "▂" "▃" "▄" "▅" "▆" "▇")
2018-03-06 18:49:41 +01:00
2020-01-16 18:03:20 +01:00
full_squares=$(( percents / 8 ))
for t in $(seq 1 $full_squares)
do
2024-03-11 12:36:54 +01:00
squares+="${chars[0]}"
2018-03-06 18:49:41 +01:00
done
2024-03-11 12:36:54 +01:00
squares+="${chars[$(( percents - ( full_squares * 8 ) ))]}"
2019-01-21 08:16:41 +01:00
2020-01-16 18:03:20 +01:00
# Complétion de la barre afin d'avoir une taille fixe
blanks=$(( 6 - ${#squares} ))
for t in $(seq 1 $blanks)
2018-03-06 18:49:41 +01:00
do
2020-01-16 18:03:20 +01:00
squares+=" "
done
2018-03-06 18:49:41 +01:00
2020-01-16 18:03:20 +01:00
# Choix de la couleur
case "${status}" in
"Charging")
color="$charging_color"
;;
"Full")
color="$full_color"
;;
"AC")
color="$ac_color"
;;
"Discharging"|"Unknown")
2024-03-08 23:18:57 +01:00
if (( percentage < 10 )); then
2020-01-16 18:03:20 +01:00
color="${dis_colors[0]}"
elif (( percentage >= 10 && percentage < 20 )); then
color="${dis_colors[1]}"
elif (( percentage >= 20 && percentage < 30 )); then
color="${dis_colors[2]}"
elif (( percentage >= 30 && percentage < 40 )); then
color="${dis_colors[3]}"
elif (( percentage >= 40 && percentage < 60 )); then
color="${dis_colors[4]}"
elif (( percentage >= 60 && percentage < 70 )); then
color="${dis_colors[5]}"
elif (( percentage >= 70 && percentage < 80 )); then
color="${dis_colors[6]}"
elif (( percentage >= 80 )); then
color="${dis_colors[7]}"
2018-03-06 18:49:41 +01:00
fi
2020-01-16 18:03:20 +01:00
;;
esac
2018-03-06 18:49:41 +01:00
2020-01-16 18:03:20 +01:00
# Format détaillé en cas de clic
if [[ -n "$BLOCK_BUTTON" ]]
then
message="<span foreground=\"$color\">${status} ${percentage}%</span> ${remaining}"
message=${message% }
else
2024-03-08 21:48:38 +01:00
message="<span foreground=\"$color\" background=\"#515151\">$squares</span> ${remaining}"
2020-01-16 18:03:20 +01:00
fi
2018-03-06 18:49:41 +01:00
2020-01-16 18:03:20 +01:00
# Affichage
echo "bat $message"
2024-03-08 22:13:25 +01:00
echo "bat <span foreground=\"$color\">${percentage}%</span>"
2024-03-08 23:18:57 +01:00
declare -p MIN MAX percents percentage >&2