color_echo 1.3.0 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,39 +1,78 @@
1
1
  #!/usr/bin/env ruby
2
+ require_relative "./lib/display"
3
+ def dump(target)
4
+ require "pp"
5
+ require "stringio"
6
+
7
+ File.open("/tmp/ruby.log", "a+") do |f|
8
+ $stdout = StringIO.new
9
+ pp target
10
+ strio = $stdout
11
+ $stdout = STDOUT
12
+ f.print strio.string
13
+ end
14
+ end
2
15
 
16
+ #
17
+ # Load color_echo
18
+ # @return void
19
+ #
3
20
  def require_color_echo
4
21
  require "color_echo"
5
22
  #require_relative "../lib/color_echo"
6
23
  end
7
24
 
25
+ #
26
+ # Load color_echo/get
27
+ # @return void
28
+ #
8
29
  def require_color_echo_get
9
30
  require "color_echo/get"
10
31
  #require_relative "../lib/color_echo/get"
11
32
  end
12
33
 
13
- require_relative "./lib/display"
14
-
34
+ #
35
+ # output in interactive
36
+ # @return void
37
+ #
15
38
  def output_interactive
16
39
  output_stripe_interactive if @is_stripe
17
40
 
18
- while input = STDIN.gets
19
- input.chomp!
20
- exit 0 if @exit_words.index(input)
21
- eval(@code_decorate)
22
- puts CE.get(input)
41
+ begin
42
+ while input = STDIN.gets
43
+ input.chomp!
44
+ exit 0 if @exit_words.index(input)
45
+ eval(@code_decorate)
46
+ $stdout.flush.puts CE.get(input)
47
+ end
48
+ rescue Interrupt
49
+ exit 1
23
50
  end
24
51
  end
25
52
 
53
+ #
54
+ # output like stripe in interactive
55
+ # @return void
56
+ #
26
57
  def output_stripe_interactive
27
58
  cnt = 0
28
- while input = STDIN.gets
29
- input.chomp!
30
- exit 0 if @exit_words.index(input)
31
- eval(@code_decorate) if cnt % 2 == 0
32
- puts CE.get(input)
33
- cnt += 1
59
+ begin
60
+ while input = STDIN.gets
61
+ input.chomp!
62
+ exit 0 if @exit_words.index(input)
63
+ eval(@code_decorate) if cnt % 2 == 0
64
+ puts CE.get(input)
65
+ cnt += 1
66
+ end
67
+ rescue Interrupt
68
+ exit 1
34
69
  end
35
70
  end
36
71
 
72
+ #
73
+ # output
74
+ # @return void
75
+ #
37
76
  def output(target)
38
77
  output_stripe(target) if @is_stripe
39
78
 
@@ -45,6 +84,10 @@ def output(target)
45
84
  exit 0
46
85
  end
47
86
 
87
+ #
88
+ # output like stripe
89
+ # @return void
90
+ #
48
91
  def output_stripe(target)
49
92
  cnt = 0
50
93
  target.split($/).each do |record|
@@ -55,44 +98,33 @@ def output_stripe(target)
55
98
  exit 0
56
99
  end
57
100
 
101
+ #
102
+ # parse the options
103
+ # @return void
104
+ #
58
105
  def parse_options
59
- @fg = nil
60
- @bg = nil
61
- @tx = nil
62
- @pickups = []
63
- @is_watch = false
64
- @last_lb = true
65
- @enable_lf = false
66
- @is_stripe = false
67
-
68
- require "optparse"
69
- opt = OptionParser.new
70
-
71
- opt.on("-h", @explain_h) { usage }
72
- opt.on("-v", @explain_v) { version }
73
- opt.on("-s", "--symbol-list", @explain_s) { display_symbol_list }
74
- opt.on("-l", "--index-list", @explain_l) { display_color_index }
75
- opt.on("--fg val", "-f val", @explain_f) { |val| @fg = val }
76
- opt.on("--bg val", "-b val", @explain_b) { |val| @bg = val }
77
- opt.on("--tx val", "-t val", @explain_t) { |val| @tx = val }
78
- opt.on("--pickup val", "-p val", @explain_p) { |val| @pickups << val }
79
- opt.on("-w", "--watch", @explain_w) { @is_watch = true }
80
- opt.on("-n", @explain_n) { @last_lb = false }
81
- opt.on("-e", @explain_e) { @enable_lf = true }
82
- opt.on("--stripe", @explain_stripe) { @is_stripe = true }
83
-
84
- opt.on("--symbol_list", @explain_s + " - [compatibility]" ) { display_symbol_list }
85
- opt.on("--index_list", @explain_l + " - [compatibility]") { display_color_index }
106
+ @fg = "yellow"
107
+ @bg = nil
108
+ @tx = nil
109
+ @highlight = nil
110
+ @pickups = []
111
+ @is_watch = false
112
+ @last_lb = true
113
+ @enable_lf = false
114
+ @is_stripe = false
115
+ @refresh_pre_match = false
116
+ @refresh = false
86
117
 
87
118
  begin
88
- opt.parse!(ARGV)
119
+ @opt.parse!(ARGV)
89
120
 
90
121
  rescue => err
91
- warn "Parse Error! Please check usage!" + $/ + $/
92
- usage
122
+ warn %(Parse Error! Please check usage!) + $/ + $/
123
+ puts @opt.help
124
+ warn $/ + %(Do you want to read example to use?)
125
+ warn %( -> Please execute: 'colorecho -h')
93
126
  end
94
127
 
95
- @fg = "yellow" if @fg == nil && @bg == nil && @tx == nil
96
128
  @fg = @fg.intern if @fg != nil
97
129
  @bg = @bg.intern if @bg != nil
98
130
 
@@ -121,13 +153,64 @@ def parse_options
121
153
  code_pickup += %(.pickup(@pickups[#{index}], @fg, @bg, @tx))
122
154
  end
123
155
  @code_decorate = "CE" + code_pickup
156
+
157
+ # highlight
158
+ if @highlight != nil
159
+ params = []
160
+ @highlight.split(",").each do |el|
161
+ el.strip!
162
+ if el != "nil"
163
+ params << ":#{el}"
164
+ else
165
+ params << "nil"
166
+ end
167
+ end
168
+ code_highlight = %(CE.hitline(#{params.join(",")}))
169
+ end
170
+ @code_decorate = code_highlight + ";" + @code_decorate if code_highlight != nil
124
171
  else
125
172
  @code_decorate = %(CE.ch(@fg, @bg, @tx))
126
173
  end
127
174
  end
128
175
 
176
+ # -----------
177
+ # top level
178
+ # -----------
179
+ require "optparse"
180
+ @opt = OptionParser.new
181
+
182
+ @opt.on("-s", "--symbol-list", @explain_s) { display_symbol_list }
183
+ @opt.on("-l", "--index-list", @explain_l) { display_color_index }
184
+ @opt.on("-f val", "--fg val", @explain_f) { |val| @fg = val }
185
+ @opt.on("-b val", "--bg val", @explain_b) { |val| @bg = val }
186
+ @opt.on("-t val", "--tx val", @explain_t) { |val| @tx = val }
187
+ @opt.on("-p val", "--pickup val", @explain_p) { |val| @pickups << val }
188
+ @opt.on("-H", "--highlight val", @explain_H) { |val| @highlight = val}
189
+ @opt.on("-w", "--watch", @explain_w) { @is_watch = true }
190
+ @opt.on("-n", @explain_n) { @last_lb = false }
191
+ @opt.on("-e", @explain_e) { @enable_lf = true }
192
+ @opt.on("--stripe", @explain_stripe) { @is_stripe = true }
193
+ @opt.on("-r", "--refresh-pre-match", @explain_r) { @refresh_pre_match = true }
194
+ @opt.on("-R", "--refresh", @explain_R) { @refresh = true }
195
+ @opt.on("-v", @explain_v) { version }
196
+ @opt.on("-h", "--help", @explain_h) { usage }
197
+
198
+ @opt.on("--symbol_list", @explain_s + " - [compatibility]" ) { display_symbol_list }
199
+ @opt.on("--index_list", @explain_l + " - [compatibility]") { display_color_index }
200
+
201
+ @opt.banner = <<EOS
202
+ Usage:
203
+ #{@explain_usage}
204
+ Options:
205
+ EOS
206
+
207
+ # has stdin?
129
208
  has_stdin = select([STDIN], [], [], 0) != nil || FileTest.pipe?(STDIN)
130
- usage if !has_stdin && ARGV.size == 0
209
+
210
+ # argv error
211
+ if !has_stdin && ARGV.size == 0
212
+ puts @opt.help
213
+ end
131
214
 
132
215
  require_color_echo_get
133
216
  Version = CE::VERSION
@@ -135,22 +218,33 @@ Version = CE::VERSION
135
218
  # parse options and set the decorate code that execute by eval
136
219
  parse_options
137
220
 
221
+ # need refresh?
222
+ if @refresh
223
+ CE.enable_refresh
224
+ elsif @refresh_pre_match
225
+ CE.enable_refresh(:prematch)
226
+ end
227
+
138
228
  if @is_watch
139
229
  # Interactive mode
140
230
  @exit_words = ["quit", "exit", "bye"]
141
231
  output_interactive
142
232
  else
143
233
  if has_stdin
144
- target = STDIN.read
234
+ begin
235
+ target = STDIN.read
236
+ rescue Interrupt
237
+ puts $/ + %(No STDIN, Try to use -w option if you need Interactive mode.)
238
+ end
145
239
 
146
240
  if ARGV.size > 0
147
241
  print target
148
- argv = ARGV.join(" ")
242
+ argv = ARGV.join(" ")
149
243
  argv += $/ if @last_lb
150
244
  output(argv)
151
245
  end
152
246
  else
153
- target = ARGV.join(" ")
247
+ target = ARGV.join(" ")
154
248
  target += $/ if @last_lb
155
249
  end
156
250
 
@@ -1,39 +1,78 @@
1
1
  #!/usr/bin/env ruby
2
+ require_relative "./lib/display"
3
+ def dump(target)
4
+ require "pp"
5
+ require "stringio"
6
+
7
+ File.open("/tmp/ruby.log", "a+") do |f|
8
+ $stdout = StringIO.new
9
+ pp target
10
+ strio = $stdout
11
+ $stdout = STDOUT
12
+ f.print strio.string
13
+ end
14
+ end
2
15
 
16
+ #
17
+ # Load color_echo
18
+ # @return void
19
+ #
3
20
  def require_color_echo
4
21
  require "color_echo"
5
22
  #require_relative "../lib/color_echo"
6
23
  end
7
24
 
25
+ #
26
+ # Load color_echo/get
27
+ # @return void
28
+ #
8
29
  def require_color_echo_get
9
30
  require "color_echo/get"
10
31
  #require_relative "../lib/color_echo/get"
11
32
  end
12
33
 
13
- require_relative "./lib/display"
14
-
34
+ #
35
+ # output in interactive
36
+ # @return void
37
+ #
15
38
  def output_interactive
16
39
  output_stripe_interactive if @is_stripe
17
40
 
18
- while input = STDIN.gets
19
- input.chomp!
20
- exit 0 if @exit_words.index(input)
21
- eval(@code_decorate)
22
- puts CE.get(input)
41
+ begin
42
+ while input = STDIN.gets
43
+ input.chomp!
44
+ exit 0 if @exit_words.index(input)
45
+ eval(@code_decorate)
46
+ $stdout.flush.puts CE.get(input)
47
+ end
48
+ rescue Interrupt
49
+ exit 1
23
50
  end
24
51
  end
25
52
 
53
+ #
54
+ # output like stripe in interactive
55
+ # @return void
56
+ #
26
57
  def output_stripe_interactive
27
58
  cnt = 0
28
- while input = STDIN.gets
29
- input.chomp!
30
- exit 0 if @exit_words.index(input)
31
- eval(@code_decorate) if cnt % 2 == 0
32
- puts CE.get(input)
33
- cnt += 1
59
+ begin
60
+ while input = STDIN.gets
61
+ input.chomp!
62
+ exit 0 if @exit_words.index(input)
63
+ eval(@code_decorate) if cnt % 2 == 0
64
+ puts CE.get(input)
65
+ cnt += 1
66
+ end
67
+ rescue Interrupt
68
+ exit 1
34
69
  end
35
70
  end
36
71
 
72
+ #
73
+ # output
74
+ # @return void
75
+ #
37
76
  def output(target)
38
77
  output_stripe(target) if @is_stripe
39
78
 
@@ -45,6 +84,10 @@ def output(target)
45
84
  exit 0
46
85
  end
47
86
 
87
+ #
88
+ # output like stripe
89
+ # @return void
90
+ #
48
91
  def output_stripe(target)
49
92
  cnt = 0
50
93
  target.split($/).each do |record|
@@ -55,44 +98,33 @@ def output_stripe(target)
55
98
  exit 0
56
99
  end
57
100
 
101
+ #
102
+ # parse the options
103
+ # @return void
104
+ #
58
105
  def parse_options
59
- @fg = nil
60
- @bg = nil
61
- @tx = nil
62
- @pickups = []
63
- @is_watch = false
64
- @last_lb = true
65
- @enable_lf = false
66
- @is_stripe = false
67
-
68
- require "optparse"
69
- opt = OptionParser.new
70
-
71
- opt.on("-h", @explain_h) { usage }
72
- opt.on("-v", @explain_v) { version }
73
- opt.on("-s", "--symbol-list", @explain_s) { display_symbol_list }
74
- opt.on("-l", "--index-list", @explain_l) { display_color_index }
75
- opt.on("--fg val", "-f val", @explain_f) { |val| @fg = val }
76
- opt.on("--bg val", "-b val", @explain_b) { |val| @bg = val }
77
- opt.on("--tx val", "-t val", @explain_t) { |val| @tx = val }
78
- opt.on("--pickup val", "-p val", @explain_p) { |val| @pickups << val }
79
- opt.on("-w", "--watch", @explain_w) { @is_watch = true }
80
- opt.on("-n", @explain_n) { @last_lb = false }
81
- opt.on("-e", @explain_e) { @enable_lf = true }
82
- opt.on("--stripe", @explain_stripe) { @is_stripe = true }
83
-
84
- opt.on("--symbol_list", @explain_s + " - [compatibility]" ) { display_symbol_list }
85
- opt.on("--index_list", @explain_l + " - [compatibility]") { display_color_index }
106
+ @fg = "yellow"
107
+ @bg = nil
108
+ @tx = nil
109
+ @highlight = nil
110
+ @pickups = []
111
+ @is_watch = false
112
+ @last_lb = true
113
+ @enable_lf = false
114
+ @is_stripe = false
115
+ @refresh_pre_match = false
116
+ @refresh = false
86
117
 
87
118
  begin
88
- opt.parse!(ARGV)
119
+ @opt.parse!(ARGV)
89
120
 
90
121
  rescue => err
91
- warn "Parse Error! Please check usage!" + $/ + $/
92
- usage
122
+ warn %(Parse Error! Please check usage!) + $/ + $/
123
+ puts @opt.help
124
+ warn $/ + %(Do you want to read example to use?)
125
+ warn %( -> Please execute: 'colorecho -h')
93
126
  end
94
127
 
95
- @fg = "yellow" if @fg == nil && @bg == nil && @tx == nil
96
128
  @fg = @fg.intern if @fg != nil
97
129
  @bg = @bg.intern if @bg != nil
98
130
 
@@ -121,13 +153,64 @@ def parse_options
121
153
  code_pickup += %(.pickup(@pickups[#{index}], @fg, @bg, @tx))
122
154
  end
123
155
  @code_decorate = "CE" + code_pickup
156
+
157
+ # highlight
158
+ if @highlight != nil
159
+ params = []
160
+ @highlight.split(",").each do |el|
161
+ el.strip!
162
+ if el != "nil"
163
+ params << ":#{el}"
164
+ else
165
+ params << "nil"
166
+ end
167
+ end
168
+ code_highlight = %(CE.hitline(#{params.join(",")}))
169
+ end
170
+ @code_decorate = code_highlight + ";" + @code_decorate if code_highlight != nil
124
171
  else
125
172
  @code_decorate = %(CE.ch(@fg, @bg, @tx))
126
173
  end
127
174
  end
128
175
 
176
+ # -----------
177
+ # top level
178
+ # -----------
179
+ require "optparse"
180
+ @opt = OptionParser.new
181
+
182
+ @opt.on("-s", "--symbol-list", @explain_s) { display_symbol_list }
183
+ @opt.on("-l", "--index-list", @explain_l) { display_color_index }
184
+ @opt.on("-f val", "--fg val", @explain_f) { |val| @fg = val }
185
+ @opt.on("-b val", "--bg val", @explain_b) { |val| @bg = val }
186
+ @opt.on("-t val", "--tx val", @explain_t) { |val| @tx = val }
187
+ @opt.on("-p val", "--pickup val", @explain_p) { |val| @pickups << val }
188
+ @opt.on("-H", "--highlight val", @explain_H) { |val| @highlight = val}
189
+ @opt.on("-w", "--watch", @explain_w) { @is_watch = true }
190
+ @opt.on("-n", @explain_n) { @last_lb = false }
191
+ @opt.on("-e", @explain_e) { @enable_lf = true }
192
+ @opt.on("--stripe", @explain_stripe) { @is_stripe = true }
193
+ @opt.on("-r", "--refresh-pre-match", @explain_r) { @refresh_pre_match = true }
194
+ @opt.on("-R", "--refresh", @explain_R) { @refresh = true }
195
+ @opt.on("-v", @explain_v) { version }
196
+ @opt.on("-h", "--help", @explain_h) { usage }
197
+
198
+ @opt.on("--symbol_list", @explain_s + " - [compatibility]" ) { display_symbol_list }
199
+ @opt.on("--index_list", @explain_l + " - [compatibility]") { display_color_index }
200
+
201
+ @opt.banner = <<EOS
202
+ Usage:
203
+ #{@explain_usage}
204
+ Options:
205
+ EOS
206
+
207
+ # has stdin?
129
208
  has_stdin = select([STDIN], [], [], 0) != nil || FileTest.pipe?(STDIN)
130
- usage if !has_stdin && ARGV.size == 0
209
+
210
+ # argv error
211
+ if !has_stdin && ARGV.size == 0
212
+ puts @opt.help
213
+ end
131
214
 
132
215
  require_color_echo_get
133
216
  Version = CE::VERSION
@@ -135,22 +218,33 @@ Version = CE::VERSION
135
218
  # parse options and set the decorate code that execute by eval
136
219
  parse_options
137
220
 
221
+ # need refresh?
222
+ if @refresh
223
+ CE.enable_refresh
224
+ elsif @refresh_pre_match
225
+ CE.enable_refresh(:prematch)
226
+ end
227
+
138
228
  if @is_watch
139
229
  # Interactive mode
140
230
  @exit_words = ["quit", "exit", "bye"]
141
231
  output_interactive
142
232
  else
143
233
  if has_stdin
144
- target = STDIN.read
234
+ begin
235
+ target = STDIN.read
236
+ rescue Interrupt
237
+ puts $/ + %(No STDIN, Try to use -w option if you need Interactive mode.)
238
+ end
145
239
 
146
240
  if ARGV.size > 0
147
241
  print target
148
- argv = ARGV.join(" ")
242
+ argv = ARGV.join(" ")
149
243
  argv += $/ if @last_lb
150
244
  output(argv)
151
245
  end
152
246
  else
153
- target = ARGV.join(" ")
247
+ target = ARGV.join(" ")
154
248
  target += $/ if @last_lb
155
249
  end
156
250