From 6498ae13c9b122aa69feab4e097a57bc58f74884 Mon Sep 17 00:00:00 2001 From: Breizh Date: Fri, 8 Mar 2024 21:48:38 +0100 Subject: [PATCH 1/4] Actualiser .i3blocks/batterybar --- .i3blocks/batterybar | 77 ++++++++++++++++++++++++++++++++++++-------- 1 file changed, 64 insertions(+), 13 deletions(-) diff --git a/.i3blocks/batterybar b/.i3blocks/batterybar index e74dca8..406dcb4 100755 --- a/.i3blocks/batterybar +++ b/.i3blocks/batterybar @@ -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="${status} ${percentage}% ${remaining}" message=${message% } else - message="$squares" + message="$squares ${remaining}" fi # Affichage echo "bat $message" -echo "bat ${percentage}%" - +echo "bat ${percentage}%" \ No newline at end of file From 98d40925ca7d2cd53bb69e0d7fed40789f9c1303 Mon Sep 17 00:00:00 2001 From: Breizh Date: Fri, 8 Mar 2024 22:13:25 +0100 Subject: [PATCH 2/4] Actualiser .i3blocks/batterybar --- .i3blocks/batterybar | 46 +++++++++++++++++++++++++++----------------- 1 file changed, 28 insertions(+), 18 deletions(-) diff --git a/.i3blocks/batterybar b/.i3blocks/batterybar index 406dcb4..fdd1d93 100755 --- a/.i3blocks/batterybar +++ b/.i3blocks/batterybar @@ -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 n’indique pas qu’elle +# est chargée d’elle-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 ${percentage}%" \ No newline at end of file +echo "bat ${percentage}%" From cad4ac1b6539dd92d4bb7983862be282026b8586 Mon Sep 17 00:00:00 2001 From: Breizh Date: Fri, 8 Mar 2024 23:18:57 +0100 Subject: [PATCH 3/4] Actualiser .i3blocks/batterybar --- .i3blocks/batterybar | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/.i3blocks/batterybar b/.i3blocks/batterybar index fdd1d93..e634cf0 100755 --- a/.i3blocks/batterybar +++ b/.i3blocks/batterybar @@ -17,9 +17,9 @@ human_time() { source /sys/class/power_supply/BAT1/uevent time="$(date +%s)" -# Min et max hardcodés (20 et 80 % ici) -MIN=1112600 -MAX=4452000 +# 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 @@ -62,6 +62,8 @@ else remaining_secs=0 fi +[[ "$remaining_secs" -lt 0 ]] && remaining_secs=0 + remaining="$(human_time "$remaining_secs")" # Définition des couleurs @@ -143,7 +145,7 @@ case "${status}" in color="$ac_color" ;; "Discharging"|"Unknown") - if (( percentage >= 0 && percentage < 10 )); then + if (( percentage < 10 )); then color="${dis_colors[0]}" elif (( percentage >= 10 && percentage < 20 )); then color="${dis_colors[1]}" @@ -175,3 +177,4 @@ fi # Affichage echo "bat $message" echo "bat ${percentage}%" +declare -p MIN MAX percents percentage >&2 From c0aa471b7be3b573fb35fcc61471b0d0e2de5404 Mon Sep 17 00:00:00 2001 From: Breizh Date: Mon, 11 Mar 2024 12:36:54 +0100 Subject: [PATCH 4/4] Actualiser .i3blocks/batterybar --- .i3blocks/batterybar | 43 ++++++------------------------------------- 1 file changed, 6 insertions(+), 37 deletions(-) diff --git a/.i3blocks/batterybar b/.i3blocks/batterybar index e634cf0..5099f02 100755 --- a/.i3blocks/batterybar +++ b/.i3blocks/batterybar @@ -82,49 +82,18 @@ percents=$(( percentage / 2 )) ## à 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 )) for t in $(seq 1 $full_squares) do - squares+="█" + squares+="${chars[0]}" done -if [[ $(( percents - ( full_squares * 8 ) )) -eq 7 ]] -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 +squares+="${chars[$(( percents - ( full_squares * 8 ) ))]}" # Complétion de la barre afin d'avoir une taille fixe blanks=$(( 6 - ${#squares} ))