Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Output a Zelda-style heart meter for your MacBook's battery level. On the command line. Based on an idea from @stephencelis, executed by myself. For a tmux version, see https://gist.github.com/4324139
#!/usr/bin/env zsh
#
# Works best with blinking text; the last heart will blink
# when you have less than 25% of your battery life remaining.
BATTERY="$(pmset -g ps | awk 'NR==2' | perl -pe 's/.*?(\d+)%.*/\1/')"
if [[ $BATTERY -lt 25 ]]; then
echo "\e[5;31m♥\e[0;31m♡♡\e[0m"
elif [[ $BATTERY -lt 50 ]]; then
echo "\e[31m♥♡♡\e[0m"
elif [[ $BATTERY -lt 75 ]]; then
echo "\e[31m♥♥♡\e[0m"
else
echo "\e[31m♥♥♥\e[0m"
fi
@cobyism

This comment has been minimized.

Copy link

commented Dec 18, 2012

That’s neat!

@spaceninja

This comment has been minimized.

Copy link

commented Dec 18, 2012

Really, really great. But the hearts overlap in my terminal... I'm guessing this is due to the font I'm using?

http://cl.ly/image/062a3N231O2R

@davidcelis

This comment has been minimized.

Copy link
Owner Author

commented Dec 18, 2012

It looks great with Menlo, which is my preferred font... However, if you're using another, lesser font and your hearts are overlapping, you can check the "Unicode East Asian Ambiguous characters are wide" setting under Advanced in Terminal.app:

terminal-settings

@vosechu

This comment has been minimized.

Copy link

commented Dec 18, 2012

@spaceninja Yes, Monaco uses half-width hearts. Andale Mono and Courier New both do it right but they look awful.

@vosechu

This comment has been minimized.

Copy link

commented Dec 18, 2012

Similar setting in Iterm2:

http://d.pr/i/zWis

@davidcelis

This comment has been minimized.

Copy link
Owner Author

commented Dec 18, 2012

The output of pmset -g ps becomes three lines when your battery approaches critical levels (under 20% remaining). So I've updated it to pipe to awk 'NR==2' instead of tail -1.

@vosechu

This comment has been minimized.

Copy link

commented Feb 24, 2013

In case I have to look this up again, this is how I ended up doing it:

zelda() {
  BATTERY="$(pmset -g ps | tail -1 | perl -pe 's/.*?(\d+)%.*/\1/')"

  if [[ $BATTERY -lt 25 ]]; then
    echo "%{$fg_blink[red]%}♥%{$fg_bold[red]%} ♡ ♡ "
  elif [[ $BATTERY -lt 50 ]]; then
    echo "♥ ♡ ♡ "
  elif [[ $BATTERY -lt 75 ]]; then
    echo "♥ ♥ ♡ "
  else
    echo "♥ ♥ ♥ "
  fi
}

# Get the current ruby version in use with RVM:
if [ -e ~/.rvm/bin/rvm-prompt ]; then
    RUBY_PROMPT_="%{$fg_bold[blue]%}rvm:(%{$fg[green]%}\$(~/.rvm/bin/rvm-prompt s i v g)%{$fg_bold[blue]%})%{$reset_color%} "
else
  if which rbenv &> /dev/null; then
    RUBY_PROMPT_="%{$fg_bold[blue]%}rbenv:(%{$fg[green]%}\$(rbenv version | sed -e 's/ (set.*$//')%{$fg_bold[blue]%})%{$reset_color%} "
  fi
fi

# Get the host name (first 4 chars)
HOST_PROMPT_="%{$fg_bold[red]%}➜  %{$fg_bold[cyan]%}%c "
GIT_PROMPT="%{$fg_bold[blue]%}\$(git_prompt_info)%{$fg_bold[blue]%} % %{$reset_color%}"
PROMPT="%{$fg_bold[red]%}$(zelda)%{$reset_color%}$HOST_PROMPT_$RUBY_PROMPT_$GIT_PROMPT"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.