Created
November 2, 2015 20:06
-
-
Save mwerner/406cef7039e400b5d6d2 to your computer and use it in GitHub Desktop.
script to show battery charge in your terminal
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 | |
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}%{[00m%}" | |
end | |
private | |
def character | |
percent_remaining == 0 ? '?' : '›' | |
end | |
def to_s | |
[@state, @status].join("\n") | |
end | |
def color | |
case | |
when charging? | |
'%{[32m%}' # green | |
when percent_remaining <= 25 | |
'%{[31m%}' # red | |
when percent_remaining <= 50 | |
'%{[1;33m%}' # yellow | |
else | |
'%{[1;37m%}' # white | |
end | |
end | |
end | |
charge = Charge.new | |
puts case ARGV[0] | |
when '--prompt' | |
charge.prompt | |
else | |
charge | |
end |
Author
mwerner
commented
Nov 2, 2015
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment