Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#!/usr/bin/env ruby
require 'optparse'
class Battery
DEFAULT_FORMAT = "P (S: R)"
def self.status
_, percent, status, remaining, _ = %x{pmset -g batt|grep InternalBattery}.split(/\t|;/).map(&:strip)
percent.gsub!(/%/, '')
remaining.gsub!(/[\(\)]/, '')
remaining.gsub!(/\s*remaining/, '')
new(percent, status, remaining)
end
attr_reader :percent, :status, :remaining
def initialize(percent, status, remaining)
@percent, @status, @remaining = percent, status, remaining
end
def charging?
status != 'discharging'
end
def to_s(format = nil)
format ||= DEFAULT_FORMAT
format
.gsub(/P/, percent)
.gsub(/R/, remaining)
.gsub(/S/, status)
end
end
options = {
discharging: false
}
OptionParser.new do |opts|
opts.banner = "Usage: #{$0} [options]"
opts.on("-d", "Only show when discharging") do |d|
options[:discharging] = true
end
end.parse!
battery = Battery.status
if options[:discharging]
if battery.charging?
exit 0
end
end
print battery.to_s(ARGV[0])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.