colorize 0.7.4 → 0.7.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 317b798812e6ac4ce6a4f841d19ace2486046303
4
- data.tar.gz: a4428a6bd6095977fbb4105bedf856cae76c6618
3
+ metadata.gz: 5daa22026f530e287e74e572407ba4ddf9520104
4
+ data.tar.gz: 3af9a37ec77d130690c976cd1decbf4fc086a321
5
5
  SHA512:
6
- metadata.gz: 3af5be1a215b84c3e6d3e34f66f198e9f39a7aa3a5c57e4aacbfd03744747a7555aa34204ab92f1bcdf2121dd41e68288acbb2321be81b41bf40aa04ed68400d
7
- data.tar.gz: 7da873f65fd383cae435eee5dc52b2ba1676a31fc4e0964f83b65b10faa3316a1d611392f02486851a0046ce5e37ab7467280b7c6a1703c540e12879beccea13
6
+ metadata.gz: bcfa897a7e8bc2ce4fa354e192a22d53a8f81114ebe98ebbc7921ba99496b72b92771ba78ad2c3a1c34acd7c83a6e24c5e146ec92ca314283ba4c80675b78d09
7
+ data.tar.gz: f3f2a9297ab0cac20506b1875e92f3cc0d476a7f053bb6d89941576b22662faa8a9a7970a54d9e64f1738d493e47c0578c35cff963077044cd1e739ebcf8b814
data/CHANGELOG CHANGED
@@ -1,3 +1,7 @@
1
+ == 0.7.5 / 2014-12-11
2
+ * big code refactoring
3
+ * disable_colorization feature added
4
+
1
5
  == 0.7.4 / 2014-12-10
2
6
  * code cleanups
3
7
 
data/README.md CHANGED
@@ -28,9 +28,14 @@ Some usage samples:
28
28
 
29
29
  Class methods:
30
30
 
31
- String.colors - return array of all possible colors names
32
- String.modes - return array of all possible modes
33
- String.color_samples - displays color samples in all combinations
31
+ String.colors - return array of all possible colors names
32
+ String.modes - return array of all possible modes
33
+ String.color_samples - displays color samples in all combinations
34
+ String.disable_colorization - check if colorization is disabled
35
+ String.disable_colorization = false - disable colorization
36
+ String.disable_colorization false - disable colorization
37
+ String.disable_colorization = true - enable colorization
38
+ String.disable_colorization true - enable colorization
34
39
 
35
40
  requirements
36
41
  ------------
@@ -47,7 +52,7 @@ install
47
52
  license
48
53
  -------
49
54
 
50
- Copyright (C) 2007 Michal Kalbarczyk
55
+ Copyright (C) 2007-2015 Michal Kalbarczyk
51
56
 
52
57
  This program is free software; you can redistribute it and/or modify
53
58
  it under the terms of the GNU General Public License as published by
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'colorize'
3
- s.version = '0.7.4'
3
+ s.version = '0.7.5'
4
4
 
5
5
  s.authors = ['fazibear']
6
6
  s.email = 'fazibear@gmail.com'
@@ -21,6 +21,8 @@ Gem::Specification.new do |s|
21
21
  'Rakefile',
22
22
  'colorize.gemspec',
23
23
  'lib/colorize.rb',
24
+ 'lib/colorize/class_methods.rb',
25
+ 'lib/colorize/instance_methods.rb',
24
26
  'test/test_colorize.rb',
25
27
  ]
26
28
  s.test_files = [
@@ -1,197 +1,12 @@
1
+ require File.expand_path('colorize/class_methods', File.dirname(__FILE__))
2
+ require File.expand_path('colorize/instance_methods', File.dirname(__FILE__))
1
3
  #
2
4
  # Colorize String class extension.
3
5
  #
4
6
  class String
7
+ extend Colorize::ClassMethods
8
+ include Colorize::InstanceMethods
5
9
 
6
- #
7
- # Colors Hash
8
- #
9
- COLORS = {
10
- :black => 0,
11
- :red => 1,
12
- :green => 2,
13
- :yellow => 3,
14
- :blue => 4,
15
- :magenta => 5,
16
- :cyan => 6,
17
- :white => 7,
18
- :default => 9,
19
-
20
- :light_black => 60,
21
- :light_red => 61,
22
- :light_green => 62,
23
- :light_yellow => 63,
24
- :light_blue => 64,
25
- :light_magenta => 65,
26
- :light_cyan => 66,
27
- :light_white => 67
28
- }
29
-
30
- #
31
- # Modes Hash
32
- #
33
- MODES = {
34
- :default => 0, # Turn off all attributes
35
- :bold => 1, # Set bold mode
36
- :underline => 4, # Set underline mode
37
- :blink => 5, # Set blink mode
38
- :swap => 7, # Exchange foreground and background colors
39
- :hide => 8 # Hide text (foreground color would be the same as background)
40
- }
41
-
42
- REGEXP_PATTERN = /\033\[([0-9]+);([0-9]+);([0-9]+)m(.+?)\033\[0m|([^\033]+)/m
43
- COLOR_OFFSET = 30
44
- BACKGROUND_OFFSET = 40
45
-
46
- public
47
-
48
- #
49
- # Change color of string
50
- #
51
- # Examples:
52
- #
53
- # puts "This is blue".colorize(:blue)
54
- # puts "This is light blue".colorize(:light_blue)
55
- # puts "This is also blue".colorize(:color => :blue)
56
- # puts "This is light blue with red background".colorize(:color => :light_blue, :background => :red)
57
- # puts "This is light blue with red background".colorize(:light_blue ).colorize( :background => :red)
58
- # puts "This is blue text on red".blue.on_red
59
- # puts "This is red on blue".colorize(:red).on_blue
60
- # puts "This is red on blue and underline".colorize(:red).on_blue.underline
61
- # puts "This is blue text on red".blue.on_red.blink
62
- # puts "This is uncolorized".blue.on_red.uncolorize
63
- #
64
- def colorize(params)
65
- windows_requires
66
-
67
- scan(REGEXP_PATTERN).inject('') do |str, match|
68
- set_defaults(match)
69
- set_from_params(match, params)
70
- str << "\033[#{match[0]};#{match[1]};#{match[2]}m#{match[3]}\033[0m"
71
- end
72
- end
73
-
74
- #
75
- # Return uncolorized string
76
- #
77
- def uncolorize
78
- scan(REGEXP_PATTERN).inject('') do |str, match|
79
- str << (match[3] || match[4])
80
- end
81
- end
82
-
83
- #
84
- # Return true if string is colorized
85
- #
86
- def colorized?
87
- scan(REGEXP_PATTERN).reject(&:last).any?
88
- end
89
-
90
- #
91
- # Make some color and on_color methods
92
- #
93
- COLORS.each_key do |key|
94
- next if key == :default
95
-
96
- define_method key do
97
- colorize(:color => key)
98
- end
99
-
100
- define_method "on_#{key}" do
101
- colorize(:background => key)
102
- end
103
- end
104
-
105
- #
106
- # Methods for modes
107
- #
108
- MODES.each_key do |key|
109
- next if key == :default
110
-
111
- define_method key do
112
- colorize(:mode => key)
113
- end
114
- end
115
-
116
- private
117
-
118
- #
119
- # Require windows libs
120
- #
121
- def windows_requires
122
- begin
123
- require 'Win32/Console/ANSI' if RUBY_VERSION < "2.0.0" && RUBY_PLATFORM =~ /win32/
124
- rescue LoadError
125
- raise 'You must gem install win32console to use colorize on Windows'
126
- end
127
- end
128
-
129
- #
130
- # Set default colors
131
- #
132
- def set_defaults(match)
133
- match[0] ||= MODES[:default]
134
- match[1] ||= COLORS[:default] + COLOR_OFFSET
135
- match[2] ||= COLORS[:default] + BACKGROUND_OFFSET
136
- match[3] ||= match[4]
137
- end
138
-
139
- #
140
- # Set color from params
141
- #
142
- def set_from_params(match, params)
143
- case params
144
- when Hash then set_from_hash(match, params)
145
- when Symbol then set_from_symbol(match, params)
146
- end
147
- end
148
-
149
- #
150
- # Set colors from params hash
151
- #
152
- def set_from_hash(match, hash)
153
- match[0] = MODES[hash[:mode]] if hash[:mode] && MODES[hash[:mode]]
154
- match[1] = COLORS[hash[:color]] + COLOR_OFFSET if hash[:color] && COLORS[hash[:color]]
155
- match[2] = COLORS[hash[:background]] + BACKGROUND_OFFSET if hash[:background] && COLORS[hash[:background]]
156
- end
157
-
158
- #
159
- # Set color from params symbol
160
- #
161
- def set_from_symbol(match, symbol)
162
- match[1] = COLORS[symbol] + COLOR_OFFSET if symbol && COLORS[symbol]
163
- end
164
-
165
- class << self
166
-
167
- #
168
- # Return array of available modes used by colorize method
169
- #
170
- def modes
171
- MODES.keys
172
- end
173
-
174
- #
175
- # Return array of available colors used by colorize method
176
- #
177
- def colors
178
- COLORS.keys
179
- end
180
-
181
- #
182
- # Display color samples
183
- #
184
- def color_samples
185
- String.colors.permutation(2).each do |background, color|
186
- puts "#{color.inspect.rjust(15)} on #{background.inspect.ljust(15)}".colorize(:color => color, :background => background) + "#{color.inspect.rjust(15)} on #{background.inspect.ljust(15)}"
187
- end
188
- end
189
-
190
- #
191
- # Method removed, raise NoMethodError
192
- #
193
- def color_matrix(txt = '')
194
- fail NoMethodError, '#color_matrix method was removed, try #color_samples instead'
195
- end
196
- end
10
+ color_methods
11
+ modes_methods
197
12
  end
@@ -0,0 +1,114 @@
1
+ module Colorize
2
+ module ClassMethods
3
+
4
+ #
5
+ # Property to disable colorization
6
+ #
7
+ def disable_colorization(value = nil)
8
+ if value.nil?
9
+ @disable_colorization || false
10
+ else
11
+ @disable_colorization = (value || false)
12
+ end
13
+ end
14
+
15
+ #
16
+ # Setter for disable colorization
17
+ #
18
+ def disable_colorization=(value)
19
+ @disable_colorization = (value || false)
20
+ end
21
+
22
+ #
23
+ # Color codes hash
24
+ #
25
+ def color_codes
26
+ {
27
+ :black => 0, :light_black => 60,
28
+ :red => 1, :light_red => 61,
29
+ :green => 2, :light_green => 62,
30
+ :yellow => 3, :light_yellow => 63,
31
+ :blue => 4, :light_blue => 64,
32
+ :magenta => 5, :light_magenta => 65,
33
+ :cyan => 6, :light_cyan => 66,
34
+ :white => 7, :light_white => 67,
35
+ :default => 9
36
+ }
37
+ end
38
+
39
+ #
40
+ # Return array of available colors used by colorize
41
+ #
42
+ def colors
43
+ color_codes.keys
44
+ end
45
+
46
+ #
47
+ # Mode codes hash
48
+ #
49
+ def mode_codes
50
+ {
51
+ :default => 0, # Turn off all attributes
52
+ :bold => 1, # Set bold mode
53
+ :underline => 4, # Set underline mode
54
+ :blink => 5, # Set blink mode
55
+ :swap => 7, # Exchange foreground and background colors
56
+ :hide => 8 # Hide text (foreground color would be the same as background)
57
+ }
58
+ end
59
+
60
+ #
61
+ # Return array of available modes used by colorize
62
+ #
63
+ def modes
64
+ mode_codes.keys
65
+ end
66
+
67
+ #
68
+ # Color and on_color methods
69
+ #
70
+ def color_methods
71
+ colors.each do |key|
72
+ next if key == :default
73
+
74
+ define_method key do
75
+ colorize(:color => key)
76
+ end
77
+
78
+ define_method "on_#{key}" do
79
+ colorize(:background => key)
80
+ end
81
+ end
82
+ end
83
+
84
+ #
85
+ # Modes methods
86
+ #
87
+ def modes_methods
88
+ modes.each do |key|
89
+ next if key == :default
90
+
91
+ define_method key do
92
+ colorize(:mode => key)
93
+ end
94
+ end
95
+ end
96
+
97
+ #
98
+ # Display color samples
99
+ #
100
+ def color_samples
101
+ colors.permutation(2).each do |background, color|
102
+ sample_text = "#{color.inspect.rjust(15)} on #{background.inspect.ljust(15)}"
103
+ puts "#{sample_text.colorize(:color => color, :background => background)} #{sample_text}"
104
+ end
105
+ end
106
+
107
+ #
108
+ # Method removed, raise NoMethodError
109
+ #
110
+ def color_matrix(txt = '')
111
+ fail NoMethodError, '#color_matrix method was removed, try #color_samples instead'
112
+ end
113
+ end
114
+ end
@@ -0,0 +1,123 @@
1
+ module Colorize
2
+ module InstanceMethods
3
+
4
+ #
5
+ # Change color of string
6
+ #
7
+ # Examples:
8
+ #
9
+ # puts "This is blue".colorize(:blue)
10
+ # puts "This is light blue".colorize(:light_blue)
11
+ # puts "This is also blue".colorize(:color => :blue)
12
+ # puts "This is light blue with red background".colorize(:color => :light_blue, :background => :red)
13
+ # puts "This is light blue with red background".colorize(:light_blue ).colorize( :background => :red)
14
+ # puts "This is blue text on red".blue.on_red
15
+ # puts "This is red on blue".colorize(:red).on_blue
16
+ # puts "This is red on blue and underline".colorize(:red).on_blue.underline
17
+ # puts "This is blue text on red".blue.on_red.blink
18
+ # puts "This is uncolorized".blue.on_red.uncolorize
19
+ #
20
+ def colorize(params)
21
+ return self if self.class.disable_colorization
22
+ require_windows_libs
23
+ scan_for_colors.inject('') do |str, match|
24
+ defaults_colors(match)
25
+ colors_from_params(match, params)
26
+ str << "\033[#{match[0]};#{match[1]};#{match[2]}m#{match[3]}\033[0m"
27
+ end
28
+ end
29
+
30
+ #
31
+ # Return uncolorized string
32
+ #
33
+ def uncolorize
34
+ scan_for_colors.inject('') do |str, match|
35
+ str << (match[3] || match[4])
36
+ end
37
+ end
38
+
39
+ #
40
+ # Return true if string is colorized
41
+ #
42
+ def colorized?
43
+ scan_for_colors.reject(&:last).any?
44
+ end
45
+
46
+ private
47
+
48
+ #
49
+ # Set default colors
50
+ #
51
+ def defaults_colors(match)
52
+ match[0] ||= mode(:default)
53
+ match[1] ||= color(:default)
54
+ match[2] ||= background_color(:default)
55
+ match[3] ||= match[4]
56
+ end
57
+
58
+ #
59
+ # Set color from params
60
+ #
61
+ def colors_from_params(match, params)
62
+ case params
63
+ when Hash then colors_from_hash(match, params)
64
+ when Symbol then color_from_symbol(match, params)
65
+ end
66
+ end
67
+
68
+ #
69
+ # Set colors from params hash
70
+ #
71
+ def colors_from_hash(match, hash)
72
+ match[0] = mode(hash[:mode]) if mode(hash[:mode])
73
+ match[1] = color(hash[:color]) if color(hash[:color])
74
+ match[2] = background_color(hash[:background]) if background_color(hash[:background])
75
+ end
76
+
77
+ #
78
+ # Set color from params symbol
79
+ #
80
+ def color_from_symbol(match, symbol)
81
+ match[1] = color(symbol) if color(symbol)
82
+ end
83
+
84
+ #
85
+ # Color for foreground (offset 30)
86
+ #
87
+ def color(color)
88
+ self.class.color_codes[color] + 30 if self.class.color_codes[color]
89
+ end
90
+
91
+ #
92
+ # Color for background (offset 40)
93
+ #
94
+ def background_color(color)
95
+ self.class.color_codes[color] + 40 if self.class.color_codes[color]
96
+ end
97
+
98
+ #
99
+ # Mode
100
+ #
101
+ def mode(mode)
102
+ self.class.mode_codes[mode]
103
+ end
104
+
105
+ #
106
+ # Scan for colorized string
107
+ #
108
+ def scan_for_colors
109
+ scan(/\033\[([0-9]+);([0-9]+);([0-9]+)m(.+?)\033\[0m|([^\033]+)/m)
110
+ end
111
+
112
+ #
113
+ # Require windows libs
114
+ #
115
+ def require_windows_libs
116
+ begin
117
+ require 'Win32/Console/ANSI' if RUBY_VERSION < "2.0.0" && RUBY_PLATFORM =~ /win32/
118
+ rescue LoadError
119
+ raise 'You must gem install win32console to use colorize on Windows'
120
+ end
121
+ end
122
+ end
123
+ end
@@ -88,9 +88,48 @@ class TestColorize < Test::Unit::TestCase
88
88
  assert_equal 'This is blue text on red'.freeze.blue.on_red.blink,
89
89
  "\e[5;34;41mThis is blue text on red\e[0m"
90
90
  end
91
-
91
+
92
92
  def test_new_line
93
93
  assert_equal "This is blue\ntext on red".freeze.blue.on_red.blink,
94
94
  "\e[5;34;41mThis is blue\ntext on red\e[0m"
95
95
  end
96
+
97
+ def test_disable_colorization_with_setter
98
+ String.disable_colorization = true
99
+ assert_equal String.disable_colorization, true
100
+ String.disable_colorization = false
101
+ end
102
+
103
+ def test_disable_colorize_with_setter
104
+ String.disable_colorization = true
105
+
106
+ assert_equal String.disable_colorization, true
107
+
108
+ assert_equal 'This is blue after disabling'.blue,
109
+ 'This is blue after disabling'
110
+
111
+ String.disable_colorization = false
112
+
113
+ assert_equal 'This is blue after enabling'.colorize(:blue),
114
+ "\e[0;34;49mThis is blue after enabling\e[0m"
115
+ end
116
+
117
+ def test_disable_colorize_with_setter
118
+ assert_equal 'This is blue before disabling'.colorize(:blue),
119
+ "\e[0;34;49mThis is blue before disabling\e[0m"
120
+
121
+ String.disable_colorization true
122
+
123
+ assert_equal String.disable_colorization, true
124
+
125
+ assert_equal 'This is blue after disabling'.blue,
126
+ 'This is blue after disabling'
127
+
128
+ String.disable_colorization false
129
+
130
+ assert_equal 'This is blue after enabling'.colorize(:blue),
131
+ "\e[0;34;49mThis is blue after enabling\e[0m"
132
+
133
+ end
134
+
96
135
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: colorize
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.4
4
+ version: 0.7.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - fazibear
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-12-10 00:00:00.000000000 Z
11
+ date: 2014-12-16 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Ruby String class extension. Adds methods to set text color, background
14
14
  color and, text effects on ruby console and command line output, using ANSI escape
@@ -24,6 +24,8 @@ files:
24
24
  - Rakefile
25
25
  - colorize.gemspec
26
26
  - lib/colorize.rb
27
+ - lib/colorize/class_methods.rb
28
+ - lib/colorize/instance_methods.rb
27
29
  - test/test_colorize.rb
28
30
  homepage: http://github.com/fazibear/colorize
29
31
  licenses: