From eef89377e3496787941b7982696a7e6b1f59dd13 Mon Sep 17 00:00:00 2001 From: Andros Fenollosa Date: Tue, 5 Apr 2016 12:38:45 +0200 Subject: [PATCH] First commit --- battery_health.60s.sh | 71 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100755 battery_health.60s.sh diff --git a/battery_health.60s.sh b/battery_health.60s.sh new file mode 100755 index 0000000..87d7693 --- /dev/null +++ b/battery_health.60s.sh @@ -0,0 +1,71 @@ +#!/bin/bash +# +# Battery Health +# v1.0 +# Andros Fenollosa +# tanrax +# Shows power percentaje and notice when you load + +# 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 + changeStatus "Battery Low" $BATTERY% "Blow" +} + +function batteryNormal { + HIGH=False + LOW=False +} + +function batteryHigh { + HIGH=True + changeStatus "Battery high" $BATTERY% "Blow" +} + +# Logic +if [ $BATTERY -le $POR_LOW ] && [ $LOW = False ]; then + batteryLow +elif [ $BATTERY -ge $POR_HIGH ] && [ $HIGH = False ]; then + batteryHigh +elif [ $BATTERY -le $POR_HIGH ] && [ $BATTERY -ge $POR_LOW ]; then + batteryNormal +fi + +# Save data +echo "$LOW|$HIGH" > "$SAVE_LOCATION"; + +# View battery +if [ $TYPE = 'AC' ]; then + echo $AC $BATTERY% +else + if [ $HIGH = True ] || [ $LOW = True ]; then + echo $BAD $BATTERY% + else + echo $BATTERY% + fi +fi