Created
November 28, 2012 08:03
-
-
Save nixpulvis/4159768 to your computer and use it in GitHub Desktop.
Ruby Mac Battery Display
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Want to add this to zsh's RPROMPT? Easy.