-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathtimeonbatt.sh
30 lines (27 loc) · 1.7 KB
/
timeonbatt.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#!/bin/bash
#code originaly take from https://apple.stackexchange.com/questions/176726/mac-os-x-report-time-on-battery-since-last-ac-unplug-but-exclude-sleep-time
#maintain by [email protected]
#update on 18th Jun 2017
#using GetBitBar to put this script to menu bar https://getbitbar.com/
humantime () { printf -v $1 "%dh%02dm%02ds" $(($2/3600)) $((($2/60)%60)) $(($2%60)); }
#unplugged=`pmset -g log | grep 'Summary- \[System: DeclUser.*\] Using Batt' | tail -1 | cut -f1,2 -d' '`
#unplugged=`syslog -u -k Sender loginwindow | grep 'magsafeStateChanged state changed old 1 new 2' | tail -n 1 | cut -f1 -dZ`
unplugged=`log show --style syslog | grep 'magsafeStateChanged state changed old 1 new 2' | tail -n 1 | cut -f1,2 -d" " | cut -f1 -d"."`
onbatt=$((`date +%s` - `date -j -f "%Y-%m-%d %H:%M:%S" "$unplugged" +%s`))
slept=`pmset -g log | sed -n "/^$unplugged/,//p" | awk '/Entering Sleep.*[0-9]+ secs/ {n=split($0,a," ");sum+=a[n-1]} END {print sum}'`
[[ -z $slept ]] && slept=0
awake=$((onbatt-slept))
remain=`pmset -g batt | head -n2 | tail -1 | cut -d' ' -f5`
chargestatus=`pmset -g batt | head -n2 | tail -1 | cut -d' ' -f4 | cut -d ';' -f1`
if [ $remain != '(no' ]; then
hremain=`date -j -f "%H:%M" "$remain" "+%Hh%Mm"`
else
hremain='(no est. remain time)'
fi
humantime hawake $awake
humantime hslept $slept
remainpercent=`pmset -g batt | head -n2 | tail -1 | cut -d' ' -f3 | cut -d$'\t' -f2 | cut -d ';' -f1 | cut -d '%' -f1`
printf "Awake on battery for $hawake (%.1f%%)\n" `echo "$awake*100/$onbatt" | bc -l`
printf "Slept on battery for $hslept (%.1f%%)\n" `echo "$slept*100/$onbatt" | bc -l`
printf "Remain on battery for $hremain (%.1f%%)\n" `echo "$remainpercent" | bc -l`
printf "Battery Status: $chargestatus \n"