Created
October 9, 2014 00:00
-
-
Save robhurring/6b1d66c97fc260e6a026 to your computer and use it in GitHub Desktop.
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 | |
| 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