ccp 0.4.5 → 0.4.6
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/lib/ccp/utils/colorize.rb +74 -12
- data/lib/ccp/version.rb +1 -1
- data/spec/utils/colorize_meter_spec.rb +48 -0
- metadata +3 -2
data/lib/ccp/utils/colorize.rb
CHANGED
@@ -62,23 +62,32 @@ module Ccp
|
|
62
62
|
extend self
|
63
63
|
end
|
64
64
|
|
65
|
+
######################################################################
|
66
|
+
### Bar
|
67
|
+
|
65
68
|
module Bar
|
66
69
|
class Progress
|
67
|
-
def initialize(colors, format, size,
|
70
|
+
def initialize(colors, format, size, vals, chars = nil)
|
68
71
|
@colors = colors.must.coerced(Array, Symbol=>lambda{|x| [x, :black]}) # [:green, :black]
|
69
72
|
@format = format.must(String) # "Mem[%sMB]"
|
70
73
|
@size = size.must(Fixnum) # 73
|
71
|
-
@
|
72
|
-
@chars = chars.must(Array) { ["|"
|
74
|
+
@vals = vals.must(Fixnum, Float, Array) # [4004,24105]
|
75
|
+
@chars = chars.must(Array) { ["|"] }
|
76
|
+
@count = @vals.is_a?(Array) ? @vals.size : 1 # value count (fill gaps with Fixnum and Array)
|
73
77
|
|
74
|
-
if @
|
75
|
-
raise "
|
78
|
+
if @vals.is_a?(Array) and @vals.size < 2
|
79
|
+
raise "vals.size expected >= %d, but got %d" % [2, @vals.size]
|
80
|
+
end
|
81
|
+
if @chars.size < @count
|
82
|
+
@chars += [@chars.last] * (@vals.size - @chars.size)
|
83
|
+
end
|
84
|
+
if @chars.size < @count + 1
|
85
|
+
@chars += [' ']
|
76
86
|
end
|
77
|
-
@
|
78
|
-
@colors.size == 2 or raise "colors.size expected %d, but got %d" % [2, @colors.size]
|
87
|
+
@colors.size >= 2 or raise "colors.size expected >= %d, but got %d" % [2, @colors.size]
|
79
88
|
|
80
|
-
@label = label(@
|
81
|
-
@
|
89
|
+
@label = label(@vals) # "4004/24105"
|
90
|
+
@rates = rates(@vals) # [0.16]
|
82
91
|
end
|
83
92
|
|
84
93
|
def to_s
|
@@ -89,7 +98,7 @@ module Ccp
|
|
89
98
|
|
90
99
|
private
|
91
100
|
def bar(max)
|
92
|
-
o = @chars.first * ((max * @
|
101
|
+
o = @chars.first * ((max * @rates).ceil) # "||||"
|
93
102
|
x = @chars.last * (max - o.size) # " "
|
94
103
|
Colorize.__send__(@colors.first, o) + Colorize.__send__(@colors.last, x)
|
95
104
|
end
|
@@ -107,11 +116,15 @@ module Ccp
|
|
107
116
|
end
|
108
117
|
end
|
109
118
|
|
110
|
-
def
|
119
|
+
def rates(vals)
|
120
|
+
return _rate(vals)
|
121
|
+
end
|
122
|
+
|
123
|
+
def _rate(x)
|
111
124
|
r = case x
|
112
125
|
when Fixnum ; x / 100.0
|
113
126
|
when Float ; x
|
114
|
-
when Array ; (v, a) = x;
|
127
|
+
when Array ; (v, a) = x; _rate(v.to_f/a)
|
115
128
|
else ; raise "error: rate got #{x.class}"
|
116
129
|
end
|
117
130
|
return [[0.0, r].max, 1.0].min
|
@@ -131,6 +144,55 @@ module Ccp
|
|
131
144
|
extend self
|
132
145
|
end
|
133
146
|
|
147
|
+
######################################################################
|
148
|
+
### Meter
|
149
|
+
|
150
|
+
module Meter
|
151
|
+
class Percent < Bar::Progress
|
152
|
+
private
|
153
|
+
def bar(max)
|
154
|
+
buf = ''
|
155
|
+
sum = 0
|
156
|
+
@rates.each_with_index do |r, i|
|
157
|
+
v = @chars[i] || @chars.first
|
158
|
+
c = @colors[i] || @chars.first
|
159
|
+
s = (max * r).ceil
|
160
|
+
s = max - sum unless sum + s <= max # validate sum should <= max
|
161
|
+
o = v * s # "||||"
|
162
|
+
sum += s
|
163
|
+
buf << Colorize.__send__(c, o)
|
164
|
+
end
|
165
|
+
buf << @chars.last * (max - sum)
|
166
|
+
return buf
|
167
|
+
end
|
168
|
+
|
169
|
+
def rates(vals)
|
170
|
+
rs = vals.map{|v| v /= 100.0; [[0.0, v].max, 1.0].min }
|
171
|
+
# validate sum should <= 1.0
|
172
|
+
|
173
|
+
sum = 0.0
|
174
|
+
rs.each_with_index do |r,i|
|
175
|
+
available = 1.0 - sum
|
176
|
+
if r > available
|
177
|
+
rs[i] = available
|
178
|
+
(i+1 ... rs.size).each{|_| rs[_] = 0.0}
|
179
|
+
return rs
|
180
|
+
end
|
181
|
+
sum += r
|
182
|
+
end
|
183
|
+
return rs
|
184
|
+
end
|
185
|
+
|
186
|
+
def label(*)
|
187
|
+
""
|
188
|
+
end
|
189
|
+
end
|
190
|
+
|
191
|
+
def percent(fmt, size, vals, cols, c = nil) Percent.new(cols, fmt, size, vals, c).to_s; end
|
192
|
+
|
193
|
+
extend self
|
194
|
+
end
|
195
|
+
|
134
196
|
def self.strip(string)
|
135
197
|
string.gsub(/\x1B\[[0-9;]*[mK]/, '')
|
136
198
|
end
|
data/lib/ccp/version.rb
CHANGED
@@ -0,0 +1,48 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
require 'spec_helper'
|
3
|
+
|
4
|
+
describe Ccp::Utils::Colorize::Meter do
|
5
|
+
delegate :strip, :to=>"Ccp::Utils::Colorize"
|
6
|
+
delegate :percent, :to=>"Ccp::Utils::Colorize::Meter"
|
7
|
+
|
8
|
+
######################################################################
|
9
|
+
### API
|
10
|
+
|
11
|
+
subject { Ccp::Utils::Colorize::Meter }
|
12
|
+
it { should respond_to(:percent) }
|
13
|
+
|
14
|
+
######################################################################
|
15
|
+
### Percent
|
16
|
+
|
17
|
+
describe ".percent" do
|
18
|
+
subject { strip(percent(*args)) }
|
19
|
+
let(:colors) { [:green, :blue, :yellow] }
|
20
|
+
let(:chars) { ['|','x','-', ' '] }
|
21
|
+
let(:vals) { [10,8,71] }
|
22
|
+
let(:fmt) { "Mem[%s24G]" }
|
23
|
+
let(:size) { 75 }
|
24
|
+
def args; [fmt, size, vals, colors, chars]; end
|
25
|
+
|
26
|
+
context 'normal' do
|
27
|
+
it { should == "Mem[|||||||xxxxxx------------------------------------------------ 24G]" }
|
28
|
+
end
|
29
|
+
|
30
|
+
context "(chars:['|'])" do
|
31
|
+
let(:chars) { ['|'] }
|
32
|
+
it { should == "Mem[||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| 24G]" }
|
33
|
+
end
|
34
|
+
|
35
|
+
######################################################################
|
36
|
+
### bound error
|
37
|
+
|
38
|
+
context '(percent exceeds 100%)' do
|
39
|
+
let(:vals) { [10,8,100] }
|
40
|
+
it { should == "Mem[|||||||xxxxxx------------------------------------------------------24G]" }
|
41
|
+
end
|
42
|
+
|
43
|
+
context '(percent contains minus value)' do
|
44
|
+
let(:vals) { [10,-8,71] }
|
45
|
+
it { should == "Mem[|||||||------------------------------------------------ 24G]" }
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ccp
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.4.
|
4
|
+
version: 0.4.6
|
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-12-
|
12
|
+
date: 2013-12-26 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activesupport
|
@@ -309,6 +309,7 @@ files:
|
|
309
309
|
- spec/storage/loadable_spec.rb
|
310
310
|
- spec/storage/usecase_spec.rb
|
311
311
|
- spec/utils/colorize_bar_spec.rb
|
312
|
+
- spec/utils/colorize_meter_spec.rb
|
312
313
|
- spec/utils/colorize_spec.rb
|
313
314
|
homepage: http://github.com/maiha/ccp
|
314
315
|
licenses:
|