Skip to content

Commit

Permalink
cleaned up batter plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
napalm255 committed Dec 12, 2014
1 parent 2939054 commit e478723
Showing 1 changed file with 56 additions and 84 deletions.
140 changes: 56 additions & 84 deletions plugins/battery.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,97 +7,69 @@ HEART_FULL="♥"
HEART_EMPTY=""

run_plugin() {
__process_settings
battery_status=$(__battery_linux)
__process_settings
battery_status=$(__battery_linux)

[ -z "$battery_status" ] && return
[ -z "$battery_status" ] && return

case "$POWERTMUX_SEG_BATTERY_TYPE" in
"percentage")
output="${HEART_FULL} ${battery_status}%"
;;
"cute")
output=$(__cutinate $battery_status)
esac
if [ -n "$output" ]; then
echo "$output"
fi
case "$POWERTMUX_SEG_BATTERY_TYPE" in
"percentage")
output="${HEART_FULL} ${battery_status}%"
;;
"cute")
output=$(__cutinate $battery_status)
;;
esac
if [ -n "$output" ]; then
echo "$output"
fi
}

__process_settings() {
if [ -z "$POWERTMUX_SEG_BATTERY_TYPE" ]; then
export POWERTMUX_SEG_BATTERY_TYPE="${POWERTMUX_SEG_BATTERY_TYPE_DEFAULT}"
fi
if [ -z "$POWERTMUX_SEG_BATTERY_NUM_HEARTS" ]; then
export POWERTMUX_SEG_BATTERY_NUM_HEARTS="${POWERTMUX_SEG_BATTERY_NUM_HEARTS_DEFAULT}"
fi
if [ -z "$POWERTMUX_SEG_BATTERY_TYPE" ]; then
export POWERTMUX_SEG_BATTERY_TYPE="${POWERTMUX_SEG_BATTERY_TYPE_DEFAULT}"
fi
if [ -z "$POWERTMUX_SEG_BATTERY_NUM_HEARTS" ]; then
export POWERTMUX_SEG_BATTERY_NUM_HEARTS="${POWERTMUX_SEG_BATTERY_NUM_HEARTS_DEFAULT}"
fi
}

__battery_linux() {
case "$SHELL_PLATFORM" in
"linux")
BATPATH=/sys/class/power_supply/BAT0
if [ ! -d $BATPATH ]; then
BATPATH=/sys/class/power_supply/BAT1
fi
STATUS=$BATPATH/status
BAT_FULL=$BATPATH/charge_full
if [ ! -r $BAT_FULL ]; then
BAT_FULL=$BATPATH/energy_full
fi
BAT_NOW=$BATPATH/charge_now
if [ ! -r $BAT_NOW ]; then
BAT_NOW=$BATPATH/energy_now
fi

if [ "$1" = `cat $STATUS` -o "$1" = "" ]; then
__linux_get_bat
fi
;;
"bsd")
STATUS=`sysctl -n hw.acpi.battery.state`
case $1 in
"Discharging")
if [ $STATUS -eq 1 ]; then
__freebsd_get_bat
fi
;;
"Charging")
if [ $STATUS -eq 2 ]; then
__freebsd_get_bat
fi
;;
"")
__freebsd_get_bat
;;
esac
;;
esac
}

__cutinate() {
perc=$1
inc=$(( 100 / $POWERTMUX_SEG_BATTERY_NUM_HEARTS ))


for i in `seq $POWERTMUX_SEG_BATTERY_NUM_HEARTS`; do
if [ $perc -lt 99 ]; then
echo -n $HEART_EMPTY
else
echo -n $HEART_FULL
fi
echo -n " "
perc=$(( $perc + $inc ))
done
}
__battery_linux() {
BATPATH=/sys/class/power_supply/BAT0
if [ ! -d $BATPATH ]; then
BATPATH=/sys/class/power_supply/BAT1
fi
STATUS=$BATPATH/status
BAT_FULL=$BATPATH/charge_full
if [ ! -r $BAT_FULL ]; then
BAT_FULL=$BATPATH/energy_full
fi
BAT_NOW=$BATPATH/charge_now
if [ ! -r $BAT_NOW ]; then
BAT_NOW=$BATPATH/energy_now
fi
if [ "$1" = `cat $STATUS` -o "$1" = "" ]; then
__get_bat
fi
}

__linux_get_bat() {
bf=$(cat $BAT_FULL)
bn=$(cat $BAT_NOW)
echo $(( 100 * $bn / $bf ))
}
__cutinate() {
perc=$1
inc=$(( 100 / $POWERTMUX_SEG_BATTERY_NUM_HEARTS ))

__freebsd_get_bat() {
echo "$(sysctl -n hw.acpi.battery.life)"
for i in `seq $POWERTMUX_SEG_BATTERY_NUM_HEARTS`; do
if [ $perc -lt 99 ]; then
echo -n $HEART_EMPTY
else
echo -n $HEART_FULL
fi
echo -n " "
perc=$(( $perc + $inc ))
done
}

}
__get_bat() {
bf=$(cat $BAT_FULL)
bn=$(cat $BAT_NOW)
echo $(( 100 * $bn / $bf ))
}

0 comments on commit e478723

Please sign in to comment.