Mise à jour de '.i3blocks/batterybar'

This commit is contained in:
Breizh 2020-01-16 18:03:20 +01:00
parent 8ca0171773
commit 7f9e9a46ad

222
.i3blocks/batterybar Executable file → Normal file
View file

@ -1,196 +1,116 @@
#!/usr/bin/env bash #!/bin/bash
# batterybar; displays battery percentage as a bar on i3blocks
#
# Copyright 2015 Keftaa <adnan.37h@gmail.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301, USA.
#
#
readarray -t output <<< $(acpi battery)
battery_count=${#output[@]}
for line in "${output[@]}"; # Récupération des infos de la batterie
do readarray -t batteries <<< $(acpi battery)
percentages+=($(echo "$line" | grep -o -m1 '[0-9]\{1,3\}%' | tr -d '%')) battery=${batteries[${BLOCK_INSTANCE:-0}]}
statuses+=($(echo "$line" | egrep -o -m1 'Not|Discharging|Charging|AC|Full|Unknown'))
remaining=$(echo "$line" | egrep -o -m1 '[0-9][0-9]:[0-9][0-9]')
if [[ -n $remaining ]]; then
remainings+=(" ($remaining)")
else
remainings+=("")
fi
done
echo $statuses >&2 # 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}')
#There are 8 colors that reflect the current battery percentage when # Définition des couleurs
#discharging dis_colors=("#F2777A" "#F2777A" "#F99157" "#F99157" "#FFCC66" "#FFCC66" "#99CC99" "#99CC99")
dis_colors=("#F2777A" "#F2777A" "#F99157" "#F99157" "#FFCC66" "#FFCC66"
"#99CC99" "#99CC99")
charging_color="#66CCCC" charging_color="#66CCCC"
full_color="#D3D0C8"
ac_color="#6699CC" ac_color="#6699CC"
full_color="#D3D0C8"
# Création de la barre « graphique »
while getopts 1:2:3:4:5:6:7:8:c:f:a:h opt; do percents=$(( percentage / 2 ))
case "$opt" in
1) dis_colors[0]="$OPTARG";;
2) dis_colors[1]="$OPTARG";;
3) dis_colors[2]="$OPTARG";;
4) dis_colors[3]="$OPTARG";;
5) dis_colors[4]="$OPTARG";;
6) dis_colors[5]="$OPTARG";;
7) dis_colors[6]="$OPTARG";;
8) dis_colors[7]="$OPTARG";;
c) charging_color="$OPTARG";;
f) full_color="$OPTARG";;
a) ac_color="$OPTARG";;
h) printf "Usage: batterybar [OPTION] color
When discharging, there are 8 [1-8] levels colors.
You can specify custom colors, for example:
batterybar -1 red -2 \"#F6F6F6\" -8 green
You can also specify the colors for the charging, AC and
charged states:
batterybar -c green -f white -a \"#EEEEEE\"\n";
exit 0;
esac
done
squares=""
end=$(($battery_count - 1))
for i in $(seq 0 $end);
do
# if (( percentages[$i] > 0 && percentages[$i] < 20 )); then
# squares="█"
# elif (( percentages[$i] >= 20 && percentages[$i] < 40 )); then
# squares="██"
# elif (( percentages[$i] >= 40 && percentages[$i] < 60 )); then
# squares="███"
# elif (( percentages[$i] >= 60 && percentages[$i] < 80 )); then
# squares="████"
# elif (( percentages[$i] >=80 )); then
# squares="█████"
# fi
percents[$i]=$(( percentages[$i] / 2 ))
(( percents > 48 )) && percents=48 (( percents > 48 )) && percents=48
_full_squares=$(( percents[$i] /8 ))
for t in $(seq 1 $_full_squares) full_squares=$(( percents / 8 ))
for t in $(seq 1 $full_squares)
do do
squares+="█" squares+="█"
done done
if [[ $(( percents[$i] - ( _full_squares * 8 ) )) -eq 7 ]] if [[ $(( percents - ( full_squares * 8 ) )) -eq 7 ]]
then then
#squares+="▉" #squares+="█"
squares+="" squares+="▉"
shortsquares+="▇" #squares+="▇"
elif [[ $(( percents[$i] - ( _full_squares * 8 ) )) -eq 6 ]] elif [[ $(( percents - ( full_squares * 8 ) )) -eq 6 ]]
then then
#squares+="▊" #squares+="▓"
squares+="" squares+="▊"
shortsquares+="▆" #squares+="▆"
elif [[ $(( percents[$i] - ( _full_squares * 8 ) )) -eq 5 ]] elif [[ $(( percents - ( full_squares * 8 ) )) -eq 5 ]]
then then
#squares+="▋" #squares+="▓"
squares+="" squares+="▋"
shortsquares+="▅" #squares+="▅"
elif [[ $(( percents[$i] - ( _full_squares * 8 ) )) -eq 4 ]] elif [[ $(( percents - ( full_squares * 8 ) )) -eq 4 ]]
then then
#squares+="▌" #squares+="▒"
squares+="" squares+="▌"
shortsquares+="▄" #squares+="▄"
elif [[ $(( percents[$i] - ( _full_squares * 8 ) )) -eq 3 ]] elif [[ $(( percents - ( full_squares * 8 ) )) -eq 3 ]]
then then
#squares+="▍" #squares+="▒"
squares+="" squares+="▍"
shortsquares+="▃" #squares+="▃"
elif [[ $(( percents[$i] - ( _full_squares * 8 ) )) -eq 2 ]] elif [[ $(( percents - ( full_squares * 8 ) )) -eq 2 ]]
then then
#squares+="▎" #squares+="░"
squares+="" squares+="▎"
shortsquares+="▂" #squares+="▂"
elif [[ $(( percents[$i] - ( _full_squares * 8 ) )) -eq 1 ]] elif [[ $(( percents - ( full_squares * 8 ) )) -eq 1 ]]
then then
#squares+="▏" #squares+="░"
squares+="" squares+="▏"
shortsquares+="▁" #squares+="▁"
fi fi
if [[ "${statuses[$i]}" = "Unknown" ]]; then # Complétion de la barre afin d'avoir une taille fixe
squares="? $squares" blanks=$(( 6 - ${#squares} ))
fi for t in $(seq 1 $blanks)
do
squares+=" "
done
case "${statuses[$i]}" in # Choix de la couleur
case "${status}" in
"Charging") "Charging")
color="$charging_color" color="$charging_color"
if (( percentages[$i] <= 10 ))
then echo 0 on > /proc/acpi/ibm/led
fi
;; ;;
"Full") "Full")
color="$full_color" color="$full_color"
;; ;;
"AC"|"Not") "AC")
color="$ac_color" color="$ac_color"
;; ;;
"Discharging"|"Unknown") "Discharging"|"Unknown")
if (( percentages[$i] >= 0 && percentages[$i] < 10 )); then if (( percentage >= 0 && percentage < 10 )); then
color="${dis_colors[0]}" color="${dis_colors[0]}"
echo 0 blink > /proc/acpi/ibm/led elif (( percentage >= 10 && percentage < 20 )); then
elif (( percentages[$i] >= 10 && percentages[$i] < 20 )); then
color="${dis_colors[1]}" color="${dis_colors[1]}"
elif (( percentages[$i] >= 20 && percentages[$i] < 30 )); then elif (( percentage >= 20 && percentage < 30 )); then
color="${dis_colors[2]}" color="${dis_colors[2]}"
elif (( percentages[$i] >= 30 && percentages[$i] < 40 )); then elif (( percentage >= 30 && percentage < 40 )); then
color="${dis_colors[3]}" color="${dis_colors[3]}"
elif (( percentages[$i] >= 40 && percentages[$i] < 60 )); then elif (( percentage >= 40 && percentage < 60 )); then
color="${dis_colors[4]}" color="${dis_colors[4]}"
elif (( percentages[$i] >= 60 && percentages[$i] < 70 )); then elif (( percentage >= 60 && percentage < 70 )); then
color="${dis_colors[5]}" color="${dis_colors[5]}"
elif (( percentages[$i] >= 70 && percentages[$i] < 80 )); then elif (( percentage >= 70 && percentage < 80 )); then
color="${dis_colors[6]}" color="${dis_colors[6]}"
elif (( percentages[$i] >= 80 )); then elif (( percentage >= 80 )); then
color="${dis_colors[7]}" color="${dis_colors[7]}"
fi fi
;; ;;
esac esac
_blanks=$(( 6 - ${#squares} )) # Format détaillé en cas de clic
for t in $(seq 1 $_blanks) if [[ -n "$BLOCK_BUTTON" ]]
do then
squares+="</span><span foreground=\"#515151\" background=\"#515151\">█" message="<span foreground=\"$color\">${status} ${percentage}%</span> ${remaining}"
done message=${message% }
# Print Battery number if there is more than one else
if (( $end > 0 )) ; then message="<span foreground=\"$color\" background=\"#515151\">$squares</span>"
message="$message$(($i + 1)): "
fi fi
shortmessage="$message" # Affichage
echo "bat $message"
echo "bat <span foreground=\"$color\">${percentage}%</span>"
if [[ "$BLOCK_BUTTON" -eq 1 ]]; then
message="$message${statuses[$i]/Not/Not charging} <span foreground=\"$color\">${percentages[$i]}%${remainings[$i]}</span> "
shortmessage="$shortmessage${statuses[$i]/Not/Not charging} <span foreground=\"$color\">${remainings[$i]}</span> "
fi
message="bat $message<span foreground=\"$color\" background=\"#515151\">$squares</span>"
shortmessage="bat $shortmessage<span foreground=\"$color\">${percentages[$i]}%</span>"
done
echo "$message"
echo "$shortmessage"