Created
November 20, 2013 20:17
-
-
Save mikerudolph/7570258 to your computer and use it in GitHub Desktop.
Ruby script to pipe `pmset` for battery info and display status back in readable text. Created for display in tmux status bar.
This file contains hidden or 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/env ruby | |
# encoding utf-8 | |
status = Hash.new() | |
if ARGF === nil | |
puts "NA" | |
exit | |
end | |
ARGF.each do |arg| | |
if arg.start_with? "Now drawing" | |
if arg.include? "AC Power" | |
status[:source] = "AC" | |
elsif arg.include? "Battery Power" | |
status[:source] = "battery" | |
else | |
status[:source] = "Uh oh..." | |
end | |
elsif arg.start_with? " -InternalBattery" | |
split = arg.match(/(\d{1,3})%;\s(.*);\s(\d:\d{2}|\(no estimate\))/) | |
status[:amount] = split[1] | |
status[:charged] = split[2] === "charged" ? true : false | |
status[:remaining] = split[3] | |
end | |
end | |
output = "Running on #{status[:source]} with " | |
if status[:charged] | |
output += "a full charge" | |
else | |
output += "#{status[:amount]}% left and #{status[:remaining]} until " | |
end | |
if status[:source] === "battery" | |
output += "empty" | |
elsif status[:source] === "battery" && !status[:charged] | |
output += "full" | |
end | |
puts output |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment