batt 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in batt.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Spike Grobstein
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,51 @@
1
+ # Batt
2
+
3
+ Read the battery status from your laptop. Designed to be used in `tmux`'s status line.
4
+
5
+ Currently only supports OSX.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ gem 'batt'
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install batt
20
+
21
+ ## Usage
22
+
23
+ There is a full API that I'm working on, but it's not complete. Gem docs will come
24
+ at the time that the code is implemented.
25
+
26
+ You can use the commandline program, in OSX only for now:
27
+
28
+ batt <action>
29
+
30
+ Where `action` is one of the following:
31
+
32
+ * `all` -- prints out all status of the battery
33
+ * `status` -- 'charging' or 'discharging'
34
+ * `source` -- 'AC Power' or 'Battery Power'
35
+ * `remaining` -- amount of time remaining til complete discharge or complete charge
36
+ * `capacity` -- a percentage of how charged the battery is
37
+
38
+ ## tmux
39
+
40
+ Example `tmux` status line configuration:
41
+
42
+ tmux set-option status-right-length 120
43
+ tmux set-option status-right "[ #(batt source) #(batt remaining) ] #(date \"+%Y-%m-%d %H:%M:%S\")"
44
+
45
+ ## Contributing
46
+
47
+ 1. Fork it
48
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
49
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
50
+ 4. Push to the branch (`git push origin my-new-feature`)
51
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/batt.gemspec ADDED
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'batt/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "batt"
8
+ spec.version = Batt::VERSION
9
+ spec.authors = ["Spike Grobstein"]
10
+ spec.email = ["me@spike.cx"]
11
+ spec.description = %q{Read basic information about your laptop's battery. Includes both ANSI and tmux colour support.}
12
+ spec.summary = %q{Read basic information about your laptop's battery}
13
+ spec.homepage = "https://github.com/spikegrobstein/batt"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "cocaine", "~> 0.5"
22
+ spec.add_dependency "thor"
23
+
24
+ spec.add_development_dependency "rspec"
25
+ spec.add_development_dependency "pry"
26
+ spec.add_development_dependency "bundler", "~> 1.3"
27
+ spec.add_development_dependency "rake"
28
+ end
data/bin/batt ADDED
@@ -0,0 +1,6 @@
1
+ #! /usr/bin/env ruby
2
+ # vi:syntax=ruby
3
+
4
+ require 'batt'
5
+
6
+ Batt::App.start(ARGV)
data/lib/batt/app.rb ADDED
@@ -0,0 +1,51 @@
1
+ module Batt
2
+ class App < Thor
3
+
4
+ desc "all", "spit out the full battery status"
5
+ # option :tmux, :type => :boolean
6
+ # option :ansi, :type => :boolean
7
+ def all
8
+ b = Batt::Reader.new
9
+ result = b.status
10
+
11
+ size = result.keys.map{ |k| k.length }.reduce(0) { |m, l| m = l if l > m; m }
12
+ result.each do |k,v|
13
+ puts "%#{ size }s: %s" % [ k, v ]
14
+ end
15
+
16
+ end
17
+
18
+ desc "source", "spit out the current source of power (AC or battery)"
19
+ def source
20
+ b = Batt::Reader.new
21
+
22
+ puts b.status[:source]
23
+ end
24
+
25
+ desc "capacity", "spit out the current capacity in % of the battery"
26
+ def capacity
27
+ b = Batt::Reader.new
28
+
29
+ puts b.status[:capacity]
30
+ end
31
+
32
+ desc "status", "spit out the status of the battery"
33
+ def status
34
+ b = Batt::Reader.new
35
+ result = b.status
36
+
37
+ puts b.status[:status]
38
+ end
39
+
40
+ desc "remaining", "spit out the remaining time for the battery"
41
+ def remaining
42
+ b = Batt::Reader.new
43
+
44
+ puts b.status[:remaining]
45
+ end
46
+
47
+ end
48
+ end
49
+
50
+
51
+
@@ -0,0 +1,49 @@
1
+ module Batt
2
+ class Reader
3
+
4
+ attr_accessor :battery_reader
5
+
6
+ def initialize
7
+ os = get_os
8
+ case os
9
+ when :darwin
10
+ @battery_reader = lambda { parse_battery_status_osx(get_battery_status_osx) }
11
+ else
12
+ raise "Unsupported OS: #{ os }"
13
+ end
14
+ end
15
+
16
+ def status
17
+ @battery_reader.call
18
+ end
19
+
20
+ # figure out which OS is running
21
+ # this might need to be a little more solid
22
+ # returns a downcased symbol
23
+ def get_os
24
+ line = Cocaine::CommandLine.new('uname')
25
+ output = line.run
26
+
27
+ output.chomp.downcase.intern
28
+ end
29
+
30
+ # get the battery status for OSX
31
+ # returns a hash.
32
+ # spits out something like:
33
+ # Currently drawing from 'AC Power'
34
+ # -InternalBattery-0 98%; charging; 0:30 remaining
35
+ # TODO: write some docs
36
+ def get_battery_status_osx
37
+ line = Cocaine::CommandLine.new('pmset', '-g batt')
38
+ output = line.run
39
+ end
40
+
41
+ def parse_battery_status_osx(output)
42
+ output = output.split(/\n/)
43
+ status = output.shift.scan(/'(.+?)'/).first
44
+ result = status + output.shift.scan(/(\d+%);\s*(.+?);\s*(.+)/).first
45
+
46
+ Hash[[:source, :capacity, :status, :remaining].zip(result)]
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,3 @@
1
+ module Batt
2
+ VERSION = "0.0.1"
3
+ end
data/lib/batt.rb ADDED
@@ -0,0 +1,11 @@
1
+ require "batt/version"
2
+
3
+ require 'cocaine'
4
+ require 'thor'
5
+
6
+ require 'batt/reader'
7
+ require 'batt/app'
8
+
9
+ module Batt
10
+ # Your code goes here...
11
+ end
@@ -0,0 +1,97 @@
1
+ require 'spec_helper'
2
+
3
+ describe Batt::Reader do
4
+ let(:reader) { Batt::Reader.new }
5
+ let(:status) { reader.status }
6
+
7
+ it "should initialize without error" do
8
+ b = Batt::Reader.new
9
+ end
10
+
11
+ context "get_battery_status_osx" do
12
+ let(:output_ac_power_no_estimate) { "Currently drawing from 'AC Power'\n -InternalBattery-0 98%; charging; (no estimate)" }
13
+ let(:output_ac_power_estimate) { "Currently drawing from 'AC Power'\n -InternalBattery-0 88%; charging; 0:30 remaining" }
14
+ let(:output_battery_power) { "Currently drawing from 'Battery Power'\n -InternalBattery-0 100%; discharging; 5:36 remaining" }
15
+
16
+ let(:status) { reader.parse_battery_status_osx(output) }
17
+
18
+ before do
19
+ reader.stub(:get_os => :darwin)
20
+ end
21
+
22
+ context "when ac attached" do
23
+ context "no estimate" do
24
+ let(:output) { output_ac_power_no_estimate }
25
+
26
+ it "should return a hash" do
27
+ status.class.should == Hash
28
+ end
29
+
30
+ it "should return a source of 'AC Power'" do
31
+ status[:source].should == 'AC Power'
32
+ end
33
+
34
+ it "should return a capacity of '98%'" do
35
+ status[:capacity].should == '98%'
36
+ end
37
+
38
+ it "should return a status of 'charging'" do
39
+ status[:status].should == 'charging'
40
+ end
41
+
42
+ it "should return a remaining of '(no estimate)'" do
43
+ status[:remaining].should == '(no estimate)'
44
+ end
45
+
46
+ end
47
+
48
+ context "with estimate" do
49
+ let(:output) { output_ac_power_estimate }
50
+
51
+ it "should return a hash" do
52
+ status.class.should == Hash
53
+ end
54
+
55
+ it "should return a source of 'AC Power'" do
56
+ status[:source].should == 'AC Power'
57
+ end
58
+
59
+ it "should return a capacity of '88%'" do
60
+ status[:capacity].should == '88%'
61
+ end
62
+
63
+ it "should return a status of 'charging'" do
64
+ status[:status].should == 'charging'
65
+ end
66
+
67
+ it "should return a remaining of '0:30 remaining'" do
68
+ status[:remaining].should == '0:30 remaining'
69
+ end
70
+ end
71
+ end
72
+
73
+ context "when on battery power" do
74
+ let(:output) { output_battery_power }
75
+
76
+ it "should return a hash" do
77
+ status.class.should == Hash
78
+ end
79
+
80
+ it "should return a source of 'Battery Power'" do
81
+ status[:source].should == 'Battery Power'
82
+ end
83
+
84
+ it "should return a capacity of '100%'" do
85
+ status[:capacity].should == '100%'
86
+ end
87
+
88
+ it "should return a status of 'discharging'" do
89
+ status[:status].should == 'discharging'
90
+ end
91
+
92
+ it "should return a remaining of '5:36 remaining'" do
93
+ status[:remaining].should == '5:36 remaining'
94
+ end
95
+ end
96
+ end
97
+ end
@@ -0,0 +1,11 @@
1
+ require 'rubygems'
2
+ require 'bundler/setup'
3
+
4
+ $: << File.dirname(__FILE__) + '/../lib'
5
+
6
+ require 'batt'
7
+
8
+ def fixture_path(filename)
9
+ File.join( File.dirname(__FILE__), 'fixtures', filename )
10
+ end
11
+
metadata ADDED
@@ -0,0 +1,166 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: batt
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Spike Grobstein
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-07-09 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: cocaine
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '0.5'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '0.5'
30
+ - !ruby/object:Gem::Dependency
31
+ name: thor
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rspec
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: pry
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ - !ruby/object:Gem::Dependency
79
+ name: bundler
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ~>
84
+ - !ruby/object:Gem::Version
85
+ version: '1.3'
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ~>
92
+ - !ruby/object:Gem::Version
93
+ version: '1.3'
94
+ - !ruby/object:Gem::Dependency
95
+ name: rake
96
+ requirement: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ! '>='
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ type: :development
103
+ prerelease: false
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ! '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ description: Read basic information about your laptop's battery. Includes both ANSI
111
+ and tmux colour support.
112
+ email:
113
+ - me@spike.cx
114
+ executables:
115
+ - batt
116
+ extensions: []
117
+ extra_rdoc_files: []
118
+ files:
119
+ - .gitignore
120
+ - .rspec
121
+ - Gemfile
122
+ - LICENSE.txt
123
+ - README.md
124
+ - Rakefile
125
+ - batt.gemspec
126
+ - bin/batt
127
+ - lib/batt.rb
128
+ - lib/batt/app.rb
129
+ - lib/batt/reader.rb
130
+ - lib/batt/version.rb
131
+ - spec/batt/reader_spec.rb
132
+ - spec/spec_helper.rb
133
+ homepage: https://github.com/spikegrobstein/batt
134
+ licenses:
135
+ - MIT
136
+ post_install_message:
137
+ rdoc_options: []
138
+ require_paths:
139
+ - lib
140
+ required_ruby_version: !ruby/object:Gem::Requirement
141
+ none: false
142
+ requirements:
143
+ - - ! '>='
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ segments:
147
+ - 0
148
+ hash: 4283814709343736854
149
+ required_rubygems_version: !ruby/object:Gem::Requirement
150
+ none: false
151
+ requirements:
152
+ - - ! '>='
153
+ - !ruby/object:Gem::Version
154
+ version: '0'
155
+ segments:
156
+ - 0
157
+ hash: 4283814709343736854
158
+ requirements: []
159
+ rubyforge_project:
160
+ rubygems_version: 1.8.24
161
+ signing_key:
162
+ specification_version: 3
163
+ summary: Read basic information about your laptop's battery
164
+ test_files:
165
+ - spec/batt/reader_spec.rb
166
+ - spec/spec_helper.rb