color_echo 2.0.4 → 3.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,260 +0,0 @@
1
- #!/usr/bin/env ruby
2
-
3
- require_relative "./lib/display"
4
-
5
- #
6
- # Load color_echo
7
- # @return void
8
- #
9
- def require_color_echo
10
- require "color_echo"
11
- #require_relative "../lib/color_echo"
12
- end
13
-
14
- #
15
- # Load color_echo/get
16
- # @return void
17
- #
18
- def require_color_echo_get
19
- require "color_echo/get"
20
- #require_relative "../lib/color_echo/get"
21
- end
22
-
23
- #
24
- # output in interactive
25
- # @return void
26
- #
27
- def output_interactive
28
- output_stripe_interactive if @is_stripe
29
-
30
- begin
31
- while input = STDIN.gets
32
- input.chomp!
33
- exit 0 if @exit_words.index(input)
34
- eval(@code_decorate)
35
- $stdout.flush.puts CE.get(input)
36
- end
37
- rescue Interrupt
38
- exit 1
39
- end
40
- end
41
-
42
- #
43
- # output like stripe in interactive
44
- # @return void
45
- #
46
- def output_stripe_interactive
47
- cnt = 0
48
- begin
49
- while input = STDIN.gets
50
- input.chomp!
51
- exit 0 if @exit_words.index(input)
52
- eval(@code_decorate) if cnt % 2 == 0
53
- puts CE.get(input)
54
- cnt += 1
55
- end
56
- rescue Interrupt
57
- exit 1
58
- end
59
- end
60
-
61
- #
62
- # output
63
- # @return void
64
- #
65
- def output(target)
66
- output_stripe(target) if @is_stripe
67
-
68
- # performance down when it read line by line..?
69
- targets = target.split($/)
70
- total = targets.count
71
- cnt = 0
72
- targets.each do |record|
73
- cnt += 1
74
- eval(@code_decorate)
75
-
76
- begin
77
- if !@last_lb && cnt == total
78
- print CE.get(record)
79
- else
80
- puts CE.get(record)
81
- end
82
- rescue Errno::EPIPE
83
- end
84
- end
85
-
86
- exit 0
87
- end
88
-
89
- #
90
- # output like stripe
91
- # @return void
92
- #
93
- def output_stripe(target)
94
- cnt = 0
95
- target.split($/).each do |record|
96
- eval(@code_decorate) if cnt % 2 == 0
97
- puts CE.get(record)
98
- cnt += 1
99
- end
100
- exit 0
101
- end
102
-
103
- #
104
- # parse the options
105
- # @return void
106
- #
107
- def parse_options
108
- @fg = "yellow"
109
- @bg = nil
110
- @tx = nil
111
- @highlight = nil
112
- @pickups = []
113
- @is_watch = false
114
- @last_lb = true
115
- @enable_lf = false
116
- @is_stripe = false
117
- @refresh_pre_match = false
118
- @refresh = false
119
-
120
- begin
121
- @opt.parse!(ARGV)
122
-
123
- 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')
128
- end
129
-
130
- @fg = @fg.intern if @fg != nil
131
- @bg = @bg.intern if @bg != nil
132
-
133
- if @tx != nil
134
- res = []
135
- @tx.split(",").each do |val|
136
- res << val.intern
137
- end
138
- @tx = res
139
- end
140
-
141
- # ignore --pickup option when specified --stripe option
142
- if @pickups.size > 0 && @is_stripe
143
- warn "pickup option is ignored because your specified --stripe option."
144
- @pickups = []
145
- end
146
-
147
- # parse pickups
148
- 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))
175
- end
176
- end
177
-
178
- # -----------
179
- # top level
180
- # -----------
181
- require "optparse"
182
- @opt = OptionParser.new
183
-
184
- @opt.on("-s", "--symbol-list", @explain_s) { display_symbol_list }
185
- @opt.on("-l", "--index-list", @explain_l) { display_color_index }
186
- @opt.on("-f val", "--fg val", @explain_f) { |val| @fg = val }
187
- @opt.on("-b val", "--bg val", @explain_b) { |val| @bg = val }
188
- @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 }
192
- @opt.on("-n", @explain_n) { @last_lb = false }
193
- @opt.on("-e", @explain_e) { @enable_lf = true }
194
- @opt.on("--stripe", @explain_stripe) { @is_stripe = true }
195
- @opt.on("-r", "--refresh-pre-match", @explain_r) { @refresh_pre_match = true }
196
- @opt.on("-R", "--refresh", @explain_R) { @refresh = true }
197
- @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 }
202
-
203
- @opt.banner = <<EOS
204
- Usage:
205
- #{@explain_usage}
206
- Options:
207
- EOS
208
-
209
- # has stdin?
210
- has_stdin = select([STDIN], [], [], 0) != nil || FileTest.pipe?(STDIN)
211
-
212
- # argv error
213
- if !has_stdin && ARGV.size == 0
214
- puts @opt.help
215
- end
216
-
217
- require_color_echo_get
218
- Version = CE::VERSION
219
-
220
- # parse options and set the decorate code that execute by eval
221
- parse_options
222
-
223
- # need refresh?
224
- if @refresh
225
- CE.enable_refresh
226
- elsif @refresh_pre_match
227
- CE.enable_refresh(:prematch)
228
- end
229
-
230
- if @is_watch
231
- # Interactive mode
232
- @exit_words = ["quit", "exit", "bye"]
233
- output_interactive
234
- else
235
- if has_stdin
236
- begin
237
- target = STDIN.read
238
- rescue Interrupt
239
- puts $/ + %(No STDIN, Try to use -w option if you need Interactive mode.)
240
- end
241
-
242
- if ARGV.size > 0
243
- print target
244
- argv = ARGV.join(" ")
245
- argv += $/ if @last_lb
246
- output(argv)
247
- end
248
- else
249
- target = ARGV.join(" ")
250
- target += $/ if @last_lb
251
- end
252
-
253
- if @enable_lf
254
- target.gsub!("\\r\\n", $/)
255
- target.gsub!("\\n", $/)
256
- target.gsub!("\\r", $/)
257
- end
258
-
259
- output(target)
260
- end