Skip to content

Instantly share code, notes, and snippets.

@bkono
Forked from PogiNate/battinfo.rb
Last active August 29, 2015 14:22
Show Gist options
  • Save bkono/2be7c301bab5a653a564 to your computer and use it in GitHub Desktop.
Save bkono/2be7c301bab5a653a564 to your computer and use it in GitHub Desktop.
#!/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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment