Skip to content

Instantly share code, notes, and snippets.

@nixpulvis
Created November 28, 2012 08:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nixpulvis/4159768 to your computer and use it in GitHub Desktop.
Save nixpulvis/4159768 to your computer and use it in GitHub Desktop.
Ruby Mac Battery Display
#!/usr/bin/ruby
# encoding: utf-8
# Mac OS X Battery Display
#
# ▐▐▐▐▐▐▐▐▐▐
#
# by Nathan Lilienthal <nathanl@ccs.neu.edu> 2012
# Distributed under the GNU General Public License, version 3.0.
#
#
# Running this command will output a 10 segmented bar indicating
# the computer's current battery state, and if it's plugged in.
#
WIDTH = 10
LIT_RED_BLOCK = "\e[31m▐\e[0m"
LIT_YELLOW_BLOCK = "\e[33m▐\e[0m"
LIT_GREEN_BLOCK = "\e[32m▐\e[0m"
UNCHARGED_BLOCK = "\e[2m\e[37m▐\e[0m"
def battery
hash = { }
data = IO.popen("ioreg -rc AppleSmartBattery").readlines()
data.select { |l| l =~ /=/ }.map{ |l| l.strip.gsub("\"", "") }.each do |l|
key, value = l.split " = "
hash[key] = value
end
return hash
end
def block( i, dim )
if i / WIDTH.to_f >= 3.0/5.0
b = LIT_GREEN_BLOCK
elsif i / WIDTH.to_f >= 1.0/5.0
b = LIT_YELLOW_BLOCK
else
b = LIT_RED_BLOCK
end
# append dimming escape
b = "\e[2m" + b if dim
return b
end
def bar( charge, charging )
b = ""
WIDTH.times do |i|
if (charge / 100 * WIDTH).round >= i
b += block(i, false)
else
b += charging ? block(i, true) : UNCHARGED_BLOCK
end
end
"#{b}\n"
end
# Current charge of computer (%)
charge = 100 * (battery['CurrentCapacity'].to_f / battery['MaxCapacity'].to_f)
# Print this bitch
print bar(charge, battery['IsCharging'] == "Yes")
@nixpulvis
Copy link
Author

Want to add this to zsh's RPROMPT? Easy.

RPROMPT='%10{$(echo `battery`)%}'

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