ccp 0.4.4 → 0.4.5

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.
@@ -5,6 +5,8 @@ module Ccp
5
5
  # Ccp::Utils::Colorize::Fore.red("hello")
6
6
  # Ccp::Utils::Colorize::Back.red("hello")
7
7
  # Ccp::Utils::Colorize.red("hello") # same as Fore
8
+ # Ccp::Utils::Colorize.strip(ansi_color_string)
9
+ # Ccp::Utils::Colorize::Bar.success("[%s]", 10, 0.5) # => "[|||| ]"
8
10
 
9
11
  module Fore
10
12
  CLEAR = "\e[0m"
@@ -60,6 +62,79 @@ module Ccp
60
62
  extend self
61
63
  end
62
64
 
65
+ module Bar
66
+ class Progress
67
+ def initialize(colors, format, size, widths, chars = nil)
68
+ @colors = colors.must.coerced(Array, Symbol=>lambda{|x| [x, :black]}) # [:green, :black]
69
+ @format = format.must(String) # "Mem[%sMB]"
70
+ @size = size.must(Fixnum) # 73
71
+ @widths = widths.must(Fixnum, Float, Array) # [4004,24105]
72
+ @chars = chars.must(Array) { ["|", " "] }
73
+
74
+ if @widths.is_a?(Array) and @widths.size != 2
75
+ raise "widths.size expected %d, but got %d" % [2, @widths.size]
76
+ end
77
+ @chars.size == 2 or raise "chars.size expected %d, but got %d" % [2, @chars.size]
78
+ @colors.size == 2 or raise "colors.size expected %d, but got %d" % [2, @colors.size]
79
+
80
+ @label = label(@widths) # "4004/24105"
81
+ @rate = rate(@widths) # 0.16
82
+ end
83
+
84
+ def to_s
85
+ rest = @size - @format.size + 2 - @label.size
86
+ return @format % @label if rest <= 0
87
+ return @format % (bar(rest) + @label)
88
+ end
89
+
90
+ private
91
+ def bar(max)
92
+ o = @chars.first * ((max * @rate).ceil) # "||||"
93
+ x = @chars.last * (max - o.size) # " "
94
+ Colorize.__send__(@colors.first, o) + Colorize.__send__(@colors.last, x)
95
+ end
96
+
97
+ def label(width)
98
+ p = lambda{|x| x.is_a?(Float) ? ("%.1f" % x) : x.to_s }
99
+
100
+ case width
101
+ when Float
102
+ return("%.1f%%" % [width*100])
103
+ when Array
104
+ return width.map{|_| p[_]}.join("/")
105
+ else
106
+ return p[width] + "%"
107
+ end
108
+ end
109
+
110
+ def rate(x)
111
+ r = case x
112
+ when Fixnum ; x / 100.0
113
+ when Float ; x
114
+ when Array ; (v, a) = x; rate(v.to_f/a)
115
+ else ; raise "error: rate got #{x.class}"
116
+ end
117
+ return [[0.0, r].max, 1.0].min
118
+ end
119
+ end
120
+
121
+ def green (*args) Progress.new(:green , *args).to_s; end
122
+ def blue (*args) Progress.new(:blue , *args).to_s; end
123
+ def yellow(*args) Progress.new(:yellow, *args).to_s; end
124
+ def red (*args) Progress.new(:red , *args).to_s; end
125
+
126
+ alias :success :green
127
+ alias :info :blue
128
+ alias :warning :yellow
129
+ alias :danger :red
130
+
131
+ extend self
132
+ end
133
+
134
+ def self.strip(string)
135
+ string.gsub(/\x1B\[[0-9;]*[mK]/, '')
136
+ end
137
+
63
138
  include Fore
64
139
  extend Fore
65
140
  end
@@ -39,7 +39,7 @@ module Ccp
39
39
  raise Ccp::Failed, "#{cmd.class} should write #{key} but not found"
40
40
  end
41
41
  TestFailed::Differ.new(exp, got, key).execute
42
- raise Ccp::Failed, "[FATAL] %s expected %s, but got %s" % [cmd.class, exp_type.inspect, got_type.inspect]
42
+ raise Ccp::Failed, "[FATAL] %s expected %s, but got %s" % [cmd.class, exp.class, got.class]
43
43
  }
44
44
  end
45
45
  end
@@ -1,3 +1,3 @@
1
1
  module Ccp
2
- VERSION = "0.4.4"
2
+ VERSION = "0.4.5"
3
3
  end
@@ -0,0 +1,76 @@
1
+ # -*- coding: utf-8 -*-
2
+ require 'spec_helper'
3
+
4
+ describe Ccp::Utils::Colorize::Bar do
5
+ delegate :strip, :to=>"Ccp::Utils::Colorize"
6
+ delegate :success, :to=>"Ccp::Utils::Colorize::Bar"
7
+
8
+ ######################################################################
9
+ ### API
10
+
11
+ subject { Ccp::Utils::Colorize::Bar }
12
+ it { should respond_to(:success) }
13
+ it { should respond_to(:info) }
14
+ it { should respond_to(:warning) }
15
+ it { should respond_to(:danger) }
16
+
17
+ it { should respond_to(:green) }
18
+ it { should respond_to(:blue) }
19
+ it { should respond_to(:yellow) }
20
+ it { should respond_to(:red) }
21
+
22
+ ######################################################################
23
+ ### Success
24
+
25
+ describe ".success" do
26
+ subject { strip(success(*args)) }
27
+
28
+ context '("Mem[%sMB]", 73, [4004,24105])' do
29
+ let(:args) { ["Mem[%sMB]", 73, [4004,24105]] }
30
+ it { should == "Mem[|||||||||| 4004/24105MB]" }
31
+ end
32
+
33
+ context '("Mem[%s]", 73, 0.16)' do
34
+ let(:args) { ["Mem[%s]", 73, 0.16] }
35
+ it { should == "Mem[||||||||||| 16.0%]" }
36
+ end
37
+
38
+ context '"Mem[%s]", 73, 16' do
39
+ let(:args) { ["Mem[%s]", 73, 16] }
40
+ it { should == "Mem[||||||||||| 16%]" }
41
+ end
42
+
43
+ ######################################################################
44
+ ### invalid value
45
+
46
+ context '("Mem[%s]", 73, -10)' do
47
+ let(:args) { ["Mem[%s]", 73, -10] }
48
+ it { should == "Mem[ -10%]" }
49
+ end
50
+
51
+ context '("Mem[%s]", 73, -1.2)' do
52
+ let(:args) { ["Mem[%s]", 73, -1.2] }
53
+ it { should == "Mem[ -120.0%]" }
54
+ end
55
+
56
+ context '("Mem[%s]", 73, [-10, 30])' do
57
+ let(:args) { ["Mem[%s]", 73, [-10, 30]] }
58
+ it { should == "Mem[ -10/30]" }
59
+ end
60
+
61
+ context '("Mem[%s]", 73, 300)' do
62
+ let(:args) { ["Mem[%s]", 73, 300] }
63
+ it { should == "Mem[||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||300%]" }
64
+ end
65
+
66
+ context '("Mem[%s]", 73, 1.5)' do
67
+ let(:args) { ["Mem[%s]", 73, 1.5] }
68
+ it { should == "Mem[||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||150.0%]" }
69
+ end
70
+
71
+ context '("Mem[%s]", 73, [1000, 50])' do
72
+ let(:args) { ["Mem[%s]", 73, [1000, 50]] }
73
+ it { should == "Mem[|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||1000/50]" }
74
+ end
75
+ end
76
+ end
@@ -53,4 +53,13 @@ describe Ccp::Utils::Colorize do
53
53
  subject { Object.new.extend(Ccp::Utils::Colorize::Back) }
54
54
  it { should be }
55
55
  end
56
+
57
+ describe ".strip" do
58
+ delegate :green, :strip, :to=>"Ccp::Utils::Colorize"
59
+
60
+ specify "should strip ansi-color codes" do
61
+ strip(green("foo")).should == "foo"
62
+ strip(green("foo") + ":" + green("bar") + ";").should == "foo:bar;"
63
+ end
64
+ end
56
65
  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
4
+ version: 0.4.5
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-14 00:00:00.000000000 Z
12
+ date: 2013-12-24 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport
@@ -308,6 +308,7 @@ files:
308
308
  - spec/spec_helper.rb
309
309
  - spec/storage/loadable_spec.rb
310
310
  - spec/storage/usecase_spec.rb
311
+ - spec/utils/colorize_bar_spec.rb
311
312
  - spec/utils/colorize_spec.rb
312
313
  homepage: http://github.com/maiha/ccp
313
314
  licenses: