Skip to content

Instantly share code, notes, and snippets.

@mikerudolph
Created November 20, 2013 20:17
Show Gist options
  • Save mikerudolph/7570258 to your computer and use it in GitHub Desktop.
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.
#!/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