batt 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/CHANGELOG.md CHANGED
@@ -1,3 +1,9 @@
1
+ 0.2.0 (2013/08/04)
2
+ ---
3
+
4
+ * added new `meter` action for rendering a meter
5
+ * created new `Batt::Formatter::Tmux` class for doing tmux color more easily
6
+
1
7
  0.1.0 (2013/07/13)
2
8
  ---
3
9
 
data/README.md CHANGED
@@ -4,6 +4,8 @@ Read the battery status from your laptop. Designed to be used in `tmux`'s status
4
4
 
5
5
  Currently only supports OSX.
6
6
 
7
+ *Dude I was just realizing I basically cannot live without batt now... it is dope* -- Rich Soni
8
+
7
9
  ## Installation
8
10
 
9
11
  Add this line to your application's Gemfile:
data/lib/batt/app.rb CHANGED
@@ -31,8 +31,8 @@ module Batt
31
31
  c = b.status[:capacity]
32
32
 
33
33
  if options[:tmux]
34
- color = %w(red yellow green)[c.to_i / 33]
35
- puts "#[bg=#{color},fg=black]#{ c }#[default]"
34
+ color = Reader.color_for_capacity(c.to_i)
35
+ puts Formatter::Tmux.format c, :fg => :black, :bg => color
36
36
  else
37
37
  puts c
38
38
  end
@@ -53,6 +53,28 @@ module Batt
53
53
  puts b.status[:remaining]
54
54
  end
55
55
 
56
+ desc "meter", "return an ascii-art battery meter showing the current capacity."
57
+ option :tmux, :type => :boolean, :desc => "Enable tmux colour"
58
+ option :size, :type => :numeric, :desc => "The size of the meter", :default => 5
59
+ def meter
60
+ b = Batt::Reader.new
61
+
62
+ c = b.status[:capacity].to_i
63
+
64
+ meter_size = options[:size]
65
+ meter_filled_level = (meter_size * ( c.to_f / 100 )).round
66
+
67
+ if options[:tmux]
68
+ meter_filled = " " * (meter_filled_level)
69
+ meter_empty = " " * (meter_size - meter_filled_level)
70
+ color = Reader.color_for_capacity(c)
71
+
72
+ puts "[#{ Formatter::Tmux.format meter_filled, :bg => color }#{ meter_empty }]"
73
+ else
74
+ puts "[#{ '|' * meter_filled_level }#{ ' ' * (meter_size - meter_filled_level) }]"
75
+ end
76
+ end
77
+
56
78
  end
57
79
  end
58
80
 
@@ -0,0 +1,36 @@
1
+ module Batt
2
+ module Formatter
3
+ class Tmux
4
+
5
+ class << self
6
+
7
+ def format(string, options={})
8
+ fg = options[:fg]
9
+ bg = options[:bg]
10
+
11
+ color_string = [ bg_color(bg), fg_color(fg) ].compact.join(',')
12
+
13
+ if color_string.length > 0
14
+ "#[#{ color_string }]#{ string }#[default]"
15
+ else
16
+ string
17
+ end
18
+ end
19
+
20
+ def bg_color(color)
21
+ return nil unless color
22
+
23
+ "bg=#{ color }"
24
+ end
25
+
26
+ def fg_color(color)
27
+ return nil unless color
28
+
29
+ "fg=#{ color }"
30
+ end
31
+
32
+ end
33
+
34
+ end
35
+ end
36
+ end
data/lib/batt/reader.rb CHANGED
@@ -47,5 +47,19 @@ module Batt
47
47
 
48
48
  Hash[[:source, :capacity, :status, :remaining].zip(result)]
49
49
  end
50
+
51
+ class << self
52
+
53
+ # Given a capacity percentage, return the color
54
+ def color_for_capacity(capacity)
55
+ case capacity.to_i
56
+ when -100..20 then :red # if there's any weird errors where things go negative, be red.
57
+ when 20..30 then :orange
58
+ when 30..75 then :yellow
59
+ else
60
+ :green
61
+ end
62
+ end
63
+ end
50
64
  end
51
65
  end
data/lib/batt/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Batt
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
data/lib/batt.rb CHANGED
@@ -3,6 +3,7 @@ require "batt/version"
3
3
  require 'cocaine'
4
4
  require 'thor'
5
5
 
6
+ require 'batt/formatter/tmux'
6
7
  require 'batt/reader'
7
8
  require 'batt/app'
8
9
 
@@ -0,0 +1,121 @@
1
+ require 'spec_helper'
2
+
3
+ describe Batt::Formatter::Tmux do
4
+ let(:formatter) { Batt::Formatter::Tmux }
5
+ let(:string_to_format) { 'just testing' }
6
+
7
+ describe "format" do
8
+
9
+ context "when no colors are passed" do
10
+ it "should return just the string" do
11
+ formatter.format(string_to_format).should == string_to_format
12
+ end
13
+ end
14
+
15
+ context "when passed just a fg color" do
16
+ let(:output) { formatter.format(string_to_format, :fg => :red) }
17
+
18
+ it "should not include a bg field" do
19
+ output.should_not match(/bg=/)
20
+ end
21
+
22
+ it "should include the fg color in the string" do
23
+ output.should match(/fg=red/)
24
+ end
25
+
26
+ it "should not have a comma in the color descriptors" do
27
+ output.should_not match(/^#\[.+?,.+?\]/)
28
+ end
29
+
30
+ it "should reset the color to default at the end" do
31
+ output.should match(/#\[default\]$/)
32
+ end
33
+
34
+ it "should include the string being formatted" do
35
+ output.should match(/#{ string_to_format }/)
36
+ end
37
+
38
+ end
39
+
40
+ context "when passed just a bg color" do
41
+ let(:output) { formatter.format(string_to_format, :bg => :red) }
42
+
43
+ it "should not include a fg field" do
44
+ output.should_not match(/fg=/)
45
+ end
46
+
47
+ it "should include the bg color in the string" do
48
+ output.should match(/bg=red/)
49
+ end
50
+
51
+ it "should not have a comma in the color descriptors" do
52
+ output.should_not match(/^#\[.+?,.+?\]/)
53
+ end
54
+
55
+ it "should reset the color to default at the end" do
56
+ output.should match(/#\[default\]$/)
57
+ end
58
+
59
+ it "should include the string being formatted" do
60
+ output.should match(/#{ string_to_format }/)
61
+ end
62
+
63
+ end
64
+
65
+ context "when passed both fg and bg colors" do
66
+ let(:output) { formatter.format(string_to_format, :fg => :black, :bg => :red) }
67
+
68
+ it "should include both fg and bg fields" do
69
+ output.should match(/fg=black/)
70
+ output.should match(/bg=red/)
71
+ end
72
+
73
+ it "should separate the color descriptors with a comma" do
74
+ output.should match(/^#\[.+?,.+?\]/)
75
+ end
76
+
77
+ it "should reset the color to default at the end" do
78
+ output.should match(/#\[default\]$/)
79
+ end
80
+
81
+ it "should include the string being formatted" do
82
+ output.should match(/#{ string_to_format }/)
83
+ end
84
+
85
+ end
86
+
87
+ end
88
+
89
+ describe "bg_color" do
90
+
91
+ it "should return nil if passed nil" do
92
+ formatter.bg_color(nil).should be_nil
93
+ end
94
+
95
+ it "should accept a string color" do
96
+ formatter.bg_color("red").should == 'bg=red'
97
+ end
98
+
99
+ it "should accept a symbol color" do
100
+ formatter.bg_color(:red).should == 'bg=red'
101
+ end
102
+
103
+ end
104
+
105
+ describe "fg_color" do
106
+
107
+ it "should return nil if passed nil" do
108
+ formatter.fg_color(nil).should be_nil
109
+ end
110
+
111
+ it "should accept a string color" do
112
+ formatter.fg_color("red").should == 'fg=red'
113
+ end
114
+
115
+ it "should accept a symbol color" do
116
+ formatter.fg_color(:red).should == 'fg=red'
117
+ end
118
+
119
+ end
120
+
121
+ end
@@ -93,5 +93,48 @@ describe Batt::Reader do
93
93
  status[:remaining].should == '5:36 remaining'
94
94
  end
95
95
  end
96
+
97
+ context "color_for_capacity" do
98
+
99
+ def color_for_capacity(capacity)
100
+ Batt::Reader.color_for_capacity(capacity)
101
+ end
102
+
103
+ it "should be :red when -1" do
104
+ color_for_capacity(-1).should == :red
105
+ end
106
+
107
+ it "should be :red when 15" do
108
+ color_for_capacity(15).should == :red
109
+ end
110
+
111
+ it "should be :red when == 20" do
112
+ color_for_capacity(20).should == :red
113
+ end
114
+
115
+ it "should be :orange when between 20 and 30" do
116
+ color_for_capacity(25).should == :orange
117
+ end
118
+
119
+ it "should be :orange when == 30" do
120
+ color_for_capacity(30).should == :orange
121
+ end
122
+
123
+ it "should be :yellow when == 50" do
124
+ color_for_capacity(50).should == :yellow
125
+ end
126
+
127
+ it "should be :green when 80" do
128
+ color_for_capacity(80).should == :green
129
+ end
130
+
131
+ it "should be :green when 100" do
132
+ color_for_capacity(100).should == :green
133
+ end
134
+
135
+ it "should be :green when 101" do
136
+ color_for_capacity(101).should == :green
137
+ end
138
+ end
96
139
  end
97
140
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: batt
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-07-13 00:00:00.000000000 Z
12
+ date: 2013-08-04 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: cocaine
@@ -127,8 +127,10 @@ files:
127
127
  - bin/batt
128
128
  - lib/batt.rb
129
129
  - lib/batt/app.rb
130
+ - lib/batt/formatter/tmux.rb
130
131
  - lib/batt/reader.rb
131
132
  - lib/batt/version.rb
133
+ - spec/batt/formatter/tmux_spec.rb
132
134
  - spec/batt/reader_spec.rb
133
135
  - spec/spec_helper.rb
134
136
  homepage: https://github.com/spikegrobstein/batt
@@ -146,7 +148,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
146
148
  version: '0'
147
149
  segments:
148
150
  - 0
149
- hash: 2248559496602016421
151
+ hash: -2829731468714431913
150
152
  required_rubygems_version: !ruby/object:Gem::Requirement
151
153
  none: false
152
154
  requirements:
@@ -155,7 +157,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
155
157
  version: '0'
156
158
  segments:
157
159
  - 0
158
- hash: 2248559496602016421
160
+ hash: -2829731468714431913
159
161
  requirements: []
160
162
  rubyforge_project:
161
163
  rubygems_version: 1.8.24
@@ -163,5 +165,6 @@ signing_key:
163
165
  specification_version: 3
164
166
  summary: Read basic information about your laptop's battery
165
167
  test_files:
168
+ - spec/batt/formatter/tmux_spec.rb
166
169
  - spec/batt/reader_spec.rb
167
170
  - spec/spec_helper.rb