paint 0.8.1 → 0.8.2

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG.rdoc CHANGED
@@ -1,4 +1,8 @@
1
- === 0.8.1 ==
1
+ === 0.8.2
2
+ * Paint.[] with only a single string argument does not colorize the string anymore, but returns the plain string
3
+ * New pseudo color :random - returns a random ansi color
4
+
5
+ === 0.8.1
2
6
  * Improve rgb function with better gray scale values
3
7
  * Add Paint.mode:
4
8
  * Set to false to deactivate colorizing
@@ -7,5 +11,5 @@
7
11
  * Tries to automatically detect your terminal's features
8
12
  * Minor changes
9
13
 
10
- === 0.8.0 ===
14
+ === 0.8.0
11
15
  * Initial release
data/README.rdoc CHANGED
@@ -10,8 +10,8 @@ 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 (ansi default)
14
- * 16 colors (ansi default with bright effect)
13
+ * 8 colors (only ansi colors)
14
+ * 16 colors (only ansi colors, combined with bright effect)
15
15
 
16
16
  == Setup
17
17
 
@@ -39,24 +39,15 @@ The first argument given to Paint.[] is the string to colorize (if the object is
39
39
  Paint['Ruby', "gold", "snow"] # Paint supports rgb.txt color names, note that the arguments are strings (:yellow != "yellow")!
40
40
  Paint['Ruby', "#123456"] # html like definitions are possible.
41
41
  Paint['Ruby', "fff"] # another html hex definition
42
- Paint['Ruby'] # don't pass any argument to get one of eight random ansi foreground colors
42
+ Paint['Ruby', :random] # pass :random to get one of eight random ansi foreground colors
43
43
  Paint['Ruby', :inverse] # swaps fore- and background
44
44
  Paint['Ruby', :italic, :encircle, :rapid_blink, :overline] # probably not supported effects
45
+ Paint['Ruby'] # don't pass any argument and the string will not be changed
45
46
 
46
47
  If you pass multiple colors, the first one is taken as foreground color and the second one defines the background color (all others will be ignored). To only change the background color, you have to pass a <tt>nil</tt> first. Effects can be passed in any order.
47
48
 
48
49
  To see more examples, checkout the specs.
49
50
 
50
- == Paint.mode
51
-
52
- You can choose between three ways to use <tt>Paint.[]</tt> by setting <tt>Paint.mode</tt> to one of the following:
53
- * 256: full support
54
- * 16: don't use 256 colors, but the ansi 8 ones (combined with bright effect)
55
- * 8: don't use 256 colors, but the ansi 8 ones (combined without bright effect)
56
- * false: don't colorize at all
57
-
58
- 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.
59
-
60
51
  == More details about terminal colors and effects
61
52
 
62
53
  Terminal colors/effects are set by {ansi escape sequences}[http://en.wikipedia.org/wiki/ANSI_escape_code]. These are strings that look like this: <tt>\e[X;X;X;X;X]m</tt> where X are integers with some meaning. For example, 0 means reset, 31 means red foreground and 41 red background. When you tell Paint to use one of the eight ansi base colors as foreground color, it just inserts a number between 30 and 37 in the sequence. The following colors are available:
@@ -105,6 +96,16 @@ Also see {en.wikipedia.org/wiki/ANSI_escape_code}[http://en.wikipedia.org/wiki/A
105
96
  54) :frame_off, :encircle_off
106
97
  55) :overline_off
107
98
 
99
+ == Paint.mode
100
+
101
+ You can choose between three ways to use <tt>Paint.[]</tt> by setting <tt>Paint.mode</tt> to one of the following:
102
+ * 256: full support
103
+ * 16: don't use 256 colors, but the ansi eight ones (combined with bright effect)
104
+ * 8: don't use 256 colors, but the ansi eight ones
105
+ * false: don't colorize at all
106
+
107
+ 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
+
108
109
  == Shortcuts
109
110
 
110
111
  Now for the fancy part: Color shortcuts for your gems and scripts! Note: You don't have to use them (and only stick to <tt>Paint.[]</tt>) ;)
data/lib/paint.rb CHANGED
@@ -58,7 +58,7 @@ module Paint
58
58
  # Takes a string and color options and colorizes the string
59
59
  # See README.rdoc for details
60
60
  def [](string, *args)
61
- if mode
61
+ if mode && !args.empty?
62
62
  color(*args) + string.to_s + NOTHING
63
63
  else
64
64
  string.to_s
@@ -68,56 +68,55 @@ module Paint
68
68
  # Sometimes, you only need the color
69
69
  # Used by []
70
70
  def color(*options)
71
- return '' unless mode
71
+ return '' if !mode || options.empty?
72
72
  mix = []
73
73
  color_seen = false
74
74
 
75
- if options.empty?
76
- mix << random(false) # random foreground color
77
- else
78
- options.each{ |option|
79
- case option
80
- when Symbol
81
- if ANSI_EFFECTS.keys.include?(option)
82
- mix << effect(option)
83
- elsif ANSI_COLORS.keys.include?(option)
84
- mix << simple(option, color_seen)
85
- color_seen = true
86
- else
87
- raise ArgumentError, "Unknown color or effect: #{ option }"
88
- end
89
-
90
- when Array
91
- if option.size == 3 && option.all?{ |n| n.is_a? Numeric }
92
- mix << rgb(*(option + [color_seen])) # 1.8 workaround
93
- color_seen = true
94
- else
95
- raise ArgumentError, "Array argument must contain 3 numerals"
96
- end
97
-
98
- when ::String
99
- if option =~ /^#?(?:[0-9a-f]{3}){1,2}$/
100
- mix << hex(option, color_seen)
101
- color_seen = true
102
- else
103
- mix << rgb_name(option, color_seen)
104
- color_seen = true
105
- end
106
-
107
- when Numeric
108
- integer = option.to_i
109
- color_seen = true if (30..49).include?(integer)
110
- mix << integer
111
-
112
- when nil
75
+ options.each{ |option|
76
+ case option
77
+ when Symbol
78
+ if option == :random
79
+ mix << random(color_seen)
80
+ color_seen = true
81
+ elsif ANSI_EFFECTS.keys.include?(option)
82
+ mix << effect(option)
83
+ elsif ANSI_COLORS.keys.include?(option)
84
+ mix << simple(option, color_seen)
113
85
  color_seen = true
114
-
115
86
  else
116
- raise ArgumentError, "Invalid argument: #{ option.inspect }"
87
+ raise ArgumentError, "Unknown color or effect: #{ option }"
88
+ end
117
89
 
90
+ when Array
91
+ if option.size == 3 && option.all?{ |n| n.is_a? Numeric }
92
+ mix << rgb(*(option + [color_seen])) # 1.8 workaround
93
+ color_seen = true
94
+ else
95
+ raise ArgumentError, "Array argument must contain 3 numerals"
118
96
  end
119
- }
120
- end
97
+
98
+ when ::String
99
+ if option =~ /^#?(?:[0-9a-f]{3}){1,2}$/
100
+ mix << hex(option, color_seen)
101
+ color_seen = true
102
+ else
103
+ mix << rgb_name(option, color_seen)
104
+ color_seen = true
105
+ end
106
+
107
+ when Numeric
108
+ integer = option.to_i
109
+ color_seen = true if (30..49).include?(integer)
110
+ mix << integer
111
+
112
+ when nil
113
+ color_seen = true
114
+
115
+ else
116
+ raise ArgumentError, "Invalid argument: #{ option.inspect }"
117
+
118
+ end
119
+ }
121
120
 
122
121
  wrap(*mix)
123
122
  end
@@ -127,8 +126,8 @@ module Paint
127
126
  # * 256 - 256 colors
128
127
  # * 16 - only ansi colors
129
128
  # * false - no colorization!
130
- def mode() @mode.nil? ? detect_mode : @mode end
131
- def mode=(val) @mode = val end
129
+ def mode() ( !defined?(@mode) || @mode.nil? ) ? detect_mode : @mode end
130
+ def mode=(val) @mode = val end
132
131
 
133
132
  # Adds ansi sequence
134
133
  def wrap(*ansi_codes)
@@ -40,7 +40,7 @@ module Paint
40
40
  if args.empty? then color_code else color_code + Array(args).join + NOTHING end
41
41
  end
42
42
  }
43
- private *shortcuts.keys unless shortcuts.empty?
43
+ private(*shortcuts.keys) unless shortcuts.empty?
44
44
  end
45
45
 
46
46
  # include variations, defined in child modules
data/lib/paint/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Paint
2
- VERSION = '0.8.1'
2
+ VERSION = '0.8.2'
3
3
  end
data/spec/paint_spec.rb CHANGED
@@ -4,8 +4,8 @@ describe 'Paint.[]' do
4
4
  end
5
5
 
6
6
  context '(with no options)' do
7
- it 'colorizes using a random ansi foreground color' do
8
- Paint['J-_-L'].should =~ /\e\[3\dmJ-_-L\e\[0m/
7
+ it "doesn't colorize at all" do
8
+ Paint['J-_-L'].should == "J-_-L"
9
9
  end
10
10
  end
11
11
 
@@ -37,6 +37,10 @@ describe 'Paint.[]' do
37
37
  it 'understands a non-hex string as rgb color name (rgb.txt) and use it as foreground color' do
38
38
  Paint['J-_-L', "medium purple"].should == "\e[38;5;141mJ-_-L\e[0m"
39
39
  end
40
+
41
+ it 'colorizes using a random ansi foreground color' do
42
+ Paint['J-_-L', :random].should =~ /\e\[3\dmJ-_-L\e\[0m/
43
+ end
40
44
  end
41
45
 
42
46
  context '(with two colors)' do
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: paint
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.8.1
5
+ version: 0.8.2
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-07 00:00:00 Z
13
+ date: 2011-07-08 00:00:00 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rspec