2016-04-05 12:38:45 +02:00
|
|
|
#!/bin/bash
|
|
|
|
#
|
|
|
|
# <bitbar.title>Battery Health</bitbar.title>
|
|
|
|
# <bitbar.version>v1.0</bitbar.version>
|
|
|
|
# <bitbar.author>Andros Fenollosa</bitbar.author>
|
|
|
|
# <bitbar.author.github>tanrax</bitbar.author.github>
|
2016-04-07 10:33:40 +02:00
|
|
|
# <bitbar.image>https://programadorwebvalencia.com/wp-content/uploads/2016/04/Screen-Shot-2016-04-05-at-12.47.25.jpg</bitbar.image>
|
2016-04-05 12:38:45 +02:00
|
|
|
# <bitbar.desc>Shows power percentaje and notice when you load</bitbar.desc>
|
|
|
|
|
|
|
|
# Variables
|
|
|
|
BATTERY=$(ioreg -l | awk '$3~/Capacity/{c[$3]=$5}END{OFMT="%.0f%";max=c["\"MaxCapacity\""];print(max>0?100*c["\"CurrentCapacity\""]/max:"?")}')
|
|
|
|
TYPE=$(pmset -g cap | sed -ne 's/^Capabilities for \(.*\) Power:$/\1/p')
|
|
|
|
POR_LOW=45
|
|
|
|
POR_HIGH=85
|
|
|
|
LOW=False
|
|
|
|
HIGH=False
|
|
|
|
SAVE_LOCATION=$TMPDIR/batteryHealth
|
|
|
|
BAD='🔴'
|
|
|
|
AC='⚡️'
|
|
|
|
|
|
|
|
# Get data
|
|
|
|
if [ -f "$SAVE_LOCATION" ]; then
|
|
|
|
DATA=$(cat "$SAVE_LOCATION")
|
|
|
|
else
|
|
|
|
DATA="$LOW|$HIGH"
|
|
|
|
fi
|
|
|
|
|
|
|
|
LOW=$(echo "$DATA" | cut -d "|" -f1)
|
|
|
|
HIGH=$(echo "$DATA" | cut -d "|" -f2)
|
|
|
|
|
|
|
|
# Functions
|
|
|
|
function changeStatus {
|
|
|
|
osascript -e "display notification \"$2\" with title \"$1\" sound name \"$3\"" &> /dev/null
|
|
|
|
}
|
|
|
|
|
|
|
|
function batteryLow {
|
|
|
|
LOW=True
|
2016-04-07 10:33:40 +02:00
|
|
|
changeStatus "Battery Low" "$BATTERY"% "Blow"
|
2016-04-05 12:38:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function batteryNormal {
|
|
|
|
HIGH=False
|
|
|
|
LOW=False
|
|
|
|
}
|
|
|
|
|
|
|
|
function batteryHigh {
|
|
|
|
HIGH=True
|
2016-04-07 10:33:40 +02:00
|
|
|
changeStatus "Battery high" "$BATTERY"% "Blow"
|
2016-04-05 12:38:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
# Logic
|
2016-04-07 10:33:40 +02:00
|
|
|
if [ "$BATTERY" -le $POR_LOW ] && [ $LOW = False ]; then
|
2016-04-05 12:38:45 +02:00
|
|
|
batteryLow
|
2016-04-07 10:33:40 +02:00
|
|
|
elif [ "$BATTERY" -ge $POR_HIGH ] && [ $HIGH = False ]; then
|
2016-04-05 12:38:45 +02:00
|
|
|
batteryHigh
|
2016-04-07 10:33:40 +02:00
|
|
|
elif [ "$BATTERY" -le $POR_HIGH ] && [ "$BATTERY" -ge $POR_LOW ]; then
|
2016-04-05 12:38:45 +02:00
|
|
|
batteryNormal
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Save data
|
|
|
|
echo "$LOW|$HIGH" > "$SAVE_LOCATION";
|
|
|
|
|
|
|
|
# View battery
|
2016-04-07 10:33:40 +02:00
|
|
|
if [ "$TYPE" = "AC" ]; then
|
|
|
|
echo $AC "$BATTERY"%
|
2016-04-05 12:38:45 +02:00
|
|
|
else
|
|
|
|
if [ $HIGH = True ] || [ $LOW = True ]; then
|
2016-04-07 10:33:40 +02:00
|
|
|
echo $BAD "$BATTERY"%
|
2016-04-05 12:38:45 +02:00
|
|
|
else
|
2016-04-07 10:33:40 +02:00
|
|
|
echo "$BATTERY"%
|
2016-04-05 12:38:45 +02:00
|
|
|
fi
|
|
|
|
fi
|