Skip to content

Instantly share code, notes, and snippets.

@mwerner
Created November 2, 2015 20:06
Show Gist options
  • Save mwerner/406cef7039e400b5d6d2 to your computer and use it in GitHub Desktop.
Save mwerner/406cef7039e400b5d6d2 to your computer and use it in GitHub Desktop.
script to show battery charge in your terminal
#!/usr/bin/env ruby
class Charge
attr_reader :state, :status
def initialize
@state, @status = `pmset -g batt`.split("\n")
end
def time_remaining
@time_remaining ||= begin
amount = status.match(/(\d+:\d+)/)[1]
amount.nil? ? '0:00' : amount
end
end
def percent_remaining
@percent_remaining ||= begin
amount = status.match(/Battery\-0\t(.*)%;/)[1]
amount.nil? ? 0 : amount.to_i
end
end
def charging?
state.match(/AC Power/)
end
def prompt
"#{color}#{character}%{%}"
end
private
def character
percent_remaining == 0 ? '?' : '›'
end
def to_s
[@state, @status].join("\n")
end
def color
case
when charging?
'%{%}' # green
when percent_remaining <= 25
'%{%}' # red
when percent_remaining <= 50
'%{%}' # yellow
else
'%{%}' # white
end
end
end
charge = Charge.new
puts case ARGV[0]
when '--prompt'
charge.prompt
else
charge
end
@mwerner
Copy link
Author

mwerner commented Nov 2, 2015

screen shot 2015-11-02 at 12 06 28 pm

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