Skip to content

Instantly share code, notes, and snippets.

@sarcasticsimba
Created November 7, 2014 02:42
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sarcasticsimba/dd13d9488ec1bd9cb26d to your computer and use it in GitHub Desktop.
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"
@caseymhunt
Copy link

For those that use a magic mouse, this will give you battery remaining of that device, in relevant color scheme to battery percentage.

EDIT: Adding a case for when the mouse is not present, or turned off; prevents bash errors.

mousebatt=$(ioreg -n "BNBMouseDevice" | grep -i '"batterypercent" =' | sed 's/[^[:digit:]]//g')
noc=$'\e[39m'

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

echo -e "\n---Bluetooth Mouse---"
if [ $mousebatt ]; then echo "$mousebattc$mousebatt%$noc";
else                  echo "No Mouse Detected";
fi; 

@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