Skip to content

Instantly share code, notes, and snippets.

@PogiNate
Last active May 22, 2022 10:25
Show Gist options
  • Save PogiNate/5490603 to your computer and use it in GitHub Desktop.
Save PogiNate/5490603 to your computer and use it in GitHub Desktop.
A simple script to format the output from `pmset -g batt` into a row of stars and icons.
#!/usr/bin/env ruby
# encoding: utf-8
full = "★"
empty = "☆"
battery = "🔋"
plug = "⚡"
star_count = 5
per_star = 100/star_count
v= Hash.new()
ARGF.each do |a|
if a.start_with? "Now"
#test for the first line
if a =~ /'(.*)'/
v[:source] = $~[1]
else
v[:source] = ""
end
elsif a.start_with?" -"
if a =~ /(\d{1,3})%;\s(.*);\s(\d:\d{2}|\(no estimate\))/
v[:percent] = $~[1].to_i
v[:state] = $~[2]
v[:time] = $~[3]
else
v[:percent] = "0"
v[:state] = "unknown"
v[:time] = "unknown"
end
end
end
outstring = ""
if v[:source]== "Battery Power"
outstring += "#{battery} "
else
outstring +="#{plug} "
end
full_stars = v[:percent]/per_star
empty_stars = star_count - full_stars
full_stars.times {outstring += "#{full} "}
empty_stars.times {outstring += "#{empty} "}
outstring += v[:time] == "0:00" ? " charged" : " #{v[:time]}"
puts outstring
@PogiNate
Copy link
Author

this had to be updated for Mavericks, because the language that comes out of pmset has changed.

@MayukhSobo
Copy link

./battinfo.rb:41:in <main>': undefined method/' for "0":String (NoMethodError) is occurred when I try to run the pmset command on terminal manually....The line causing error is
full_stars = v[:percent]/per_star

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