color_echo 0.6.0 → 0.7.0

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: 120d4b59dd2fd42500f5936e8d0a48ecaa022640
4
- data.tar.gz: 121a6a958155b520f09fb50ed975e77776f3c3dd
3
+ metadata.gz: 0200fec858bea1e367e23aa75a6fc4fff1f81df5
4
+ data.tar.gz: 6a7e5cecc4aa224c739287eacbb4ce26211bc6b2
5
5
  SHA512:
6
- metadata.gz: 5382227d1dbdc744c494ec55d4d20b760e44dfac856e68878f5a780b7bc86bb39aa14a120ae8a2e0cb35ffcb6841ce9fe16d56106278d956993e575a38e6ed56
7
- data.tar.gz: 5679f4b1a3e012ed0860509e959bbe8c72a160e9b139d33b60888039e1de4c88b9cd423e985ffe448478a03d3f0aaf767ec1971e6690ec3f9cfa61cd1d806afc
6
+ metadata.gz: 53ed7b16d9c2a4ab792eaaa8df91d7886eed13079d501f94f4dc1a588163cd31b78726949bf093fb233517fb9d67f0e17d95852344737aa947d5faeca517b80c
7
+ data.tar.gz: d1ca2fccfd254044ffa771404fea3b85ea8082c6601a0c8174361f4a0223a313a00af0139da7e36e260d81446e27ca025038d3ef848790efb3e6a6ae97d5465c
data/README.md CHANGED
@@ -1,9 +1,9 @@
1
1
  # color_echo
2
- Decorate the command line output with ANSI escape sequence.
3
- This Library will extend the Kernel module's functions(#print, #puts, #p).
4
- Required StringIO.
2
+ Decorate the command line output with ANSI escape sequence.
3
+ String that is output by "print, puts, p" method is decorated.
4
+ It is also can to decorate only your specified words!
5
5
 
6
- Version: 0.6.0
6
+ Version: 0.7.0
7
7
  Compliant Rubys Version: 1.9.3, 2.0.0, 2.1.0 (for Linux)
8
8
  License: MIT
9
9
  Gems repository: http://rubygems.org/gems/color_echo
@@ -18,13 +18,36 @@ Or install it yourself as:
18
18
 
19
19
  $ gem install color_echo
20
20
 
21
- ## Usage
22
21
 
23
- ### module functions
22
+ ## Can to select 256 colors!
23
+ Please install color_echo and do `color_echo -l`.
24
+ ex.) CE.ch :index197, :index230
25
+ ![screen shot](/images/color_index.png)
26
+ ![screen shot](/images/color_index2.png)
27
+
28
+ ### You can run on the command line
29
+ <pre>
30
+ * Usage
31
+ color_echo [options]
32
+
33
+ * Options
34
+ -v:
35
+ Display version of color_echo.
36
+
37
+ --symbol_list, -s
38
+ Display symbol list that can to specify.
39
+
40
+ --index_list, -l
41
+ Display color index list that can to specify.
42
+
43
+ If your server using the rbenv, You have to do `rbenv rehash`.
44
+ </pre>
45
+
46
+ ## module functions
24
47
  #### CE.ch_fg :symbol
25
48
  Change the foreground color to the your specified color.
26
- - Alias -> fg (available in v0.2.0 on and after)
27
- - Parameter -> Symbol
49
+ - Alias -> fg
50
+ - Parameter -> symbol
28
51
  - Return -> self
29
52
 
30
53
  * symbol list:
@@ -36,8 +59,6 @@ Change the foreground color to the your specified color.
36
59
  * magenta
37
60
  * cyan
38
61
  * white
39
-
40
- available in v0.3.0 on and after.
41
62
  * gray
42
63
  * h_red
43
64
  * h_green
@@ -46,8 +67,6 @@ Change the foreground color to the your specified color.
46
67
  * h_magenta
47
68
  * h_cyan
48
69
  * h_white
49
-
50
- available in v0.4.0 on and after.
51
70
  * index[1-256]
52
71
 
53
72
  ex.) CE.ch_fg :red #=> foreground color will be changed red
@@ -57,8 +76,8 @@ ex.) CE.ch_fg :red #=> foreground color will be changed red
57
76
 
58
77
  #### CE.ch_bg :symbol
59
78
  Change the background color to the your specified color.
60
- - Alias -> bg (available in v0.2.0 on and after)
61
- - Parameter -> Symbol
79
+ - Alias -> bg
80
+ - Parameter -> symbol
62
81
  - Return -> self
63
82
 
64
83
  * symbol list:
@@ -70,8 +89,6 @@ Change the background color to the your specified color.
70
89
  * magenta
71
90
  * cyan
72
91
  * white
73
-
74
- available in v0.3.0 on and after.
75
92
  * gray
76
93
  * h_red
77
94
  * h_green
@@ -80,8 +97,6 @@ Change the background color to the your specified color.
80
97
  * h_magenta
81
98
  * h_cyan
82
99
  * h_white
83
-
84
- available in v0.4.0 on and after.
85
100
  * index[1-256]
86
101
 
87
102
  ex.) CE.ch_bg :white #=> background color will be changed white
@@ -90,8 +105,8 @@ ex.) CE.ch_bg :white #=> background color will be changed white
90
105
 
91
106
  #### CE.ch_tx :symbol
92
107
  Change the text attribute to the your specified decoration.
93
- - Alias -> tx (available in v0.2.0 on and after)
94
- - Parameter -> Symbol
108
+ - Alias -> tx
109
+ - Parameter -> symbol
95
110
  - Return -> self
96
111
 
97
112
  * symbol list:
@@ -106,34 +121,32 @@ ex.) CE.ch_tx :blink #=> text blink on
106
121
 
107
122
  #### CE.ch foreground [,background [,text_attribute]]
108
123
  Change collectively.
109
- This method is available in v0.1.0 on and after.
110
- - Parameter foreground -> Symbol
111
- - Parameter background -> Symbol
112
- - Parameter text_attribute -> Symbol
124
+ - Parameter foreground -> Symbol or nil
125
+ - Parameter background -> Symbol or nil
126
+ - Parameter text_attribute -> Symbol
113
127
  - Return -> self
114
128
 
115
129
  ex.) CE.ch :white, :green
130
+ ex.) CE.ch :h_red, nil, :blink
116
131
 
117
-
118
- #### CE.reset([target={:fg|:bg]})
132
+ #### CE.reset([target={:fg|:bg|:pickup]})
119
133
  Reset to set the escape sequence.
120
- - Alias -> off, disable (available in v0.1.0 on and after)
121
- - Parameter target -> Symbol (available in v0.3.0 on and after)
134
+ - Alias -> off, disable
135
+ - Parameter target -> symbol
122
136
  - Return -> self
123
137
 
124
- ex.) CE.reset :fg #=> foreground color will be reset.
125
- ex.) CE.reset :bg #=> background color will be reset.
126
- ex.) CE.reset #=> All reset the set escape sequence.
138
+ ex.) CE.reset :fg #=> foreground color will be reset.
139
+ ex.) CE.reset :bg #=> background color will be reset.
140
+ ex.) CE.reset :pickup #=> pickup text will be reset.
141
+ ex.) CE.reset #=> All reset the set escape sequence.
127
142
 
128
143
 
129
144
  #### CE.once
130
145
  Reset automatically after once output.
131
- This method is available in v0.5.0 on and after.
132
146
  - Return -> self
133
147
 
134
148
  #### CE.times(cnt)
135
149
  Reset automatically after cnt times output.
136
- This method is available in v0.5.0 on and after.
137
150
  - Parameter cnt -> Integer
138
151
  - Return -> self
139
152
 
@@ -154,40 +167,35 @@ puts "switch off"
154
167
 
155
168
  #### CE.unuse
156
169
  Force ignore the function of this library.
157
- This method is available in v0.1.0 on and after.
158
170
 
159
171
 
160
172
  #### CE.rainbow
161
173
  Text color will change to rainbow color.
162
- ~~String Object only. Non-string object is excluded.~~
163
- -> Can v0.2.4 on and after be able to specify a non-string when rainbow mode.
164
- This method is available in v0.2.0 on and after.
165
174
 
166
175
 
167
- ### Can to select 256 colors!
168
- Please install color_echo and do `color_echo -l`.
169
- ex.) CE.ch :index197, :index230
170
- ![screen shot](/images/color_index.png)
171
- ![screen shot](/images/color_index2.png)
176
+ ## You can to decorate your specified words!
172
177
 
173
- ### You can run on the command line
174
- <pre>
175
- * Usage
176
- color_echo [options]
177
-
178
- * Options
179
- -v:
180
- Display version of color_echo.
178
+ #### CE.pickup(target, foreground=:red, backgruond=nil, *textattr)
179
+ To decorate the words that specfied in the String or Regexp or Array of them.
180
+ If state of enable rainbow mode, This feature is disabled.
181
+ - Parameter target -> String|Regexp|Array
182
+ - Parameter foreground -> Symbol or nil
183
+ - Parameter background -> Symbol or nil
184
+ - Parameter text_attribute -> Symbol
185
+ - Return -> self
181
186
 
182
- --symbol_list, -s
183
- Display symbol list that can to specify.
184
-
185
- --index_list, -l
186
- Display color index list that can to specify.
187
-
188
- If your server using the rbenv, You have to do `rbenv rehash`.
189
- This is available in v0.6.0 on and after.
190
- </pre>
187
+ ```ruby
188
+ CE.fg(:h_cyan).pickup("color_echo", :h_white, :red, :underscore).pickup("COLOR_ECHO", :h_yellow)
189
+
190
+ puts <<EOS
191
+ xxxxxxxxxxxxxxxxxcolor_echoxxxxxxxxxxxxxxxxxxxxxxxx
192
+ xxxxcolor_echoxxxxxxxCOLOR_ECHOxxxxxxxxxxxxxxxxxxxx
193
+ xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
194
+ xxxxxxxxxxxxxxxxxcolor_echoxxxxxxxxxxcolor_echoxxxx
195
+ xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
196
+ EOS
197
+ ```
198
+ ![screen shot](/images/pickup1.png)
191
199
 
192
200
  ### Example
193
201
  ```ruby
@@ -275,6 +283,10 @@ puts "Disable rainbow mode."
275
283
  ![screen shot](/images/screenshot.png)
276
284
 
277
285
  ## Release Note
286
+ * v0.7.0, 2014-01-08
287
+ * Added new method -> pickup
288
+ * Added new symbol that can to specify in reset method of first parameter -> CE.reset(:pickup)
289
+
278
290
  * v0.6.0, 2014-01-05
279
291
  * Added command line tool.
280
292
 
@@ -295,3 +307,11 @@ puts "Disable rainbow mode."
295
307
 
296
308
  * v0.2.3, 2014-12-02
297
309
  * Fixed small bugs.
310
+
311
+ * v0.2.0
312
+ * Added new method -> rainbow
313
+ * Added some method alias.
314
+
315
+ * v0.1.0
316
+ * Added new method -> ch, unuse
317
+ * Added some method alias.
@@ -1,11 +1,11 @@
1
1
  module CE
2
2
  LIBS_NAME = "color_echo"
3
- VERSION = "0.6.0"
3
+ VERSION = "0.7.0"
4
4
  SUMMARY = "Decorate the command line output with ANSI escape sequence."
5
5
  DOCS_PAGE = "https://github.com/khotta/color_echo"
6
6
  DESCRIPTION = <<EOS
7
7
  #{SUMMARY}
8
- This Library will extend the Kernel module's functions(#print, #puts, #p).
9
- Required StringIO.
8
+ String that is output by "print, puts, p" method is decorated.
9
+ It is also can to decorate only your specified words!
10
10
  EOS
11
11
  end
@@ -1,38 +1,47 @@
1
1
  module CE
2
+ # is allow to use?
2
3
  # @return bool
3
- def available?
4
- return @@enable && isset?
4
+ def enable?
5
+ return @@enable
5
6
  end
6
7
 
8
+ # is allow to use? and is set code?
7
9
  # @return bool
8
- def isset?
9
- return get_start_code != "" || @@rainbow
10
+ def available?
11
+ return @@enable && isset?
10
12
  end
11
13
 
14
+ # is set code?
12
15
  # @return bool
13
- def enable?
14
- return @@enable
16
+ def isset?
17
+ return get_start_code != "" || @@pickup_list.size > 0 || @@rainbow
15
18
  end
16
19
 
20
+ # do not allow to use
17
21
  # @return void
18
22
  def unuse
19
23
  @@enable = false
20
24
  end
21
25
 
26
+ # reset code
22
27
  # @return self
23
28
  def reset(target=nil)
24
- if (target == :fg)
29
+ if target == :fg
25
30
  @@code_fg_color = ""
26
31
 
27
- elsif (target == :bg)
32
+ elsif target == :bg
28
33
  @@code_bg_color = ""
29
34
 
35
+ elsif target == :pickup
36
+ @@pickup_list = {}
37
+
30
38
  else
31
39
  @@code_bg_color = ""
32
40
  @@code_fg_color = ""
33
41
  @@code_text_attr = ""
34
42
  @@code_rainbow = ""
35
43
  @@rainbow = false
44
+ @@pickup_list = {}
36
45
  end
37
46
 
38
47
  return self
@@ -50,54 +59,105 @@ module CE
50
59
  times(1)
51
60
  end
52
61
 
53
- # @param Symbol name
62
+ # @param symbol name
54
63
  # @return self
55
64
  def ch_fg(name)
56
65
  return nil if !name.instance_of?(Symbol)
66
+
57
67
  @@rainbow = false if @@rainbow
58
68
  @@code_fg_color = convert_to_code("ForeGround", name)
59
69
 
60
70
  return self
61
71
  end
62
72
 
63
- # @param Symbol name
73
+ # @param symbol name
64
74
  # @return self
65
75
  def ch_bg(name)
66
76
  return nil if !name.instance_of?(Symbol)
77
+
67
78
  @@rainbow = false if @@rainbow
68
79
  @@code_bg_color = convert_to_code("BackGround", name)
69
80
 
70
81
  return self
71
82
  end
72
83
 
73
- # @param array name - array of Symbols
84
+ # @param array name : Array of Symbols
74
85
  # @return self
75
86
  def ch_tx(*names)
76
87
  @@rainbow = false if @@rainbow
77
88
  names = names[0] if names[0].instance_of?(Array)
78
89
 
79
90
  names.each do |name|
80
- return nil if !name.instance_of?(Symbol)
91
+ next if !name.instance_of?(Symbol)
81
92
  @@code_text_attr += convert_to_code("TextAttr", name)
82
93
  end
83
94
 
84
95
  return self
85
96
  end
86
97
 
87
- # @param Symbol fg
88
- # @param Symbol bg
89
- # @param array txs
98
+ # @param symbol fg
99
+ # @param symbol bg
100
+ # @param symbol|array txs
90
101
  # @return self
91
102
  def ch(fg, bg=nil, txs=nil)
92
103
  ch_fg(fg)
93
104
  ch_bg(bg)
94
- ch_tx(*txs)
105
+ ch_tx(*txs) # passing expand
95
106
 
96
107
  return self
97
108
  end
98
109
 
99
- # return start color sequence code
100
- # @return String
110
+ # to decorate only the specified target
111
+ # @param string|regexp|array target
112
+ # @param symbol fg
113
+ # @param symbol bg
114
+ # @param symbol tx
115
+ # @return self
116
+ def pickup(target, fg=:red, bg=nil, *txs)
117
+ key = target.object_id.to_s
118
+ @@pickup_list[key] = {}
119
+
120
+ if target.is_a?(Array)
121
+ @@pickup_list[key][:patterns] = target
122
+ else
123
+ @@pickup_list[key][:patterns] = [target]
124
+ end
125
+
126
+ if fg.instance_of?(Symbol)
127
+ code_fg = convert_to_code("ForeGround", fg)
128
+ else
129
+ code_fg = ""
130
+ end
131
+
132
+ if bg.instance_of?(Symbol)
133
+ code_bg = convert_to_code("BackGround", bg)
134
+ else
135
+ code_bg = ""
136
+ end
137
+
138
+ code_tx = ""
139
+ if txs.size > 0
140
+ txs = txs[0] if txs[0].instance_of?(Array)
141
+ txs.each do |name|
142
+ next if !name.instance_of?(Symbol)
143
+ code_tx += convert_to_code("TextAttr", name)
144
+ end
145
+ end
146
+
147
+ @@pickup_list[key][:code] = code_fg + code_bg + code_tx
148
+
149
+ return self
150
+ end
151
+
152
+ # change mode to rainbow
153
+ # @return void
154
+ def rainbow
155
+ @@rainbow = true
156
+ @@code_rainbow = @@code_bg_color + @@code_text_attr
157
+ end
158
+
159
+ # return start escape sequence code
160
+ # @return string
101
161
  def get_start_code
102
162
  if @@rainbow
103
163
  return @@code_rainbow
@@ -112,23 +172,16 @@ module CE
112
172
  end
113
173
 
114
174
  # add reset & start code to line feed code
115
- # @param String input
175
+ # @param string input
116
176
  def add_reset_line_feed(input)
117
177
  input.gsub!(/#{$/}/, CE::get_reset_code + $/ + CE::get_start_code)
118
178
  input += CE::get_reset_code
119
179
  return input
120
180
  end
121
181
 
122
- # change mode to rainbow
123
- # @return void
124
- def rainbow
125
- @@rainbow = true
126
- @@code_rainbow = @@code_bg_color + @@code_text_attr
127
- end
128
-
129
182
  # add code to be a rainbow color
130
- # @param String text
131
- # @return String
183
+ # @param string text
184
+ # @return string
132
185
  def add_rainbow(text)
133
186
  cnt = 0
134
187
  output = get_start_code
@@ -164,11 +217,44 @@ module CE
164
217
  return output
165
218
  end
166
219
 
220
+ # add pickup code
221
+ # @param string text
222
+ # @return string
223
+ def add_pickup_code(text)
224
+ @@pickup_list.each_pair do |key, hash|
225
+ patterns = hash[:patterns]
226
+ code = hash[:code]
227
+
228
+ # repeat to each specified pickup pattern
229
+ patterns.each do |pattern|
230
+ # pattern is Regexp
231
+ if pattern.is_a?(Regexp)
232
+ after_text = ""
233
+ # global match
234
+ (text.scan(pattern)).size.times do
235
+ pattern =~ text
236
+ after_text += $` + code + $& + get_reset_code + get_start_code
237
+ text = $'
238
+ end
239
+ text = after_text + text
240
+
241
+ # pattern is String
242
+ else
243
+ text.gsub!(pattern, code + pattern + get_reset_code + get_start_code)
244
+ end
245
+ end
246
+ end
247
+
248
+ return text
249
+ end
250
+
167
251
  # get sequence code by symbol
168
- # @param String module_name
169
- # @param Symbol name
170
- # @return String
252
+ # @param string module_name
253
+ # @param symbol name
254
+ # @return string
171
255
  def convert_to_code(module_name, name)
256
+ return "" if (name == nil || name == "")
257
+
172
258
  begin
173
259
  cname = name.to_s.swapcase
174
260
 
@@ -185,12 +271,17 @@ module CE
185
271
  return code
186
272
  end
187
273
 
188
- # @return Proc
274
+ # @return proc
189
275
  def task
190
276
  return @@task
191
277
  end
192
278
 
193
- require_relative "alias_method.rb"
279
+ # method alias
280
+ alias_method :off, :reset
281
+ alias_method :disable, :reset
282
+ alias_method :fg, :ch_fg
283
+ alias_method :bg, :ch_bg
284
+ alias_method :tx, :ch_tx
194
285
 
195
286
  module_function :available?,
196
287
  :isset?,
@@ -208,11 +299,13 @@ module CE
208
299
  :ch_tx,
209
300
  :tx,
210
301
  :ch,
302
+ :pickup,
303
+ :rainbow,
211
304
  :get_start_code,
212
305
  :get_reset_code,
213
306
  :add_reset_line_feed,
214
307
  :add_rainbow,
215
- :rainbow,
308
+ :add_pickup_code,
216
309
  :convert_to_code,
217
310
  :task
218
311
  end
@@ -6,6 +6,7 @@ module CE
6
6
  @@code_text_attr = ""
7
7
  @@code_rainbow = ""
8
8
  @@rainbow = false
9
+ @@pickup_list = {}
9
10
  @@cnt_limit = 0
10
11
 
11
12
  @@print = method :print
@@ -18,9 +19,6 @@ module CE
18
19
  strio = StringIO.new
19
20
  $stdout = strio
20
21
 
21
- # output color sequence
22
- $stdout.print get_start_code if !@@rainbow
23
-
24
22
  # call original method
25
23
  eval("@@#{caller_locations(2).first.label}").call(*arg)
26
24
 
@@ -29,11 +27,24 @@ module CE
29
27
 
30
28
  # output to STDOUT
31
29
  if @@rainbow
32
- $stdout.print add_rainbow(strio.string)
30
+ output = add_rainbow(strio.string)
33
31
  else
34
- $stdout.print add_reset_line_feed(strio.string)
32
+ output = strio.string
33
+
34
+ # decorate pickup
35
+ if @@pickup_list.size > 0
36
+ output = add_pickup_code(output)
37
+ end
38
+
39
+ # add start code in haed
40
+ output = get_start_code + output
41
+
42
+ # add reset code in front of line feed
43
+ output = add_reset_line_feed(output)
35
44
  end
36
45
 
46
+ $stdout.print output
47
+
37
48
  # auto off
38
49
  if @@cnt_limit > 0
39
50
  @@cnt_limit -= 1
metadata CHANGED
@@ -1,19 +1,19 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: color_echo
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.0
4
+ version: 0.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - khotta
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-01-05 00:00:00.000000000 Z
11
+ date: 2015-01-08 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: |
14
14
  Decorate the command line output with ANSI escape sequence.
15
- This Library will extend the Kernel module's functions(#print, #puts, #p).
16
- Required StringIO.
15
+ String that is output by "print, puts, p" method is decorated.
16
+ It is also can to decorate only your specified words!
17
17
  email:
18
18
  executables:
19
19
  - color_echo
@@ -24,7 +24,6 @@ files:
24
24
  - README.md
25
25
  - bin/color_echo
26
26
  - lib/color_echo.rb
27
- - lib/color_echo/alias_method.rb
28
27
  - lib/color_echo/const.rb
29
28
  - lib/color_echo/module/background.rb
30
29
  - lib/color_echo/module/foreground.rb
@@ -1,7 +0,0 @@
1
- module CE
2
- alias_method :off, :reset
3
- alias_method :disable, :reset
4
- alias_method :fg, :ch_fg
5
- alias_method :bg, :ch_bg
6
- alias_method :tx, :ch_tx
7
- end