Skip to content

Instantly share code, notes, and snippets.

@sarcasticsimba
Created November 7, 2014 02:42
Show Gist options
  • Select an option

  • Save sarcasticsimba/dd13d9488ec1bd9cb26d to your computer and use it in GitHub Desktop.

Select an option

Save sarcasticsimba/dd13d9488ec1bd9cb26d to your computer and use it in GitHub Desktop.
full=$(pmset -g batt | { read; read n full; echo "$full"; })
batt=$(echo "$full" | cut -d';' -f1 | tr -d '%')
status=$(echo "$full" | cut -d';' -f2 | tr -d ' ')
time=$(echo "$full" | cut -d';' -f3 | sed 's/^ *//')
noc=$'\e[39m'
if (($batt >= 66)); then battc=$'\e[32m';
elif (($batt >= 33)); then battc=$'\e[33m';
else battc=$'\e[31m';
fi;
if [ "$status" == "discharging" ]; then
statc=$'\e[31m';
elif [ "$status" == "charging" ]; then
statc=$'\e[32m';
elif [ "$status" == "charged" ]; then
statc=$'\e[32m';
else
statc=$'\e[33m';
fi
echo "$battc$batt%$noc | $statc$status$noc | $time"
@TechplexEngineer
Copy link

On osx 10.12.6 there seems to be an additional id field at the beginning

#! /bin/bash

full=$(pmset -g batt | { read; read n full; echo "$full"; })
batt=$(echo "$full" | awk '/(%);/{print $2}' | cut -d'%' -f1)
status=$(echo "$full" | cut -d';' -f2 | tr -d ' ')
time=$(echo "$full" | awk '/(%);/{print $4 " " $5}') #cut -d';' -f3 | sed 's/^ *//'
noc=$'\e[39m'


if   (($batt >= 66)); then battc=$'\e[32m';
elif (($batt >= 33)); then battc=$'\e[33m';
else                       battc=$'\e[31m';
fi;

if [ "$status" == "discharging" ]; then
    statc=$'\e[31m';
elif [ "$status" == "charging" ]; then
    statc=$'\e[32m';
elif [ "$status" == "charged" ]; then
    statc=$'\e[32m';
else
    statc=$'\e[33m';
fi

echo "$battc$batt%$noc | $statc$status$noc | $time"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment