color_echo 0.8.0 → 0.9.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: 51b73486f80605fc2b29e9a87129e3f528222599
4
- data.tar.gz: a1fcdaa02b03ee665075e7768fa05931f5a9216f
3
+ metadata.gz: 71656796cab59b1ebe1d27d680e2ce4c30b7b604
4
+ data.tar.gz: 2406fffc8c2a85628cfc797da963bc8bf373e705
5
5
  SHA512:
6
- metadata.gz: 3640bb11d8f5477ab2cc93b809e9f691e42ff28d9d174a7d2dab546d96cad801aacecb33e18b3faf668d29b9c4f93ca658896d0d5fb223527db30c205585928c
7
- data.tar.gz: b180b3d77e7df8420c8ad493768407b6031161722c8af5efb60d50ea43e4eb9a2fe6600f104701ad98396c0c3d09103f88a4db07a7ccbd469cbc6876db6973b3
6
+ metadata.gz: 9c955163de5d79d247474036f1a53608376e9e9383322bbc5b672ec8dada2618c32ed444f514c186f18fe4caed9f503780dae04cef29b66c342a8b1a0d3fa46c
7
+ data.tar.gz: 4ae8cca7ff900843822a143ab41b9d8dc38321dd948d011514010144afa9bd138366516995ffe5ea129ba5ec0c2003fc601526e0349bc27bc8a8489a560cc687
data/README.md CHANGED
@@ -3,8 +3,8 @@ Decorate the command line output with ANSI escape sequence.
3
3
  String that is output by "print, puts, p" method is decorated.
4
4
  It is also can to decorate only your specified words!
5
5
 
6
- Version: 0.8.0
7
- Compliant Rubys Version: 1.9.3, 2.0.0, 2.1.0 (for Linux)
6
+ Version: 0.9.0
7
+ Compliant Rubys Version: 2.0.0, 2.1.0 (for Linux)
8
8
  License: MIT
9
9
  Gems repository: http://rubygems.org/gems/color_echo
10
10
 
@@ -41,6 +41,26 @@ EOS
41
41
  ![screen shot](/images/pickup1.png)
42
42
 
43
43
 
44
+
45
+ ## Do you want to return words with the escape sequence?
46
+
47
+ Please write `require "color_echo/get"` rather than `require "color_echo"`.
48
+ ```ruby
49
+ require "color_echo/get"
50
+
51
+ greet = CE.fg(:yellow).get("Hello")
52
+ name = CE.ch(:h_blue, :gray).get("Foo")
53
+ myname = CE.tx(:underscore).rainbow.get("Bar")
54
+
55
+ puts greet + ", " + name + "!"
56
+ puts "My name is " + myname
57
+
58
+ output = CE.fg(:blue).pickup(/color$/, :index199).pickup(/^color/, :h_green).get("color color color")
59
+ puts "output is -> " + output
60
+ ```
61
+ ![screen shot](/images/get1.png)
62
+
63
+
44
64
  ## You can run on the command line
45
65
  <pre>
46
66
  * Usage
@@ -134,7 +154,7 @@ ex.) CE.ch_bg :white #=> background color will be changed white
134
154
  #### CE.ch_tx(text_attribute)
135
155
  Change the text attribute to the your specified decoration.
136
156
  - Alias -> tx
137
- - Parameter attribute -> symbol or array of them
157
+ - Parameter text_attribute -> symbol or array of them
138
158
  - Return -> self
139
159
 
140
160
  * symbol list of text attribute:
@@ -180,7 +200,7 @@ Reset automatically after once output.
180
200
 
181
201
  #### CE.times(cnt)
182
202
  Reset automatically after cnt times output.
183
- - Parameter cnt -> Integer
203
+ - Parameter cnt -> integer
184
204
  - Return -> self
185
205
 
186
206
 
@@ -205,7 +225,14 @@ Force ignore the function of this library.
205
225
 
206
226
  #### CE.rainbow
207
227
  Text color will change to rainbow color.
228
+ - Return -> self
229
+
208
230
 
231
+ #### CE.get(text)
232
+ Decorate and return argument with ANSI escape sequence.
233
+ If you want to use this module function; You have to require "color_echo/get" only!
234
+ - Parameter text -> string
235
+ - Return -> string
209
236
 
210
237
 
211
238
  ### Example
@@ -294,6 +321,9 @@ puts "Disable rainbow mode."
294
321
  ![screen shot](/images/screenshot.png)
295
322
 
296
323
  ## Release Note
324
+ * v0.9.0, 2014-01-19
325
+ * Added a mode to receive as the words with ANSI escape sequence; without output to display.
326
+
297
327
  * v0.8.0, 2014-01-14
298
328
  * Changed for the specified arguments of reset method.
299
329
  * Fixed small bugs.
@@ -33,7 +33,7 @@ def usage
33
33
  puts padding + "--index_list, -l"
34
34
  puts padding * 2 + "Display color index list that can to specify." + $/ + $/
35
35
 
36
- exit
36
+ exit 0
37
37
  end
38
38
 
39
39
  def display_color_index
@@ -61,6 +61,8 @@ def display_color_index
61
61
  end
62
62
  end
63
63
  CE.off
64
+
65
+ exit 0
64
66
  end
65
67
 
66
68
  def display_symbol_list
@@ -120,24 +122,30 @@ def display_symbol_list
120
122
  end
121
123
 
122
124
  print $/
123
- end
124
125
 
126
+ exit 0
127
+ end
125
128
 
126
129
  usage if ARGV.size != 1
127
130
 
128
- case ARGV[0]
129
- when "--symbol_list", "-s"
130
- display_symbol_list
131
+ require "optparse"
132
+ opt = OptionParser.new
133
+
134
+ opt.on("-h") { usage }
135
+ opt.on("-v") { puts CE::VERSION; exit 0 }
136
+ opt.on("-s", "--symbol_list") { display_symbol_list }
137
+ opt.on("-l", "--index_list") { display_color_index }
131
138
 
132
- when "--index_list", "-l"
133
- display_color_index
139
+ begin
140
+ opt.parse!(ARGV)
134
141
 
135
- when "-v"
136
- puts CE::VERSION
142
+ rescue OptionParser::InvalidOption => err
143
+ mess = "[OPTION ERROR] Invalid option ->"
144
+ err.args.each do |op|
145
+ mess += %( '#{op}')
146
+ end
147
+ mess += " (OptionParser::InvalidOption)"
148
+ warn mess + $/ + $/
137
149
 
138
- else
139
150
  usage
140
151
  end
141
-
142
-
143
-
@@ -1,20 +1,7 @@
1
1
  # color_echo
2
- #
3
- # Decorate the command line output with ANSI escape sequense.
4
- # This Library will extend the Kernel module's functions(#print, #puts, #p).
5
- # Required StringIO.
6
- #
7
- begin
8
- require "stringio"
9
- rescue LoadError
10
- warn "color_echo required 'stringio'."
11
- end
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!
12
5
 
13
- require_relative "color_echo/const.rb"
14
- require_relative "color_echo/module/foreground.rb"
15
- require_relative "color_echo/module/background.rb"
16
- require_relative "color_echo/module/textattr.rb"
17
- require_relative "color_echo/module/off.rb"
18
- require_relative "color_echo/variables.rb"
19
- require_relative "color_echo/module_functions.rb"
20
- require_relative "color_echo/override.rb"
6
+ require_relative "color_echo/get"
7
+ require_relative "color_echo/override"
@@ -1,8 +1,8 @@
1
1
  module CE
2
- LIBS_NAME = "color_echo"
3
- VERSION = "0.8.0"
4
- SUMMARY = "Decorate the command line output with ANSI escape sequence."
5
- DOCS_PAGE = "https://github.com/khotta/color_echo"
2
+ LIBS_NAME = "color_echo"
3
+ VERSION = "0.9.0"
4
+ SUMMARY = "Decorate the command line output with ANSI escape sequence."
5
+ DOCS_PAGE = "https://github.com/khotta/color_echo"
6
6
  DESCRIPTION = <<EOS
7
7
  #{SUMMARY}
8
8
  String that is output by "print, puts, p" method is decorated.
@@ -0,0 +1,177 @@
1
+ module CE
2
+ # do not allow to use
3
+ # @return void
4
+ def unuse
5
+ @@enable = false
6
+ end
7
+
8
+ # reset code
9
+ # @param scope symbol|array
10
+ # @return self
11
+ def reset(scope=:all)
12
+ if scope.is_a?(Array)
13
+ scopes = scope
14
+ else
15
+ scopes = [scope]
16
+ end
17
+
18
+ scopes.each do |scope|
19
+ case scope
20
+ when :all
21
+ reset_fg
22
+ reset_bg
23
+ reset_tx
24
+ reset_pickup
25
+ reset_rainbow
26
+ when :fg
27
+ reset_fg
28
+ when :bg
29
+ reset_bg
30
+ when :tx
31
+ reset_tx
32
+ when :pickup
33
+ reset_pickup
34
+ when :rainbow
35
+ reset_rainbow
36
+ end
37
+ end
38
+
39
+ return self
40
+ end
41
+
42
+ # auto off until output set count
43
+ # @params int cnt
44
+ # @return self
45
+ def times(cnt)
46
+ @@cnt_limit = cnt
47
+ return self
48
+ end
49
+
50
+ def once
51
+ times(1)
52
+ end
53
+
54
+ # @param symbol name
55
+ # @return self
56
+ def ch_fg(name)
57
+ return nil if !name.is_a?(Symbol)
58
+
59
+ @@rainbow = false if @@rainbow
60
+ @@code_fg_color = convert_to_code("ForeGround", name)
61
+
62
+ return self
63
+ end
64
+
65
+ # @param symbol name
66
+ # @return self
67
+ def ch_bg(name)
68
+ return nil if !name.is_a?(Symbol)
69
+
70
+ @@rainbow = false if @@rainbow
71
+ @@code_bg_color = convert_to_code("BackGround", name)
72
+
73
+ return self
74
+ end
75
+
76
+ # @param array name : Array of Symbols
77
+ # @return self
78
+ def ch_tx(*names)
79
+ @@rainbow = false if @@rainbow
80
+ names = names[0] if names[0].instance_of?(Array)
81
+
82
+ names.each do |name|
83
+ next if !name.is_a?(Symbol)
84
+ @@code_text_attr += convert_to_code("TextAttr", name)
85
+ end
86
+
87
+ return self
88
+ end
89
+
90
+ # @param symbol fg
91
+ # @param symbol bg
92
+ # @param symbol|array txs
93
+ # @return self
94
+ def ch(fg, bg=nil, txs=nil)
95
+ ch_fg(fg)
96
+ ch_bg(bg)
97
+ ch_tx(*txs) # passing expand
98
+
99
+ return self
100
+ end
101
+
102
+ # to decorate only the specified target
103
+ # @param string|regexp|array target
104
+ # @param symbol fg
105
+ # @param symbol bg
106
+ # @param symbol tx
107
+ # @return self
108
+ def pickup(target, fg=:red, bg=nil, *txs)
109
+ key = target.object_id.to_s
110
+ @@pickup_list[key] = {}
111
+
112
+ if target.is_a?(Array)
113
+ @@pickup_list[key][:patterns] = target
114
+ else
115
+ @@pickup_list[key][:patterns] = [target]
116
+ end
117
+
118
+ if fg.instance_of?(Symbol)
119
+ code_fg = convert_to_code("ForeGround", fg)
120
+ else
121
+ code_fg = ""
122
+ end
123
+
124
+ if bg.instance_of?(Symbol)
125
+ code_bg = convert_to_code("BackGround", bg)
126
+ else
127
+ code_bg = ""
128
+ end
129
+
130
+ code_tx = ""
131
+ if txs.size > 0
132
+ txs = txs[0] if txs[0].instance_of?(Array)
133
+ txs.each do |name|
134
+ next if !name.is_a?(Symbol)
135
+ code_tx += convert_to_code("TextAttr", name)
136
+ end
137
+ end
138
+
139
+ @@pickup_list[key][:code] = code_fg + code_bg + code_tx
140
+
141
+ return self
142
+ end
143
+
144
+ # change mode to rainbow
145
+ # @return void
146
+ def rainbow
147
+ @@rainbow = true
148
+ @@code_rainbow = @@code_bg_color + @@code_text_attr
149
+
150
+ return self
151
+ end
152
+
153
+ # get decorated text
154
+ # require "color_echo/get"
155
+ # @param string text
156
+ def get(text)
157
+ if @@allow_output
158
+ warn (%([WARNING] CE.get; If you want to use this module function; You have to require "color_echo/get" only!))
159
+ return text
160
+ end
161
+
162
+ if !text.is_a?(String)
163
+ text = text.to_s
164
+ end
165
+
166
+ @@task.call(text)
167
+ end
168
+
169
+ # method alias
170
+ alias_method :off, :reset
171
+ alias_method :disable, :reset
172
+ alias_method :fg, :ch_fg
173
+ alias_method :bg, :ch_bg
174
+ alias_method :tx, :ch_tx
175
+
176
+ extend self
177
+ end
@@ -0,0 +1,14 @@
1
+ begin
2
+ require "stringio"
3
+ rescue LoadError
4
+ warn "color_echo required 'stringio'."
5
+ end
6
+
7
+ require_relative "const"
8
+ require_relative "variables"
9
+ require_relative "module/foreground"
10
+ require_relative "module/background"
11
+ require_relative "module/textattr"
12
+ require_relative "module/off"
13
+ require_relative "functions"
14
+ require_relative "internal"
@@ -0,0 +1,186 @@
1
+ module CE
2
+ # is allow to use?
3
+ # @return bool
4
+ def enable?
5
+ return @@enable
6
+ end
7
+
8
+ # is allow to use? and is set code?
9
+ # @return bool
10
+ def available?
11
+ return @@enable && isset?
12
+ end
13
+
14
+ # is set code?
15
+ # @return bool
16
+ def isset?
17
+ return get_start_code != "" || @@pickup_list.size > 0 || @@rainbow
18
+ end
19
+
20
+ # reset foreground code
21
+ # @return self
22
+ def reset_fg
23
+ @@code_fg_color = ""
24
+ return self
25
+ end
26
+
27
+ # reset background code
28
+ # @return self
29
+ def reset_bg
30
+ @@code_bg_color = ""
31
+ return self
32
+ end
33
+
34
+ # reset text attr code
35
+ # @return self
36
+ def reset_tx
37
+ @@code_text_attr = ""
38
+ return self
39
+ end
40
+
41
+ # reset pickup code
42
+ # @return self
43
+ def reset_pickup
44
+ @@pickup_list = {}
45
+ return self
46
+ end
47
+
48
+ # reset and off raubow mode
49
+ # @return self
50
+ def reset_rainbow
51
+ @@code_rainbow = ""
52
+ @@rainbow = false
53
+ return self
54
+ end
55
+
56
+ # reset all state of code
57
+ # @return self
58
+ def reset_all
59
+ reset_fg
60
+ reset_bg
61
+ reset_tx
62
+ reset_pickup
63
+ reset_rainbow
64
+ return self
65
+ end
66
+
67
+ # return start escape sequence code
68
+ # @return string
69
+ def get_start_code
70
+ if @@rainbow
71
+ return @@code_rainbow
72
+ else
73
+ return @@code_fg_color + @@code_bg_color + @@code_text_attr
74
+ end
75
+ end
76
+
77
+ # @return String
78
+ def get_reset_code
79
+ return self::Off::ALL
80
+ end
81
+
82
+ # add reset & start code to line feed code
83
+ # @param string input
84
+ def add_reset_line_feed(input)
85
+ input.gsub!(/#{$/}/, get_reset_code + $/ + get_start_code)
86
+ input += get_reset_code
87
+ return input
88
+ end
89
+
90
+ # add code to be a rainbow color
91
+ # @param string text
92
+ # @return string
93
+ def add_rainbow(text)
94
+ cnt = 0
95
+ output = get_start_code
96
+
97
+ text.each_char do |char|
98
+ if char == $/
99
+ output += get_reset_code + $/ + get_start_code
100
+ next
101
+ end
102
+
103
+ case cnt
104
+ when 0
105
+ output += ForeGround::RED + char + Off::ALL + get_start_code
106
+ when 1
107
+ output += ForeGround::GREEN + char + Off::ALL + get_start_code
108
+ when 2
109
+ output += ForeGround::YELLOW + char + Off::ALL + get_start_code
110
+ when 3
111
+ output += ForeGround::BLUE + char + Off::ALL + get_start_code
112
+ when 4
113
+ output += ForeGround::MAGENTA + char + Off::ALL + get_start_code
114
+ when 5
115
+ output += ForeGround::CYAN + char + Off::ALL + get_start_code
116
+ when 6
117
+ output += ForeGround::WHITE + char + Off::ALL + get_start_code
118
+ end
119
+ cnt += 1
120
+ cnt = 0 if cnt >= 7
121
+ end
122
+
123
+ output += get_reset_code
124
+
125
+ return output
126
+ end
127
+
128
+ # add pickup code
129
+ # @param string text
130
+ # @return string
131
+ def add_pickup_code(text)
132
+ @@pickup_list.each_pair do |key, hash|
133
+ patterns = hash[:patterns]
134
+ code = hash[:code]
135
+
136
+ # repeat to each specified pickup pattern
137
+ patterns.each do |pattern|
138
+ # pattern is Regexp
139
+ if pattern.is_a?(Regexp)
140
+ after_text = ""
141
+ # global match
142
+ (text.scan(pattern)).size.times do
143
+ pattern =~ text
144
+ after_text += $` + code + $& + get_reset_code + get_start_code
145
+ text = $'
146
+ end
147
+ text = after_text + text
148
+
149
+ # pattern is String
150
+ else
151
+ text.gsub!(pattern, code + pattern + get_reset_code + get_start_code)
152
+ end
153
+ end
154
+ end
155
+
156
+ return text
157
+ end
158
+
159
+ # get sequence code by symbol
160
+ # @param string module_name
161
+ # @param symbol name
162
+ # @return string
163
+ def convert_to_code(module_name, name)
164
+ return "" if (name == nil || name == "")
165
+
166
+ begin
167
+ cname = name.to_s.swapcase
168
+
169
+ # specified color index
170
+ if cname.index("INDEX")
171
+ num = cname.sub("INDEX", "").to_i - 1
172
+ return eval(%{#{module_name}::INDEX}) + num.to_s + "m"
173
+ end
174
+
175
+ code = eval(%{#{module_name}::#{cname}})
176
+ rescue NameError
177
+ raise(NameError ,%{:#{name} is not defined! Please check reference.})
178
+ end
179
+ return code
180
+ end
181
+
182
+ # @return proc
183
+ def task
184
+ return @@task
185
+ end
186
+ end
@@ -1,3 +1,7 @@
1
+ module CE
2
+ @@allow_output = true
3
+ end
4
+
1
5
  # override Kernel methods
2
6
  def print(*arg)
3
7
  CE.task.call(*arg)
@@ -8,10 +8,14 @@ module CE
8
8
  @@rainbow = false
9
9
  @@pickup_list = {}
10
10
  @@cnt_limit = 0
11
+ @@allow_output = false
11
12
 
12
13
  @@print = method :print
13
14
  @@p = method :p
14
15
  @@puts = method :puts
16
+ @@get = lambda do |text|
17
+ print text
18
+ end
15
19
 
16
20
  @@task = lambda do |*arg|
17
21
  if available?
@@ -27,7 +31,7 @@ module CE
27
31
 
28
32
  #is_hit_pickup = false
29
33
 
30
- # output to STDOUT
34
+ # get output text
31
35
  if @@rainbow
32
36
  output = add_rainbow(strio.string)
33
37
  else
@@ -47,21 +51,30 @@ module CE
47
51
  output = add_reset_line_feed(output)
48
52
  end
49
53
 
50
- $stdout.print output
51
-
52
- # auto off
53
- if @@cnt_limit > 0
54
- @@cnt_limit -= 1
55
- # to reset
56
- if @@cnt_limit == 0
57
- reset [:fg, :bg, :tx, :rainbow]
54
+ # output to STDOUT
55
+ if @@allow_output
56
+ $stdout.print output
57
+ # auto off
58
+ if @@cnt_limit > 0
59
+ @@cnt_limit -= 1
60
+ # to reset
61
+ if @@cnt_limit == 0
62
+ reset [:fg, :bg, :tx, :rainbow]
63
+ end
58
64
  end
65
+ else
66
+ reset
67
+ return output
59
68
  end
60
69
 
61
70
  # no available "color echo"
62
71
  else
63
- # call original method
64
- eval("@@#{caller_locations(2).first.label}").call(*arg)
72
+ if @@allow_output
73
+ # call original method
74
+ eval("@@#{caller_locations(2).first.label}").call(*arg)
75
+ else
76
+ return arg
77
+ end
65
78
  end
66
79
  end
67
80
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: color_echo
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.0
4
+ version: 0.9.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-14 00:00:00.000000000 Z
11
+ date: 2015-01-19 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: |
14
14
  Decorate the command line output with ANSI escape sequence.
@@ -25,11 +25,13 @@ files:
25
25
  - bin/color_echo
26
26
  - lib/color_echo.rb
27
27
  - lib/color_echo/const.rb
28
+ - lib/color_echo/functions.rb
29
+ - lib/color_echo/get.rb
30
+ - lib/color_echo/internal.rb
28
31
  - lib/color_echo/module/background.rb
29
32
  - lib/color_echo/module/foreground.rb
30
33
  - lib/color_echo/module/off.rb
31
34
  - lib/color_echo/module/textattr.rb
32
- - lib/color_echo/module_functions.rb
33
35
  - lib/color_echo/override.rb
34
36
  - lib/color_echo/variables.rb
35
37
  homepage: https://github.com/khotta/color_echo
@@ -48,7 +50,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
48
50
  requirements:
49
51
  - - ">="
50
52
  - !ruby/object:Gem::Version
51
- version: 1.9.3
53
+ version: 2.0.0
52
54
  required_rubygems_version: !ruby/object:Gem::Requirement
53
55
  requirements:
54
56
  - - ">="
@@ -1,374 +0,0 @@
1
- module CE
2
- # is allow to use?
3
- # @return bool
4
- def enable?
5
- return @@enable
6
- end
7
-
8
- # is allow to use? and is set code?
9
- # @return bool
10
- def available?
11
- return @@enable && isset?
12
- end
13
-
14
- # is set code?
15
- # @return bool
16
- def isset?
17
- return get_start_code != "" || @@pickup_list.size > 0 || @@rainbow
18
- end
19
-
20
- # do not allow to use
21
- # @return void
22
- def unuse
23
- @@enable = false
24
- end
25
-
26
- # reset code
27
- # @param scope symbol|array
28
- # @return self
29
- def reset(scope=:all)
30
- if scope.is_a?(Array)
31
- scopes = scope
32
- else
33
- scopes = [scope]
34
- end
35
-
36
- scopes.each do |scope|
37
- case scope
38
- when :all
39
- reset_fg
40
- reset_bg
41
- reset_tx
42
- reset_pickup
43
- reset_rainbow
44
- when :fg
45
- reset_fg
46
- when :bg
47
- reset_bg
48
- when :tx
49
- reset_tx
50
- when :pickup
51
- reset_pickup
52
- when :rainbow
53
- reset_rainbow
54
- end
55
- end
56
-
57
- return self
58
- end
59
-
60
- # reset foreground code
61
- # @return self
62
- def reset_fg
63
- @@code_fg_color = ""
64
- return self
65
- end
66
-
67
- # reset background code
68
- # @return self
69
- def reset_bg
70
- @@code_bg_color = ""
71
- return self
72
- end
73
-
74
- # reset text attr code
75
- # @return self
76
- def reset_tx
77
- @@code_text_attr = ""
78
- return self
79
- end
80
-
81
- # reset pickup code
82
- # @return self
83
- def reset_pickup
84
- @@pickup_list = {}
85
- return self
86
- end
87
-
88
- # reset and off raubow mode
89
- # @return self
90
- def reset_rainbow
91
- @@code_rainbow = ""
92
- @@rainbow = false
93
- return self
94
- end
95
-
96
- # reset all state of code
97
- # @return self
98
- def reset_all
99
- reset_fg
100
- reset_bg
101
- reset_tx
102
- reset_pickup
103
- reset_rainbow
104
- return self
105
- end
106
-
107
- # auto off until output set count
108
- # @params int cnt
109
- # @return self
110
- def times(cnt)
111
- @@cnt_limit = cnt
112
- return self
113
- end
114
-
115
- def once
116
- times(1)
117
- end
118
-
119
- # @param symbol name
120
- # @return self
121
- def ch_fg(name)
122
- return nil if !name.instance_of?(Symbol)
123
-
124
- @@rainbow = false if @@rainbow
125
- @@code_fg_color = convert_to_code("ForeGround", name)
126
-
127
- return self
128
- end
129
-
130
- # @param symbol name
131
- # @return self
132
- def ch_bg(name)
133
- return nil if !name.instance_of?(Symbol)
134
-
135
- @@rainbow = false if @@rainbow
136
- @@code_bg_color = convert_to_code("BackGround", name)
137
-
138
- return self
139
- end
140
-
141
- # @param array name : Array of Symbols
142
- # @return self
143
- def ch_tx(*names)
144
- @@rainbow = false if @@rainbow
145
- names = names[0] if names[0].instance_of?(Array)
146
-
147
- names.each do |name|
148
- next if !name.instance_of?(Symbol)
149
- @@code_text_attr += convert_to_code("TextAttr", name)
150
- end
151
-
152
- return self
153
- end
154
-
155
- # @param symbol fg
156
- # @param symbol bg
157
- # @param symbol|array txs
158
- # @return self
159
- def ch(fg, bg=nil, txs=nil)
160
- ch_fg(fg)
161
- ch_bg(bg)
162
- ch_tx(*txs) # passing expand
163
-
164
- return self
165
- end
166
-
167
- # to decorate only the specified target
168
- # @param string|regexp|array target
169
- # @param symbol fg
170
- # @param symbol bg
171
- # @param symbol tx
172
- # @return self
173
- def pickup(target, fg=:red, bg=nil, *txs)
174
- key = target.object_id.to_s
175
- @@pickup_list[key] = {}
176
-
177
- if target.is_a?(Array)
178
- @@pickup_list[key][:patterns] = target
179
- else
180
- @@pickup_list[key][:patterns] = [target]
181
- end
182
-
183
- if fg.instance_of?(Symbol)
184
- code_fg = convert_to_code("ForeGround", fg)
185
- else
186
- code_fg = ""
187
- end
188
-
189
- if bg.instance_of?(Symbol)
190
- code_bg = convert_to_code("BackGround", bg)
191
- else
192
- code_bg = ""
193
- end
194
-
195
- code_tx = ""
196
- if txs.size > 0
197
- txs = txs[0] if txs[0].instance_of?(Array)
198
- txs.each do |name|
199
- next if !name.instance_of?(Symbol)
200
- code_tx += convert_to_code("TextAttr", name)
201
- end
202
- end
203
-
204
- @@pickup_list[key][:code] = code_fg + code_bg + code_tx
205
-
206
- return self
207
- end
208
-
209
- # change mode to rainbow
210
- # @return void
211
- def rainbow
212
- @@rainbow = true
213
- @@code_rainbow = @@code_bg_color + @@code_text_attr
214
- end
215
-
216
- # return start escape sequence code
217
- # @return string
218
- def get_start_code
219
- if @@rainbow
220
- return @@code_rainbow
221
- else
222
- return @@code_fg_color + @@code_bg_color + @@code_text_attr
223
- end
224
- end
225
-
226
- # @return String
227
- def get_reset_code
228
- return self::Off::ALL
229
- end
230
-
231
- # add reset & start code to line feed code
232
- # @param string input
233
- def add_reset_line_feed(input)
234
- input.gsub!(/#{$/}/, CE::get_reset_code + $/ + CE::get_start_code)
235
- input += CE::get_reset_code
236
- return input
237
- end
238
-
239
- # add code to be a rainbow color
240
- # @param string text
241
- # @return string
242
- def add_rainbow(text)
243
- cnt = 0
244
- output = get_start_code
245
-
246
- text.each_char do |char|
247
- if char == $/
248
- output += get_reset_code + $/ + get_start_code
249
- next
250
- end
251
-
252
- case cnt
253
- when 0
254
- output += ForeGround::RED + char + Off::ALL + get_start_code
255
- when 1
256
- output += ForeGround::GREEN + char + Off::ALL + get_start_code
257
- when 2
258
- output += ForeGround::YELLOW + char + Off::ALL + get_start_code
259
- when 3
260
- output += ForeGround::BLUE + char + Off::ALL + get_start_code
261
- when 4
262
- output += ForeGround::MAGENTA + char + Off::ALL + get_start_code
263
- when 5
264
- output += ForeGround::CYAN + char + Off::ALL + get_start_code
265
- when 6
266
- output += ForeGround::WHITE + char + Off::ALL + get_start_code
267
- end
268
- cnt += 1
269
- cnt = 0 if cnt >= 7
270
- end
271
-
272
- output += get_reset_code
273
-
274
- return output
275
- end
276
-
277
- # add pickup code
278
- # @param string text
279
- # @return string
280
- def add_pickup_code(text)
281
- @@pickup_list.each_pair do |key, hash|
282
- patterns = hash[:patterns]
283
- code = hash[:code]
284
-
285
- # repeat to each specified pickup pattern
286
- patterns.each do |pattern|
287
- # pattern is Regexp
288
- if pattern.is_a?(Regexp)
289
- after_text = ""
290
- # global match
291
- (text.scan(pattern)).size.times do
292
- pattern =~ text
293
- after_text += $` + code + $& + get_reset_code + get_start_code
294
- text = $'
295
- end
296
- text = after_text + text
297
-
298
- # pattern is String
299
- else
300
- text.gsub!(pattern, code + pattern + get_reset_code + get_start_code)
301
- end
302
- end
303
- end
304
-
305
- return text
306
- end
307
-
308
- # get sequence code by symbol
309
- # @param string module_name
310
- # @param symbol name
311
- # @return string
312
- def convert_to_code(module_name, name)
313
- return "" if (name == nil || name == "")
314
-
315
- begin
316
- cname = name.to_s.swapcase
317
-
318
- # specified color index
319
- if cname.index("INDEX")
320
- num = cname.sub("INDEX", "").to_i - 1
321
- return eval(%{#{module_name}::INDEX}) + num.to_s + "m"
322
- end
323
-
324
- code = eval(%{#{module_name}::#{cname}})
325
- rescue NameError
326
- raise(NameError ,%{:#{name} is not defined! Please check reference.})
327
- end
328
- return code
329
- end
330
-
331
- # @return proc
332
- def task
333
- return @@task
334
- end
335
-
336
- # method alias
337
- alias_method :off, :reset
338
- alias_method :disable, :reset
339
- alias_method :fg, :ch_fg
340
- alias_method :bg, :ch_bg
341
- alias_method :tx, :ch_tx
342
-
343
- module_function :available?,
344
- :isset?,
345
- :enable?,
346
- :unuse,
347
- :reset,
348
- :reset_fg,
349
- :reset_bg,
350
- :reset_tx,
351
- :reset_pickup,
352
- :reset_rainbow,
353
- :reset_all,
354
- :off,
355
- :disable,
356
- :times,
357
- :once,
358
- :ch_fg,
359
- :fg,
360
- :ch_bg,
361
- :bg,
362
- :ch_tx,
363
- :tx,
364
- :ch,
365
- :pickup,
366
- :rainbow,
367
- :get_start_code,
368
- :get_reset_code,
369
- :add_reset_line_feed,
370
- :add_rainbow,
371
- :add_pickup_code,
372
- :convert_to_code,
373
- :task
374
- end