paint 0.8.2 → 0.8.3

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.
@@ -1,3 +1,8 @@
1
+ === 0.8.3
2
+ * Paint.[] also accepts uppercased hex strings (gh#2)
3
+ * Performance tweaks (thanks to murphy) (gh#4, #5)
4
+ * API change: deactivate colorizing with Paint.mode = 0
5
+
1
6
  === 0.8.2
2
7
  * Paint.[] with only a single string argument does not colorize the string anymore, but returns the plain string
3
8
  * New pseudo color :random - returns a random ansi color
@@ -5,7 +10,7 @@
5
10
  === 0.8.1
6
11
  * Improve rgb function with better gray scale values
7
12
  * Add Paint.mode:
8
- * Set to false to deactivate colorizing
13
+ * Set to 0 to deactivate colorizing
9
14
  * Set to 16 or 8 and all color generation methods will generate simple ansi colors
10
15
  * Set to 256 for 256 color support
11
16
  * Tries to automatically detect your terminal's features
@@ -10,8 +10,9 @@ Paint manages terminal colors and effects for you. It combines the strengths of
10
10
  * Custom shortcuts can be defined and flexibly mixed in
11
11
  * Fall-back modes for non-256-color terminals (Paint.mode), supported modes:
12
12
  * 256 colors
13
- * 8 colors (only ansi colors)
14
13
  * 16 colors (only ansi colors, combined with bright effect)
14
+ * 8 colors (only ansi colors)
15
+ * 0 colors (deactivate)
15
16
 
16
17
  == Setup
17
18
 
@@ -102,7 +103,7 @@ You can choose between three ways to use <tt>Paint.[]</tt> by setting <tt>Paint.
102
103
  * 256: full support
103
104
  * 16: don't use 256 colors, but the ansi eight ones (combined with bright effect)
104
105
  * 8: don't use 256 colors, but the ansi eight ones
105
- * false: don't colorize at all
106
+ * 0: don't colorize at all
106
107
 
107
108
  Paint tries to detect automatically the proper value, but this is still experimental. Please open an issue if <tt>Paint.detect_mode</tt> yields a wrong value for you.
108
109
 
@@ -169,4 +170,8 @@ Mainly influenced by rainbow[https://github.com/sickill/rainbow] and {term-ansic
169
170
 
170
171
  Copyright (c) 2011 Jan Lelis, http://rbjl.net, released under the MIT license.
171
172
 
173
+ Contributions[https://github.com/janlelis/paint/contributors] by and thanks to:
174
+ * {murphy}[https://github.com/rubychan]
175
+
172
176
  J-_-L
177
+
data/Rakefile CHANGED
@@ -49,3 +49,68 @@ end
49
49
  # rdoc.rdoc_files.include('README*')
50
50
  # rdoc.rdoc_files.include('lib/**/*.rb')
51
51
  #end
52
+
53
+ desc "Run a Benchmark"
54
+ task :benchmark do
55
+ require 'benchmark'
56
+ require 'term/ansicolor'
57
+ class String
58
+ include Term::ANSIColor
59
+ end
60
+
61
+ require 'rainbow'
62
+ $:.unshift '../lib'
63
+ require 'paint'
64
+
65
+ n = 100_000
66
+ colors = [:black, :red, :green, :yellow, :blue, :magenta, :cyan]
67
+ def colors.next
68
+ @index ||= 0
69
+ at((@index += 1) % size)
70
+ end
71
+ Benchmark.bmbm 30 do |results|
72
+ string = 'Ruby is awesome!'
73
+
74
+ results.report 'cycle' do
75
+ n.times do
76
+ colors.next
77
+ end
78
+ end
79
+
80
+ results.report 'paint' do
81
+ n.times do
82
+ Paint[string, colors.next]
83
+ end
84
+ end
85
+
86
+ results.report 'term-ansicolor' do
87
+ n.times do
88
+ string.send(colors.next)
89
+ end
90
+ end
91
+
92
+ results.report 'rainbow' do
93
+ n.times do
94
+ string.color(colors.next)
95
+ end
96
+ end
97
+
98
+ results.report 'paint with background' do
99
+ n.times do
100
+ Paint[string, colors.next, colors.next]
101
+ end
102
+ end
103
+
104
+ results.report 'term-ansicolor with background' do
105
+ n.times do
106
+ string.send(colors.next).send("on_#{colors.next}")
107
+ end
108
+ end
109
+
110
+ results.report 'rainbow with background' do
111
+ n.times do
112
+ string.color(colors.next).background(colors.next)
113
+ end
114
+ end
115
+ end
116
+ end
@@ -54,35 +54,64 @@ module Paint
54
54
  :overline_off => 55,
55
55
  }
56
56
 
57
+ # cache
58
+ ANSI_COLORS_FOREGROUND = {
59
+ :black => '30',
60
+ :red => '31',
61
+ :green => '32',
62
+ :yellow => '33',
63
+ :blue => '34',
64
+ :magenta => '35',
65
+ :cyan => '36',
66
+ :white => '37',
67
+ :default => '39',
68
+ }
69
+
70
+ # cache
71
+ ANSI_COLORS_BACKGROUND = {
72
+ :black => '40',
73
+ :red => '41',
74
+ :green => '42',
75
+ :yellow => '43',
76
+ :blue => '44',
77
+ :magenta => '45',
78
+ :cyan => '46',
79
+ :white => '47',
80
+ :default => '49',
81
+ }
82
+
57
83
  class << self
58
84
  # Takes a string and color options and colorizes the string
59
85
  # See README.rdoc for details
60
- def [](string, *args)
61
- if mode && !args.empty?
62
- color(*args) + string.to_s + NOTHING
63
- else
64
- string.to_s
86
+ def [](string, *options)
87
+ return string.to_s if mode.zero? || options.empty?
88
+
89
+ if options.size == 1 && !options.first.respond_to?(:to_ary)
90
+ options = options.first
65
91
  end
92
+
93
+ cache[options] + string.to_s + NOTHING
66
94
  end
67
95
 
68
96
  # Sometimes, you only need the color
69
97
  # Used by []
70
98
  def color(*options)
71
- return '' if !mode || options.empty?
99
+ return '' if mode.zero? || options.empty?
72
100
  mix = []
73
101
  color_seen = false
102
+ colors = ANSI_COLORS_FOREGROUND
74
103
 
75
104
  options.each{ |option|
76
105
  case option
77
106
  when Symbol
78
- if option == :random
79
- mix << random(color_seen)
80
- color_seen = true
81
- elsif ANSI_EFFECTS.keys.include?(option)
107
+ if color = colors[option]
108
+ mix << color
109
+ color_seen = :set
110
+ elsif ANSI_EFFECTS.key?(option)
82
111
  mix << effect(option)
83
- elsif ANSI_COLORS.keys.include?(option)
84
- mix << simple(option, color_seen)
85
- color_seen = true
112
+ elsif option == :random
113
+ mix << random(color_seen)
114
+ color_seen = :set
86
115
  else
87
116
  raise ArgumentError, "Unknown color or effect: #{ option }"
88
117
  end
@@ -90,32 +119,37 @@ module Paint
90
119
  when Array
91
120
  if option.size == 3 && option.all?{ |n| n.is_a? Numeric }
92
121
  mix << rgb(*(option + [color_seen])) # 1.8 workaround
93
- color_seen = true
122
+ color_seen = :set
94
123
  else
95
124
  raise ArgumentError, "Array argument must contain 3 numerals"
96
125
  end
97
126
 
98
127
  when ::String
99
- if option =~ /^#?(?:[0-9a-f]{3}){1,2}$/
128
+ if option =~ /^#?(?:[0-9a-f]{3}){1,2}$/i
100
129
  mix << hex(option, color_seen)
101
- color_seen = true
130
+ color_seen = :set
102
131
  else
103
132
  mix << rgb_name(option, color_seen)
104
- color_seen = true
133
+ color_seen = :set
105
134
  end
106
135
 
107
136
  when Numeric
108
137
  integer = option.to_i
109
- color_seen = true if (30..49).include?(integer)
138
+ color_seen = :set if (30..49).include?(integer)
110
139
  mix << integer
111
140
 
112
141
  when nil
113
- color_seen = true
142
+ color_seen = :set
114
143
 
115
144
  else
116
145
  raise ArgumentError, "Invalid argument: #{ option.inspect }"
117
146
 
118
147
  end
148
+
149
+ if color_seen == :set
150
+ colors = ANSI_COLORS_BACKGROUND
151
+ color_seen = true
152
+ end
119
153
  }
120
154
 
121
155
  wrap(*mix)
@@ -124,10 +158,11 @@ module Paint
124
158
  # This variable influences the color code generation
125
159
  # Currently supported values:
126
160
  # * 256 - 256 colors
127
- # * 16 - only ansi colors
128
- # * false - no colorization!
129
- def mode() ( !defined?(@mode) || @mode.nil? ) ? detect_mode : @mode end
130
- def mode=(val) @mode = val end
161
+ # * 16 - only ansi colors and bright effect
162
+ # * 8 - only ansi colors
163
+ # * 0 - no colorization!
164
+ def mode() @mode ||= detect_mode end
165
+ def mode=(val) cache.clear; @mode = val end
131
166
 
132
167
  # Adds ansi sequence
133
168
  def wrap(*ansi_codes)
@@ -173,12 +208,16 @@ module Paint
173
208
  (background ? 40 : 30) + rand(8)
174
209
  end
175
210
 
176
- # Creates the specified effect by looking it up in Paint::ANSI_COLORS
211
+ # Creates the specified effect by looking it up in Paint::ANSI_EFFECTS
177
212
  def effect(effect_name)
178
213
  ANSI_EFFECTS[effect_name]
179
214
  end
180
215
 
181
216
  private
217
+
218
+ def cache
219
+ @cache ||= Hash.new { |h, k| h[k] = color(*k) }
220
+ end
182
221
 
183
222
  # Returns nearest supported 256-color an rgb value, without fore-/background information
184
223
  # Inspired by the rainbow gem
@@ -38,7 +38,7 @@ module Paint
38
38
  if ENV['ANSICON']
39
39
  16
40
40
  else
41
- false
41
+ 0
42
42
  end
43
43
  else
44
44
  # case ENV['COLORTERM']
@@ -1,3 +1,3 @@
1
1
  module Paint
2
- VERSION = '0.8.2'
2
+ VERSION = '0.8.3'
3
3
  end
@@ -17,6 +17,8 @@ Gem::Specification.new do |s|
17
17
  s.add_development_dependency 'rspec'
18
18
  s.add_development_dependency 'rspec-core'
19
19
  s.add_development_dependency 'rake'
20
+ s.add_development_dependency 'rainbow'
21
+ s.add_development_dependency 'term-ansicolor'
20
22
 
21
23
  len = s.homepage.size
22
24
  s.post_install_message = \
@@ -4,8 +4,8 @@ describe 'Paint.mode' do
4
4
  Paint['J-_-L', 'gold'].should == "\e[38;5;226mJ-_-L\e[0m"
5
5
  end
6
6
 
7
- it "doesn't colorize anything if mode is false" do
8
- Paint.mode = false
7
+ it "doesn't colorize anything if mode is 0" do
8
+ Paint.mode = 0
9
9
  Paint['J-_-L', 'gold'].should == "J-_-L"
10
10
  end
11
11
 
@@ -34,6 +34,10 @@ describe 'Paint.[]' do
34
34
  Paint['J-_-L', "fff"].should == "\e[38;5;255mJ-_-L\e[0m"
35
35
  end
36
36
 
37
+ it 'understands a hex string (with uppercased letters) as rgb color definition and use it as foreground color' do
38
+ Paint['J-_-L', "#4183C4"].should == "\e[38;5;74mJ-_-L\e[0m"
39
+ end
40
+
37
41
  it 'understands a non-hex string as rgb color name (rgb.txt) and use it as foreground color' do
38
42
  Paint['J-_-L', "medium purple"].should == "\e[38;5;141mJ-_-L\e[0m"
39
43
  end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: paint
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.8.2
5
+ version: 0.8.3
6
6
  platform: ruby
7
7
  authors:
8
8
  - Jan Lelis
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-07-08 00:00:00 Z
13
+ date: 2011-07-10 00:00:00 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rspec
@@ -45,6 +45,28 @@ dependencies:
45
45
  version: "0"
46
46
  type: :development
47
47
  version_requirements: *id003
48
+ - !ruby/object:Gem::Dependency
49
+ name: rainbow
50
+ prerelease: false
51
+ requirement: &id004 !ruby/object:Gem::Requirement
52
+ none: false
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: "0"
57
+ type: :development
58
+ version_requirements: *id004
59
+ - !ruby/object:Gem::Dependency
60
+ name: term-ansicolor
61
+ prerelease: false
62
+ requirement: &id005 !ruby/object:Gem::Requirement
63
+ none: false
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: "0"
68
+ type: :development
69
+ version_requirements: *id005
48
70
  description: "Terminal painter / no string extensions / 256 color support / effect support / define custom shortcuts / basic usage: Paint['string', :red, :bright]"
49
71
  email: mail@janlelis.de
50
72
  executables: []