Merge branch 'master' of git.breizh.pm:Breizh/dotfiles

This commit is contained in:
Breizh 2024-04-03 01:02:50 +02:00
commit 49e1e1f0f1

View file

@ -1,13 +1,70 @@
#!/bin/bash #!/bin/bash
# Récupération des infos de la batterie runtime="${XDG_RUNTIME_DIR}/i3blocks"
readarray -t batteries <<< $(acpi battery) [[ ! -d "$runtime" ]] && mkdir -p "$runtime"
battery=${batteries[${BLOCK_INSTANCE:-0}]}
# Stockage en variables human_time() {
percentage=$(echo $battery | grep -o -m1 '[0-9]\{1,3\}%' | tr -d '%') local seconds="${1}"
status=$(echo $battery | grep -E -o -m1 ': [[:alpha:]]*,' | tr -cd '[:alpha:]') local minutes=$(( seconds / 60 ))
remaining=$(echo $battery | grep -E -o -m1 '[0-9]{2}:[0-9]{2}') 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
time="$(date +%s)"
# Min et max personnalisés (20 et 80% ici)
MIN="$(( POWER_SUPPLY_CHARGE_FULL * 2 / 10 ))"
MAX="$(( POWER_SUPPLY_CHARGE_FULL * 8 / 10 ))"
# Utiliser toute la plage de la batterie
#MIN=0
#MAX="$POWER_SUPPLY_CHARGE_FULL"
# %age réel
#percentage="$POWER_SUPPLY_CAPACITY"
# %age relatif aux MIN/MAX
percentage=$(( (POWER_SUPPLY_CHARGE_NOW - MIN) * 100 / (MAX - MIN) ))
# État de la batterie
status="$POWER_SUPPLY_STATUS"
# 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
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 - 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=0
fi
[[ "$remaining_secs" -lt 0 ]] && remaining_secs=0
remaining="$(human_time "$remaining_secs")"
# Définition des couleurs # Définition des couleurs
dis_colors=("#F2777A" "#F2777A" "#F99157" "#F99157" "#FFCC66" "#FFCC66" "#99CC99" "#99CC99") dis_colors=("#F2777A" "#F2777A" "#F99157" "#F99157" "#FFCC66" "#FFCC66" "#99CC99" "#99CC99")
@ -15,52 +72,28 @@ charging_color="#66CCCC"
ac_color="#6699CC" ac_color="#6699CC"
full_color="#D3D0C8" full_color="#D3D0C8"
# Création de la barre « graphique » # Création de la barre « graphique », 48 états affichables
## à partir du pourcentage de calculé plus tôt
percents=$(( percentage / 2 )) percents=$(( percentage / 2 ))
(( percents > 48 )) && percents=48 (( percents > 48 )) && percents=48
## à partir des données brutes
# percents=$(( (POWER_SUPPLY_CHARGE_NOW - MIN) * 48 / (MAX - MIN) ))
# Définition des symboles
chars=("█" "░" "░" "▒" "▒" "▓" "▓" "█")
#chars=("█" "▏" "▎" "▍" "▌" "▋" "▊" "▉")
#chars=("█" "▁" "▂" "▃" "▄" "▅" "▆" "▇")
full_squares=$(( percents / 8 )) full_squares=$(( percents / 8 ))
for t in $(seq 1 $full_squares) for t in $(seq 1 $full_squares)
do do
squares+="█" squares+="${chars[0]}"
done done
if [[ $(( percents - ( full_squares * 8 ) )) -eq 7 ]] squares+="${chars[$(( percents - ( full_squares * 8 ) ))]}"
then
#squares+="█"
squares+="▉"
#squares+="▇"
elif [[ $(( percents - ( full_squares * 8 ) )) -eq 6 ]]
then
#squares+="▓"
squares+="▊"
#squares+="▆"
elif [[ $(( percents - ( full_squares * 8 ) )) -eq 5 ]]
then
#squares+="▓"
squares+="▋"
#squares+="▅"
elif [[ $(( percents - ( full_squares * 8 ) )) -eq 4 ]]
then
#squares+="▒"
squares+="▌"
#squares+="▄"
elif [[ $(( percents - ( full_squares * 8 ) )) -eq 3 ]]
then
#squares+="▒"
squares+="▍"
#squares+="▃"
elif [[ $(( percents - ( full_squares * 8 ) )) -eq 2 ]]
then
#squares+="░"
squares+="▎"
#squares+="▂"
elif [[ $(( percents - ( full_squares * 8 ) )) -eq 1 ]]
then
#squares+="░"
squares+="▏"
#squares+="▁"
fi
# Complétion de la barre afin d'avoir une taille fixe # Complétion de la barre afin d'avoir une taille fixe
blanks=$(( 6 - ${#squares} )) blanks=$(( 6 - ${#squares} ))
@ -81,7 +114,7 @@ case "${status}" in
color="$ac_color" color="$ac_color"
;; ;;
"Discharging"|"Unknown") "Discharging"|"Unknown")
if (( percentage >= 0 && percentage < 10 )); then if (( percentage < 10 )); then
color="${dis_colors[0]}" color="${dis_colors[0]}"
elif (( percentage >= 10 && percentage < 20 )); then elif (( percentage >= 10 && percentage < 20 )); then
color="${dis_colors[1]}" color="${dis_colors[1]}"
@ -107,10 +140,10 @@ then
message="<span foreground=\"$color\">${status} ${percentage}%</span> ${remaining}" message="<span foreground=\"$color\">${status} ${percentage}%</span> ${remaining}"
message=${message% } message=${message% }
else else
message="<span foreground=\"$color\" background=\"#515151\">$squares</span>" message="<span foreground=\"$color\" background=\"#515151\">$squares</span> ${remaining}"
fi fi
# Affichage # Affichage
echo "bat $message" echo "bat $message"
echo "bat <span foreground=\"$color\">${percentage}%</span>" echo "bat <span foreground=\"$color\">${percentage}%</span>"
declare -p MIN MAX percents percentage >&2