Skip to content
This repository has been archived by the owner on Jun 8, 2019. It is now read-only.

Commit

Permalink
add the ability to override the battery charging icon & enable nerd f…
Browse files Browse the repository at this point in the history
…onts battery indicators. Also remove emoji
  • Loading branch information
ahmedelgabri committed May 11, 2018
1 parent 2d82699 commit a4145f5
Showing 1 changed file with 51 additions and 17 deletions.
68 changes: 51 additions & 17 deletions battery
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ battery usage:
-h, --help print this message
-t output tmux status bar format
-z output zsh prompt format
-e don't output the emoji
-c replace the battery charging icon
-i enable nerd fonts battery indicators
-G show the graph
-a output ascii instead of spark
-b battery path default: /sys/class/power_supply/BAT0
Expand All @@ -30,9 +31,10 @@ setDefaults() {
output_tmux=0
output_zsh=0
show_graph=0
show_battery_icon=0
charge_icon='ϟ'
ascii=0
ascii_bar='=========='
emoji=1
good_color="1;32"
middle_color="1;33"
warn_color="0;31"
Expand Down Expand Up @@ -135,42 +137,70 @@ apply_colors() {
fi
}

# Apply the correct icon to the battery status prompt
apply_battery_icon() {
if ((BATT_CONNECTED)); then
GRAPH=${charge_icon}
else
# Full
if [[ $BATT_PCT -ge 95 ]]; then
GRAPH=""

# NEARL-FULL
elif [[ $BATT_PCT -ge 75 ]] && [[ $BATT_PCT -lt 95 ]]; then
GRAPH=""

# HALF-FULL
elif [[ $BATT_PCT -ge 25 ]] && [[ $BATT_PCT -lt 75 ]]; then
GRAPH=""

# NEARLY EMPTY
elif [[ $BATT_PCT -lt 25 ]]; then
GRAPH=""
fi
fi
}

# Print the battery status
print_status() {
if ((emoji)) && ((BATT_CONNECTED)); then
GRAPH="ϟ"
if ((show_battery_icon)); then
apply_battery_icon
else
if ((BATT_CONNECTED)); then
GRAPH=${charge_icon}
else
if ((show_graph)); then
if command -v spark &>/dev/null; then
sparks=$(spark 0 ${BATT_PCT} 100)
GRAPH=${sparks:1:1}
sparks=$(spark 0 ${BATT_PCT} 100)
GRAPH=${sparks:1:1}
else
ascii=1
ascii=1
fi
fi

if ((ascii)); then
barlength=${#ascii_bar}
barlength=${#ascii_bar}

# Battery percentage rounded to the lenght of ascii_bar
rounded_n=$(( $barlength * $BATT_PCT / 100 + 1))
# Battery percentage rounded to the lenght of ascii_bar
rounded_n=$(( $barlength * $BATT_PCT / 100 + 1))

# Creates the bar
GRAPH=$(printf "[%-${barlength}s]" "${ascii_bar:0:rounded_n}")
# Creates the bar
GRAPH=$(printf "[%-${barlength}s]" "${ascii_bar:0:rounded_n}")
fi
fi
fi

if ((output_tmux)); then
printf "%s%s %s%s" "$COLOR" "$BATT_PCT%" "$GRAPH" "#[default]"
elif ((output_zsh)); then
printf "%%B%s%s %s" "$COLOR" "$BATT_PCT%" "$GRAPH"
else
printf "\e[0;%sm%s %s \e[m\n" "$COLOR" "$BATT_PCT%" "$GRAPH"
printf "\e[0;%sm%s %s \e[m\n" "$COLOR" "$BATT_PCT%" "$GRAPH"
fi
}

# Read args
while getopts ":g:m:w:tzeGab:p" opt; do
while getopts ":g:m:w:c:tizGab:p" opt; do
case $opt in
g)
good_color=$OPTARG
Expand All @@ -179,8 +209,15 @@ while getopts ":g:m:w:tzeGab:p" opt; do
middle_color=$OPTARG
;;
w)
echo "$OPTARG"
warn_color=$OPTARG
;;
c)
charge_icon=$OPTARG
;;
i)
show_battery_icon=1
;;
t)
output_tmux=1
good_color="green"
Expand All @@ -193,9 +230,6 @@ while getopts ":g:m:w:tzeGab:p" opt; do
middle_color="136"
warn_color="160"
;;
e)
emoji=0
;;
G)
show_graph=1
;;
Expand Down

0 comments on commit a4145f5

Please sign in to comment.