color_echo 2.0.4 → 3.0.0

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5c40fafb369a9dab0acabca7b1803e6ea98fc1a3
4
- data.tar.gz: cf298e3ed99b6a11be36d25b04480fde833cee85
3
+ metadata.gz: d6506be07d0069c30e631fe82b4b2efb44deb11c
4
+ data.tar.gz: 6c5ef32192472f4be0d8c507fb50ad5bdb59e221
5
5
  SHA512:
6
- metadata.gz: 7d0ae0460fac4bc29f849f91f9bb27846d9939d1e98391acbb9547f069c782d3730382e9413e710db4e9967fbd3db082720e1d7b66bf6067bf3bc204cc1ef038
7
- data.tar.gz: 6286cbe9988d02ad0bc075d8041555a68c5355a0f49d91bcd2b44f2e0151d00ecb92ffe54355667bf0c14ed4785437697ab42ad4328715877eb9be502094769e
6
+ metadata.gz: 15e8243c5504922d0fd2bc9bd90acac164f8d028af05d5d3b2e6b92c5af9c56e1ac81f51a1117d9b8dbb2ceac30e09681effd2529ed5c496eeb4268ae5235fc8
7
+ data.tar.gz: a0cc6e4418eff5461d137ebeb36b9898dacbc3d63aca9a3a190499b2056acd9f45d74cc143f1e0483c616c3fbca12e782c5182eb588be381530733e5c01fc35c
@@ -1,4 +1,4 @@
1
- Copyright (c) 2014 Kazuya Hotta
1
+ Copyright (c) 2014,2015 Kazuya Hotta
2
2
 
3
3
  MIT License
4
4
 
data/README.md CHANGED
@@ -1,406 +1,9 @@
1
- # color_echo
2
- Decorate the command line output with ANSI escape sequence.
3
- Text that output by "print, puts, p" method is decorated.
4
- It is also can to decorate only your specified words!
1
+ # Color Echo
2
+ Decorates command line output with ANSI escape sequence.
5
3
 
6
- Version: 2.0.2
7
- Compliant Rubys Version: 2.0.0, 2.1.0 (for Linux)
8
- License: MIT
9
- Gems repository: http://rubygems.org/gems/color_echo
10
- Japanese Document: https://github.com/khotta/color_echo/blob/master/README.ja.md
4
+ Documents and all informations are as below =(^x^=
5
+ http://colorecho.net
11
6
 
12
- ## Installation
13
-
14
- Add this line to your application's Gemfile:
15
-
16
- gem 'color_echo'
17
-
18
- Or install it yourself as:
19
-
20
- $ gem install color_echo
21
-
22
-
23
- ## Can to select 256 colors!
24
- Please install color_echo and do `color_echo -l`.
25
- ex.) CE.ch :index197, :index230
26
- ![screen shot](/images/color_index.png)
27
- ![screen shot](/images/color_index2.png)
28
-
29
- ## You can to decorate your specified words!
30
-
31
- ```ruby
32
- CE.fg(:h_cyan).pickup("color_echo", :h_white, :red, :underscore).pickup("COLOR_ECHO", :h_yellow)
33
-
34
- puts <<EOS
35
- xxxxxxxxxxxxxxxxxcolor_echoxxxxxxxxxxxxxxxxxxxxxxxx
36
- xxxxcolor_echoxxxxxxxCOLOR_ECHOxxxxxxxxxxxxxxxxxxxx
37
- xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
38
- xxxxxxxxxxxxxxxxxcolor_echoxxxxxxxxxxcolor_echoxxxx
39
- xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
40
- EOS
41
- ```
42
- ![screen shot](/images/pickup1.png)
43
-
44
-
45
-
46
- ## Do you want to return words with the escape sequence?
47
-
48
- Please write `require "color_echo/get"` rather than `require "color_echo"`.
49
- ```ruby
50
- require "color_echo/get"
51
-
52
- greet = CE.fg(:yellow).get("Hello")
53
- name = CE.ch(:h_blue, :gray).get("Foo")
54
- myname = CE.tx(:underscore).rainbow.get("Bar")
55
-
56
- puts greet + ", " + name + "!"
57
- puts "My name is " + myname
58
-
59
- output = CE.fg(:blue).pickup(/color$/, :index199).pickup(/^color/, :h_green).get("color color color")
60
- puts "output is -> " + output
61
- ```
62
- ![screen shot](/images/get1.png)
63
-
64
-
65
- ## Command Line Interface
66
- You can use color_echo on command line!
67
- ![screen shot](/images/cli1.png)
68
- ![screen shot](/images/cli2.png)
69
-
70
-
71
- ## module functions
72
-
73
- #### CE.pickup(target, foreground=:red, backgruond=nil, *text_attribute)
74
- To decorate the words that specfied in the String or Regexp or Array of them.
75
- If state of enable rainbow mode, This feature is disabled.
76
- - Parameter target -> string|regexp or array of them
77
- - Parameter foreground -> symbol|nil
78
- - Parameter background -> symbol|nil
79
- - Parameter text_attribute -> symbol or array of them
80
- - Return -> self
81
-
82
-
83
- #### CE.hitline(foreground=nil, background=nil, *text_attribute)
84
- To decorate match lines by CE.pickup method with specified here.
85
- - Parameter foreground -> symbol|nil
86
- - Parameter background -> symbol|nil
87
- - Parameter text_attribute -> symbol or array of them
88
- - Return -> self
89
-
90
-
91
- #### CE.ch_fg(foreground)
92
- Change the foreground color to the your specified color.
93
- - Alias -> fg
94
- - Parameter foreground -> symbol
95
- - Return -> self
96
-
97
- * symbol list of foreground:
98
- * black
99
- * red
100
- * green
101
- * yellow
102
- * blue
103
- * magenta
104
- * cyan
105
- * white
106
- * gray
107
- * h_red
108
- * h_green
109
- * h_yellow
110
- * h_blue
111
- * h_magenta
112
- * h_cyan
113
- * h_white
114
- * index[1-256]
115
-
116
- ex.) CE.ch_fg :red #=> foreground color will be changed red
117
-
118
-
119
-
120
-
121
- #### CE.ch_bg(background)
122
- Change the background color to the your specified color.
123
- - Alias -> bg
124
- - Parameter background -> symbol
125
- - Return -> self
126
-
127
- * symbol list of background:
128
- * black
129
- * red
130
- * green
131
- * yellow
132
- * blue
133
- * magenta
134
- * cyan
135
- * white
136
- * gray
137
- * h_red
138
- * h_green
139
- * h_yellow
140
- * h_blue
141
- * h_magenta
142
- * h_cyan
143
- * h_white
144
- * index[1-256]
145
-
146
- ex.) CE.ch_bg :white #=> background color will be changed white
147
-
148
-
149
-
150
- #### CE.ch_tx(*text_attribute)
151
- Change the text attribute to the your specified decoration.
152
- - Alias -> tx
153
- - Parameter text_attribute -> symbol or array of them
154
- - Return -> self
155
-
156
- * symbol list of text attribute:
157
- * bold
158
- * underscore
159
- * blink
160
- * reverse_video
161
- * concealed
162
-
163
- ex.) CE.ch_tx :blink #=> text blink on
164
-
165
-
166
- #### CE.ch(foreground, background=nil, *text_attribute)
167
- Change collectively.
168
- - Parameter foreground -> symbol|nil
169
- - Parameter background -> symbol|nil
170
- - Parameter text_attribute -> symbol or array of them
171
- - Return -> self
172
-
173
- ex.) CE.ch :white, :green
174
- ex.) CE.ch :h_red, nil, :blink
175
-
176
-
177
- #### CE.reset(scope=:all)
178
- Reset to set the escape sequence.
179
- - Alias -> off, disable
180
- - Parameter scope -> symbol or array of them
181
- - Return -> self
182
-
183
- ex.) CE.reset #=> reset all of the set escape sequence.
184
- ex.) CE.reset :fg #=> foreground color will be reset.
185
- ex.) CE.reset :bg #=> background color will be reset.
186
- ex.) CE.reset :tx #=> text attribute will be reset.
187
- ex.) CE.reset :pickup #=> the pickups text will be reset.
188
- ex.) CE.reset :hitline #=> sequence code list will be reset that is used at match line by CE.pickup.
189
- ex.) CE.reset :rainbow #=> rainbow mode will be reset and swich off.
190
- ex.) CE.reset [:fg. :tx] #=> foreground color and ext attribute will be reset.
191
-
192
-
193
- #### CE.once
194
- Reset automatically after once output.
195
- - Return -> self
196
-
197
-
198
- #### CE.times(cnt)
199
- Reset automatically after cnt times output.
200
- - Parameter cnt -> integer
201
- - Return -> self
202
-
203
- ```ruby
204
- CE.once.ch :h_yellow, :h_red, :underscore
205
- puts "decorated"
206
- puts "switch off"
207
-
208
- puts "\n"
209
-
210
- CE.times(3).rainbow
211
- puts "one"
212
- puts "two"
213
- puts "three"
214
- puts "switch off"
215
- ```
216
- ![screen shot](/images/result_off.png)
217
-
218
-
219
- #### CE.enable_refresh(scope=:all)
220
- Try to remove the sequence code from the given.
221
- - Parameter scope -> symbol
222
- - :all: Try to remove the sequence code from the given always.
223
- - :prematch: If given matches, Try to remove sequence code from it.
224
- - Return -> self
225
-
226
-
227
- #### CE.disable_refresh
228
- Not try to remove the sequence code from the given.
229
- Default is this.
230
- - Return -> self
231
-
232
-
233
- #### CE.unuse
234
- Force ignore the function of this library.
235
-
236
-
237
- #### CE.rainbow
238
- Text color will change to rainbow color.
239
- - Return -> self
240
-
241
-
242
- #### CE.get(text)
243
- Decorate and return argument with ANSI escape sequence.
244
- If you want to use this module function; You have to require "color_echo/get" only!
245
- - Parameter text -> string
246
- - Return -> string
247
-
248
-
249
- ### Example
250
- ```ruby
251
- #
252
- # Example Code
253
- #
254
- #require "color_echo"
255
-
256
- # force ignore the function of this library
257
- #CE.unuse
258
-
259
- CE.ch :yellow
260
- puts "fooooooooo"
261
- puts "baaaaaaaar"
262
- puts "AAAAA", "BBBBB", "CCCCC"
263
-
264
- CE.ch :white, :red
265
- print "fooooooooo"
266
- print "baaaaaaaar"
267
- print "AAAAA", "BBBBB", "CCCCC"
268
-
269
- CE.ch :black, :magenta, :underscore
270
- p "fooooooooo"
271
- p "baaaaaaaar"
272
- p "AAAAA", "BBBBB", "CCCCC"
273
-
274
- ary = ["Duis", "aute", "irure", "dolor", "in", "reprehenderit", "in", "voluptate"]
275
-
276
- CE.off
277
- puts "switch off all colors and attribute."
278
-
279
- CE.fg :red
280
- puts ary
281
-
282
- CE.bg :cyan
283
- print ary
284
-
285
- CE.tx :underscore
286
- p ary
287
-
288
- CE.fg(:black).bg(:white).tx(:blink)
289
- puts <<EOM
290
- Lorem ipsum dolor sit amet,
291
- consectetur adipisicing elit,
292
- sed do eiusmod tempor incididunt
293
- ut labore et dolore magna aliqua.
294
- EOM
295
-
296
- CE.off
297
- CE.rainbow
298
- puts "fooooooooo"
299
- puts "baaaaaaaar"
300
- puts "AAAAA", "BBBBB", "CCCCC"
301
- puts ary
302
-
303
- print "fooooooooo"
304
- print "baaaaaaaar"
305
- print "AAAAA", "BBBBB", "CCCCC"
306
- print ary
307
-
308
- p "fooooooooo"
309
- p "baaaaaaaar"
310
- p "AAAAA", "BBBBB", "CCCCC"
311
- p ary
312
-
313
- puts <<EOM
314
- Lorem ipsum dolor sit amet,
315
- consectetur adipisicing elit,
316
- sed do eiusmod tempor incididunt
317
- ut labore et dolore magna aliqua.
318
- EOM
319
-
320
- CE.tx(:underscore).bg(:black)
321
- CE.rainbow
322
- hash = {:foo => [111, 222, 333], :bar => {:str => String}}
323
- p hash
324
- p "fooooooo", ["AAA", "BBB", "CCC"]
325
-
326
- CE.off
327
- puts "Disable rainbow mode."
328
- ```
329
-
330
- #### Result
331
-
332
- ![screen shot](/images/screenshot.png)
333
-
334
- ## Release Note
335
- * v2.0.3, 2016-01-06
336
- * rebuild by old gem, because latest gem can't use symbolic link in bin/.
337
-
338
- * v2.0.2, 2016-01-06
339
- * Fixed problem as below; https://github.com/khotta/color_echo/issues/1
340
- * This version has problem of fail to execute colorecho command, Please use v2.0.3 and over.
341
-
342
- * v2.0.1, 2015-08-21
343
- * Fixed bug that highlight of line is incorrect in commandline interface.
344
-
345
- * v2.0.0, 2015-05-20
346
- * Add new method -> CE::hitline, CE.enable_refresh, CE.disable_refresh, Please check the reference.
347
- * Can to select new parameter ':hitline' in CE.reset.
348
- * Flushes any buffered data when output data to STDOUT.
349
- * Fixed bug, When the input was included invalid encoding.
350
- * Fixed not to be output the interruptted message, When you pressed ctl + C.
351
- * Fixed default foreground color to yellow in command line interface.
352
-
353
- * v1.3.0, 2015-02-06
354
- * Change some options help messages.
355
- * You can call 'colorecho' as 'color_echo' in command line interface.
356
-
357
- * v1.2.0, 2015-01-28
358
- * Add -e option.
359
-
360
- * v1.1.0, 2015-01-27
361
- * Modified to output the argument when the standard input is hit.
362
- * Add --stripe option in the command line interface.
363
- * Change the delimiter of long option with a hyphen -> -symbol-list,--index-list; Can to specify also underscore as before.
364
-
365
- * v1.0.0, 2015-01-23
366
- * Add command line interface.
367
-
368
- * v0.9.0, 2015-01-19
369
- * Add a mode to receive as the words with ANSI escape sequence; without output to display.
370
-
371
- * v0.8.0, 2015-01-14
372
- * Change for the specified arguments of reset method.
373
- * Fix small bugs.
374
-
375
- * v0.7.0, 2015-01-08
376
- * Add new method -> pickup
377
- * Add new symbol that can to specify in reset method of first parameter -> CE.reset(:pickup)
378
-
379
- * v0.6.0, 2015-01-05
380
- * Add command line tool.
381
-
382
- * v0.5.0, 2014-12-16
383
- * Add a new method -> \#once, \#times
384
-
385
- * v0.4.0, 2014-12-11
386
- * Add 256 colors.
387
-
388
- * v0.3.0, 2014-12-08
389
- * Add high colors.
390
- * Can to select multi value in ch_tx method.
391
- * Add parameter to the #reset method.
392
-
393
- * v0.2.4, 2014-12-04
394
- * Can to specify a non-string when rainbow mode.
395
- * Cab take over the setting of other types of sequence when rainbow mode.
396
-
397
- * v0.2.3, 2014-12-02
398
- * Fix small bugs.
399
-
400
- * v0.2.0
401
- * Added new method -> rainbow
402
- * Added some method alias.
403
-
404
- * v0.1.0
405
- * Added new method -> ch, unuse
406
- * Added some method alias.
7
+ ### You can choose 256 colors!!
8
+ ![screen shot](/image/colorindex01.png)
9
+ ![screen shot](/image/colorindex02.png)
@@ -1,29 +1,30 @@
1
1
  #!/usr/bin/env ruby
2
+ # coding: utf-8
2
3
 
4
+ # Load file
3
5
  require_relative "./lib/display"
4
6
 
5
- #
7
+ # const
8
+ DEFAULT_FG_PICKUP = :cyan
9
+ DEFAULT_FG_STRIPE = :cyan
10
+ CONF_DEFAULT_NAME = "default"
11
+
6
12
  # Load color_echo
7
13
  # @return void
8
- #
9
14
  def require_color_echo
10
15
  require "color_echo"
11
16
  #require_relative "../lib/color_echo"
12
17
  end
13
18
 
14
- #
15
19
  # Load color_echo/get
16
20
  # @return void
17
- #
18
21
  def require_color_echo_get
19
22
  require "color_echo/get"
20
23
  #require_relative "../lib/color_echo/get"
21
24
  end
22
25
 
23
- #
24
26
  # output in interactive
25
27
  # @return void
26
- #
27
28
  def output_interactive
28
29
  output_stripe_interactive if @is_stripe
29
30
 
@@ -39,10 +40,8 @@ def output_interactive
39
40
  end
40
41
  end
41
42
 
42
- #
43
43
  # output like stripe in interactive
44
44
  # @return void
45
- #
46
45
  def output_stripe_interactive
47
46
  cnt = 0
48
47
  begin
@@ -58,23 +57,26 @@ def output_stripe_interactive
58
57
  end
59
58
  end
60
59
 
61
- #
62
60
  # output
63
61
  # @return void
64
- #
65
62
  def output(target)
66
63
  output_stripe(target) if @is_stripe
67
64
 
68
- # performance down when it read line by line..?
69
- targets = target.split($/)
65
+ #targets = target.split($/) #=> This way dosen't work as expected the issue of String#split
66
+ targets = target.split($/, target.size)
70
67
  total = targets.count
71
68
  cnt = 0
72
69
  targets.each do |record|
73
70
  cnt += 1
74
- eval(@code_decorate)
71
+ begin
72
+ eval(@code_decorate)
73
+ rescue SyntaxError
74
+ msg = %(Invalid value: #{@code_decorate})
75
+ error_display(msg, "Syntax")
76
+ end
75
77
 
76
78
  begin
77
- if !@last_lb && cnt == total
79
+ if cnt == total && !@last_lb
78
80
  print CE.get(record)
79
81
  else
80
82
  puts CE.get(record)
@@ -86,13 +88,13 @@ def output(target)
86
88
  exit 0
87
89
  end
88
90
 
89
- #
90
91
  # output like stripe
91
92
  # @return void
92
- #
93
93
  def output_stripe(target)
94
- cnt = 0
95
- target.split($/).each do |record|
94
+ #targets = target.split($/) #=> This way dosen't work as expected the issue of String#split
95
+ targets = target.split($/, target.size)
96
+ cnt = 0
97
+ targets.each do |record|
96
98
  eval(@code_decorate) if cnt % 2 == 0
97
99
  puts CE.get(record)
98
100
  cnt += 1
@@ -100,105 +102,328 @@ def output_stripe(target)
100
102
  exit 0
101
103
  end
102
104
 
103
- #
105
+ # Return Symbol object if val was not nil
106
+ # @param string|nil input
107
+ # @return symbol|nil
108
+ def get_symbol(input)
109
+ return nil if input == nil
110
+ return input.intern
111
+ end
112
+
113
+ # Return String like Symbol following ":string"
114
+ # @param string|nil input
115
+ # @return string
116
+ def get_symbol_string(input)
117
+ return "nil" if input == nil
118
+ return ":#{input}"
119
+ end
120
+
121
+ # Return Symbol object list if val was not nil
122
+ # @param string|nil input
123
+ # @return array|nil
124
+ def get_symbol_list(input)
125
+ return nil if input == nil
126
+
127
+ list = []
128
+ input.split(",").each do |val|
129
+ list << val.intern
130
+ end
131
+ return list
132
+ end
133
+
134
+ # Return String like Symbol that likes ":string"
135
+ # @param array|string|nil input
136
+ # @return string
137
+ def get_symbol_string_list(input)
138
+ return "nil" if input == nil
139
+
140
+ input = input.split(",") if !input.is_a?(Array)
141
+
142
+ list = []
143
+ input.each do |val|
144
+ list << ":#{val}"
145
+ end
146
+ list = list.join(",")
147
+
148
+ return "[#{list}]"
149
+ end
150
+
151
+ # @param string|nil fg
152
+ # @param string|nil bg
153
+ # @param string|nil tx
154
+ # @return [symbol|nil ,symbol|nil ,symbol|nil]
155
+ def convert_symbol(fg, bg, tx)
156
+ return get_symbol(fg), get_symbol(bg), get_symbol_list(tx)
157
+ end
158
+
159
+ # @param string|nil fg
160
+ # @param string|nil bg
161
+ # @param array|string tx
162
+ # @return [string, string, string]
163
+ def convert_symbol_string(fg, bg, tx)
164
+ return get_symbol_string(fg), get_symbol_string(bg), get_symbol_string_list(tx)
165
+ end
166
+
167
+ # Parse and update decoration code with pickup & highlight
168
+ # @return void
169
+ def parse_pickups
170
+ code_addition = ""
171
+ @pickups.each_index do |index|
172
+ p_list = @pickups[index].split(/(?<!\\),/)
173
+
174
+ # No input pattern
175
+ if p_list.size == 0
176
+ error_display(%(You have to specify pattern when using -p option!), "Argument")
177
+ end
178
+
179
+ pattern = p_list[0]
180
+ fg = nil
181
+ bg = nil
182
+ tx = nil
183
+ fg = p_list[1] if defined?(p_list[1])
184
+ bg = p_list[2] if defined?(p_list[2])
185
+ tx = p_list[3...p_list.size] if defined?(p_list[3])
186
+ fg = DEFAULT_FG_PICKUP if fg == nil && bg == nil && tx == nil
187
+ fg, bg, tx = convert_symbol_string(fg, bg, tx)
188
+
189
+ if pattern[0] == "/" && (pattern[-1] == "/" || pattern[-2] == "/")
190
+ pattern_str = pattern
191
+ else
192
+ pattern_str = %("#{pattern}")
193
+ end
194
+
195
+ code_addition += %(.pickup(#{pattern_str}, #{fg}, #{bg}, #{tx}))
196
+ end
197
+
198
+ # highlights
199
+ if @highlights.size > 0
200
+ @highlights.each do |highlight|
201
+ params = []
202
+ highlight.split(",").each do |el|
203
+ el.strip!
204
+ if el != "nil"
205
+ params << ":#{el}"
206
+ else
207
+ params << "nil"
208
+ end
209
+ end
210
+ code_addition += %(.highlight(#{params.join(",")}))
211
+ end
212
+ end
213
+
214
+ @code_decorate = %(CE.ch(@fg, @bg, @tx)) + code_addition
215
+ end
216
+
217
+ # Read options files
218
+ # @param data string
219
+ def read_conf(conf_path, list)
220
+ if list.size > 2
221
+ error_display(%(-c option is allowed to use only one time!), "Argument")
222
+ end
223
+
224
+ additions = []
225
+ no_val_allows = ["-w", "--watch", "-n", "-e", "--stripe", "-r", "--refresh-match", "-R", "--refresh"]
226
+ no_val_allows.concat(["w", "watch", "n", "e", "stripe", "r", "refresh-match", "R", "refresh"])
227
+ list += list.pop.split(",")
228
+ list.each do |file_name|
229
+ full_path = conf_path + File::SEPARATOR + file_name
230
+
231
+ if FileTest.exist?(full_path) && !FileTest.directory?(full_path)
232
+ begin
233
+ f = File.open(full_path, File::RDONLY)
234
+ data = f.readlines
235
+ f.close
236
+ rescue => e
237
+ error_display("#{e.class}: #{e.message}", "File Open")
238
+ end
239
+
240
+ data.each do |option|
241
+ # This is comment
242
+ option.strip!
243
+ next if /^#/ =~ option
244
+ next if option == ""
245
+
246
+ op = option.split("=")
247
+ if op.size != 2 && no_val_allows.index(op[0]) == nil
248
+ error_display(%(Wrong syntax '#{op.join("=")}' Check your options file: #{full_path}), "Configuration")
249
+ end
250
+
251
+ opname = ((/^-/ =~ op[0]) == nil) ? (op[0].size > 1 ? "--" : "-") + op[0] : op[0]
252
+ additions << opname
253
+ additions << op[1] if defined? op[1]
254
+ end
255
+ else
256
+ # Missing specified file
257
+ if (file_name != CONF_DEFAULT_NAME)
258
+ if FileTest.directory?(full_path)
259
+ msg = %(This is directory: #{full_path})
260
+ else
261
+ msg = %(Missing your specified options file: #{full_path})
262
+ end
263
+ error_display(msg, "Configuration")
264
+ end
265
+ end
266
+ end
267
+
268
+ # Parse options from options files
269
+ # Always prevail the options be specfied at command line
270
+ fg = nil
271
+ bg = nil
272
+ tx = nil
273
+ pickups = []
274
+ highlights = []
275
+ is_watch = false
276
+ last_lb = true
277
+ enable_lf = false
278
+ is_stripe = false
279
+ refresh_pre_match = false
280
+ refresh = false
281
+
282
+ begin
283
+ OptionParser.new do |opt|
284
+ opt.on("-f val", "--fg val", ) { |val| fg = val }
285
+ opt.on("-b val", "--bg val", ) { |val| bg = val }
286
+ opt.on("-t val", "--tx val", ) { |val| tx = val }
287
+ opt.on("-p val", "--pickup format") { |val| pickups << val }
288
+ opt.on("-H", "--highlight format" ) { |val| highlights << val }
289
+ opt.on("-w", "--watch", ) { is_watch = true }
290
+ opt.on("-n", ) { last_lb = false }
291
+ opt.on("-e", ) { enable_lf = true }
292
+ opt.on("--stripe", ) { is_stripe = true }
293
+ opt.on("-r", "--refresh-match" ) { refresh_pre_match = true }
294
+ opt.on("-R", "--refresh" ) { refresh = true }
295
+ opt.parse!(additions)
296
+ end
297
+ @fg ||= fg
298
+ @bg ||= bg
299
+ @tx ||= tx
300
+ @pickups += pickups
301
+ @highlights += highlights
302
+ @is_watch = true if !@is_watch && is_watch
303
+ @last_lb = false if @last_lb && !last_lb
304
+ @enable_lf = true if !@enable_lf && enable_lf
305
+ @is_stripe = true if !@is_stripe && is_stripe
306
+ @refresh_pre_match = true if !@refresh_pre_match && refresh_pre_match
307
+ @refresh = true if !@refresh && refresh
308
+
309
+ rescue => err
310
+ msg1 = err.message
311
+ msg2 = %(You can't specify this option here, Please check your options files.)
312
+ error_display([msg1, msg2], "Configuration")
313
+ end
314
+ end
315
+
316
+ # Parse options
317
+ # @return void
318
+ def parse_conf
319
+ # find options
320
+ if ENV["COLORECHO"] != nil
321
+ conf_path = ENV["COLORECHO"]
322
+ elsif ENV["HOME"] == nil
323
+ conf_path = ""
324
+ else
325
+ conf_path = ENV["HOME"]
326
+ conf_path += File::SEPARATOR if (/#{File::SEPARATOR}$/ =~ @conf_path) == nil
327
+ conf_path += ".colorecho"
328
+ end
329
+
330
+ if FileTest.directory?(conf_path)
331
+ # found options directory
332
+ @conf_list.unshift(CONF_DEFAULT_NAME)
333
+ options_conf = read_conf(conf_path, @conf_list)
334
+
335
+ elsif FileTest.exist?(conf_path)
336
+ # color_echo options path is not directory
337
+ if @conf_list.size > 0
338
+ msg1 = %('#{conf_path}' is not directory!)
339
+ msg2 = %(Couldn't find your options.)
340
+ error_display([msg1, msg2], "Configuration")
341
+ end
342
+ else
343
+ # no color_echo options directory
344
+ if @conf_list.size > 0
345
+ msg1 = %(Could't find a options directory of color_echo: #{conf_path})
346
+ msg2 = %(You can create options files in $HOME/.colorecho/ or specified directory by env variable $COLORECHO.)
347
+ error_display([msg1, msg2], "Configuration")
348
+ end
349
+ end
350
+ end
351
+
104
352
  # parse the options
105
353
  # @return void
106
- #
107
354
  def parse_options
108
- @fg = "yellow"
355
+ @fg = nil
109
356
  @bg = nil
110
357
  @tx = nil
111
- @highlight = nil
112
358
  @pickups = []
359
+ @highlights = []
113
360
  @is_watch = false
114
361
  @last_lb = true
115
362
  @enable_lf = false
116
363
  @is_stripe = false
117
364
  @refresh_pre_match = false
118
365
  @refresh = false
366
+ @conf_list = []
119
367
 
120
368
  begin
121
369
  @opt.parse!(ARGV)
122
370
 
123
371
  rescue => err
124
- warn %(Parse Error! Please check usage!) + $/ + $/
125
- puts @opt.help
126
- warn $/ + %(Do you want to read example to use?)
127
- warn %( -> Please execute: 'colorecho -h')
372
+ msg = %(Please take a look at usage.) + $/ * 2
373
+ msg += @opt.help
374
+ error_display(msg, "Parse")
128
375
  end
129
376
 
130
- @fg = @fg.intern if @fg != nil
131
- @bg = @bg.intern if @bg != nil
377
+ # parse options
378
+ parse_conf
132
379
 
133
- if @tx != nil
134
- res = []
135
- @tx.split(",").each do |val|
136
- res << val.intern
137
- end
138
- @tx = res
139
- end
380
+ # cast String to Symbol
381
+ @fg, @bg, @tx = convert_symbol(@fg, @bg, @tx)
382
+
383
+ # set default stripe color if foreground was nil
384
+ @fg = DEFAULT_FG_STRIPE if @is_stripe && @fg == nil && @bg == nil && @tx == nil
140
385
 
141
386
  # ignore --pickup option when specified --stripe option
142
387
  if @pickups.size > 0 && @is_stripe
143
- warn "pickup option is ignored because your specified --stripe option."
388
+ warn_display(%(--pickup option was ignored because you are specified --stripe option.), "Argument")
144
389
  @pickups = []
145
390
  end
146
391
 
392
+ # code for decoration
393
+ @code_decorate = %(CE.ch(@fg, @bg, @tx))
394
+
147
395
  # parse pickups
148
396
  if @pickups.size > 0
149
- code_pickup = ""
150
- @pickups.each_index do |index|
151
- if @pickups[index][0] == "/" && (@pickups[index][-1] == "/" || @pickups[index][-2] == "/")
152
- @pickups[index] = eval(@pickups[index])
153
- end
154
-
155
- code_pickup += %(.pickup(@pickups[#{index}], @fg, @bg, @tx))
156
- end
157
- @code_decorate = "CE" + code_pickup
158
-
159
- # highlight
160
- if @highlight != nil
161
- params = []
162
- @highlight.split(",").each do |el|
163
- el.strip!
164
- if el != "nil"
165
- params << ":#{el}"
166
- else
167
- params << "nil"
168
- end
169
- end
170
- code_highlight = %(CE.hitline(#{params.join(",")}))
171
- end
172
- @code_decorate = code_highlight + ";" + @code_decorate if code_highlight != nil
173
- else
174
- @code_decorate = %(CE.ch(@fg, @bg, @tx))
397
+ parse_pickups
175
398
  end
176
399
  end
177
400
 
178
401
  # -----------
179
402
  # top level
180
403
  # -----------
181
- require "optparse"
404
+ begin
405
+ require "optparse"
406
+ rescue LoadError
407
+ error_display(%('optparse' is required for color_echo.), "Load")
408
+ end
182
409
  @opt = OptionParser.new
183
410
 
184
- @opt.on("-s", "--symbol-list", @explain_s) { display_symbol_list }
185
- @opt.on("-l", "--index-list", @explain_l) { display_color_index }
411
+ @opt.on("-s", "--symbol-list", @explain_s) { display_symbol_list }
412
+ @opt.on("-l", "--index-list", @explain_l) { display_color_index }
186
413
  @opt.on("-f val", "--fg val", @explain_f) { |val| @fg = val }
187
414
  @opt.on("-b val", "--bg val", @explain_b) { |val| @bg = val }
188
415
  @opt.on("-t val", "--tx val", @explain_t) { |val| @tx = val }
189
- @opt.on("-p val", "--pickup val", @explain_p) { |val| @pickups << val }
190
- @opt.on("-H", "--highlight val", @explain_H) { |val| @highlight = val}
191
- @opt.on("-w", "--watch", @explain_w) { @is_watch = true }
416
+ @opt.on("-p val", "--pickup format", @explain_p) { |val| @pickups << val }
417
+ @opt.on("-H", "--highlight format", @explain_H) { |val| @highlights << val }
418
+ @opt.on("-w", "--watch", @explain_w) { @is_watch = true }
192
419
  @opt.on("-n", @explain_n) { @last_lb = false }
193
420
  @opt.on("-e", @explain_e) { @enable_lf = true }
194
421
  @opt.on("--stripe", @explain_stripe) { @is_stripe = true }
195
- @opt.on("-r", "--refresh-pre-match", @explain_r) { @refresh_pre_match = true }
422
+ @opt.on("-r", "--refresh-match", @explain_r) { @refresh_pre_match = true }
196
423
  @opt.on("-R", "--refresh", @explain_R) { @refresh = true }
424
+ @opt.on("-c conf_name", @explain_c) { |val| @conf_list << val }
197
425
  @opt.on("-v", @explain_v) { version }
198
- @opt.on("-h", "--help", @explain_h) { usage }
199
-
200
- @opt.on("--symbol_list", @explain_s + " - [compatibility]" ) { display_symbol_list }
201
- @opt.on("--index_list", @explain_l + " - [compatibility]") { display_color_index }
426
+ @opt.on("-h", "--help", @explain_h) { usage }
202
427
 
203
428
  @opt.banner = <<EOS
204
429
  Usage:
@@ -209,15 +434,16 @@ EOS
209
434
  # has stdin?
210
435
  has_stdin = select([STDIN], [], [], 0) != nil || FileTest.pipe?(STDIN)
211
436
 
212
- # argv error
437
+ # no argument
213
438
  if !has_stdin && ARGV.size == 0
214
439
  puts @opt.help
440
+ exit 0
215
441
  end
216
442
 
443
+ # load color_echo liblary
217
444
  require_color_echo_get
218
445
  Version = CE::VERSION
219
446
 
220
- # parse options and set the decorate code that execute by eval
221
447
  parse_options
222
448
 
223
449
  # need refresh?
@@ -236,7 +462,7 @@ else
236
462
  begin
237
463
  target = STDIN.read
238
464
  rescue Interrupt
239
- puts $/ + %(No STDIN, Try to use -w option if you need Interactive mode.)
465
+ error_display(%(No STDIN, Try to use -w option if you need Interactive mode.))
240
466
  end
241
467
 
242
468
  if ARGV.size > 0
@@ -247,7 +473,6 @@ else
247
473
  end
248
474
  else
249
475
  target = ARGV.join(" ")
250
- target += $/ if @last_lb
251
476
  end
252
477
 
253
478
  if @enable_lf