Skip to content

Commit

Permalink
Merge pull request #16 from wolfcore/master
Browse files Browse the repository at this point in the history
Added ASCII output option, and used more functions
  • Loading branch information
Goles committed Sep 10, 2013
2 parents f9d2fa8 + 28331da commit 2d955b9
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 38 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ Battery is a little bash script that uses [Spark](https://github.com/holman/spar

Right now, battery requires [Spark](https://github.com/holman/spark) to graph your battery status, and only runs on __Mac OS X__.

If you don't want to use Spark, you can use the `-a` flag, for ascii output:
![image](http://i.imgur.com/w9qtQeu.png)

### Install

#### Homebrew
Expand Down
104 changes: 66 additions & 38 deletions battery
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ battery: usage:
general:
-h, --help print this message
-t output tmux status bar format
-p use pmset (more accurate)
-t output tmux status bar format
-a output ascii bar instead of spark's
colors:
-g <color> good battery level default: green or 1;32
Expand All @@ -25,6 +26,8 @@ fi
setDefaults() {
pmset_on=0
output_tmux=0
ascii=0
ascii_bar='=========='
good_color="1;32"
middle_color="1;33"
warn_color="0;31"
Expand Down Expand Up @@ -65,49 +68,19 @@ if [[ ! $(battery_external_connected) = "No" ]]; then
connected=1
fi

# Read args
while getopts ":g:m:w:tph" opt; do
case $opt in
g)
good_color=$OPTARG
;;
m)
middle_color=$OPTARG
;;
w)
warn_color=$OPTARG
;;
t)
output_tmux=1
good_color="green"
middle_color="yellow"
warn_color="red"
;;
p)
pmset_on=1
;;
\?)
echo "Invalid option: -$OPTARG"
exit 1
;;
:)
echo "Option -$OPTARG requires an argument"
exit 1
;;
esac
done

# Run Battery
run_battery() {
if ((pmset_on)); then
BATTERY_STATUS="$(pmset -g batt | grep -o '[0-9]*%' | tr -d %)"
else
BATTERY_STATUS="$(battery_charge)"
fi

[[ -z "$BATTERY_STATUS" ]] && exit 1

}

# Apply the correct color to the battery status prompt
apply_colors() {
# Green
if [[ $BATTERY_STATUS -ge 75 ]]; then
if ((output_tmux)); then
Expand All @@ -132,17 +105,72 @@ elif [[ $BATTERY_STATUS -lt 25 ]]; then
COLOR=$warn_color
fi
fi
}


print_status() {
# Print the battery status
if ((connected)); then
GRAPH=""

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

# Divides BATTTERY_STATUS by 10 to get a decimal number; i.e 7.6
n=$(echo "scale = 1; $BATTERY_STATUS / 10" | bc)

# Round the number to the nearest whole number
rounded_n=$(printf "%.0f" "$n")

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

else
GRAPH=$(spark 0 ${BATTERY_STATUS} 100 | awk '{print substr($0,4,3)}')
fi

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

# Read args
while getopts ":g:m:w:tap" opt; do
case $opt in
g)
good_color=$OPTARG
;;
m)
middle_color=$OPTARG
;;
w)
warn_color=$OPTARG
;;
t)
output_tmux=1
good_color="green"
middle_color="yellow"
warn_color="red"
;;
a)
ascii=1
;;
p)
pmset_on=1
;;
\?)
echo "Invalid option: -$OPTARG"
exit 1
;;
:)
echo "Option -$OPTARG requires an argument"
exit 1
;;
esac
done


run_battery
apply_colors
print_status

0 comments on commit 2d955b9

Please sign in to comment.