color_echo 0.1.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +42 -33
  3. data/lib/color_echo.rb +142 -95
  4. metadata +4 -4
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 45220e4d771e41b9cf1eaec6aea9765f890df1f5
4
- data.tar.gz: 92f0f3d0abe43075121cd563e1b5f26df20bedcb
3
+ metadata.gz: 922b6df5b99b54e8fc52dd2716fa5acb778b2704
4
+ data.tar.gz: 8c39228ed3879d2091f0bf2925374bcae33ae83f
5
5
  SHA512:
6
- metadata.gz: bdb27477e4aa9be98a35a2e62dc1ab8c99c3fa92357e354391197375b4f3605a3adb486e7691add9870dc1dcbeb839f3515b1440085473c9326990aad24bccb3
7
- data.tar.gz: 1aaa00927976c25fd398996c46f30f0885a103856a98858027a0590fa5d60636619041fcf87b4d2517b3017bd10b0f00ee443674e4c7307acf0e41d7e259fa9a
6
+ metadata.gz: 62f7a9e07a0ce3773ec8d2f98c5413aba5af2546521ef5738fce07a7e2e2a0ac017085b794dfd62193f3c17580fa31cabde99a2abd6f6866a618c859c16cf78e
7
+ data.tar.gz: fa98969050f46c0acba35c77daa25cc7a4ebe638f058c386696c193651a871712d85b4b71d02ff52fb041fec4308dd26e699d1135cb14b189880058c34b392ae
data/README.md CHANGED
@@ -21,8 +21,10 @@ Or install it yourself as:
21
21
  ## Usage
22
22
 
23
23
  ### module functions
24
- #### CE::ch_fg :symbol
24
+ #### CE.ch_fg :symbol
25
25
  Change the foreground color to the your specified color.
26
+ Alias is, CE.fg
27
+ This method alias is available in version 0.2.0 on and after.
26
28
 
27
29
  * symbol list:
28
30
  * black
@@ -34,11 +36,13 @@ Change the foreground color to the your specified color.
34
36
  * cyan
35
37
  * white
36
38
 
37
- ex.) CE::ch_fg :red #=> foreground color will be changed red
39
+ ex.) CE.ch_fg :red #=> foreground color will be changed red
38
40
 
39
-
40
- #### CE::ch_bg :symbol
41
+ #### CE.ch_bg :symbol
41
42
  Change the background color to the your specified color.
43
+ Alias is, CE.bg
44
+ This method alias is available in version 0.2.0 on and after.
45
+
42
46
 
43
47
  * symbol list:
44
48
  * black
@@ -50,9 +54,9 @@ Change the background color to the your specified color.
50
54
  * cyan
51
55
  * white
52
56
 
53
- ex.) CE::ch_bg :white #=> background color will be changed white
57
+ ex.) CE.ch_bg :white #=> background color will be changed white
54
58
 
55
- #### CE::ch_tx :symbol
59
+ #### CE.ch_tx :symbol
56
60
  Change the text attribute to the your specified decoration.
57
61
 
58
62
  * symbol list:
@@ -62,22 +66,29 @@ Change the text attribute to the your specified decoration.
62
66
  * reverse_video
63
67
  * concealed
64
68
 
65
- ex.) CE::ch_tx :blink #=> text blink on
69
+ ex.) CE.ch_tx :blink #=> text blink on
70
+ Alias is, CE.tx
71
+ This method alias is available in version 0.2.0 on and after.
72
+
66
73
 
67
- #### CE::ch :foreground [,:background [,:text_attribute]]
74
+ #### CE.ch :foreground [,:background [,:text_attribute]]
68
75
  Change collectively.
69
76
  This method is available in version 0.1.0 on and after.
70
- ex.) CE:ch :white, :green
77
+ ex.) CE.ch :white, :green
71
78
 
72
- #### CE::disable
79
+ #### CE.disable
73
80
  Reset to set the color sequence.
74
81
  Alias is, CE::off, CE::reset
75
82
  This method alias is available in version 0.1.0 on and after.
76
83
 
77
- #### CE::unuse
84
+ #### CE.unuse
78
85
  Force ignore the function of this library.
79
86
  This method is available in version 0.1.0 on and after.
80
87
 
88
+ #### CE.rainbow
89
+ Enjoy rainbow.
90
+ This method is available in version 0.2.0 on and after.
91
+
81
92
  ### Example
82
93
  <pre>
83
94
  #
@@ -88,46 +99,44 @@ require "color_echo"
88
99
  # force ignore the function of this library
89
100
  #CE::unuse
90
101
 
91
- # change the foreground color to 'yellow'
92
- CE::ch_fg :yellow
93
-
102
+ CE.ch :yellow
94
103
  puts "fooooooooo"
95
104
  puts "baaaaaaaar"
96
105
 
97
- # change the foreground color to 'white'
98
- CE::ch_fg :white
99
- # change the foreground color to 'red'
100
- CE::ch_bg :red
101
-
106
+ CE.ch :white, :red
102
107
  print "testtesttest"
103
108
 
104
- # change the foreground color to 'black'
105
- CE::ch_fg :black
106
- # change the foreground color to 'cyan'
107
- CE::ch_bg :cyan
108
- # change the text attribute 'underscore'
109
- CE::ch_tx :underscore
110
-
111
- ary = ["aaa", "bbb", "ccc"]
109
+ CE.ch :black, :cyan, :underscore
110
+ ary = ["Duis", "aute", "irure", "dolor", "in", "reprehenderit", "in", "voluptate"]
112
111
  p ary
113
112
  print ary
114
113
 
115
- CE::ch_bg :blue
116
- CE::ch_fg :white
114
+ CE.off
115
+
116
+ CE.fg :white
117
117
  p "aaaaa", "bbbbb", "ccccc", "ddddd", "eeeee"
118
118
 
119
- # reset the color sequence
120
- CE::off
119
+ CE.bg :magenta
120
+ print ary
121
121
 
122
+ CE.reset
122
123
  puts "hogehoge"
123
124
 
124
- # to change collectively
125
- CE::ch :black, :white, :blink
125
+ CE.tx :underscore
126
+ puts "hogehoge"
126
127
 
128
+ CE.ch :black, :white, :blink
127
129
  puts &lt;&lt;EOM
128
130
  Lorem ipsum dolor sit amet,
129
131
  consectetur adipisicing elit,
130
132
  sed do eiusmod tempor incididunt
131
133
  ut labore et dolore magna aliqua.
132
134
  EOM
135
+
136
+ CE.rainbow
137
+ puts "String will be rainbow! You can specify the argument only one."
138
+ puts ["rainbow", "is", "String", "only"]
139
+ puts "this is rainbow.", "This argument is ignored when rainbow mode."
140
+ CE.off
141
+ puts "Disable rainbow mode."
133
142
  </pre>
data/lib/color_echo.rb CHANGED
@@ -1,17 +1,58 @@
1
- # ColorEcho - handle the ANSI color sequences.
1
+ # color_echo
2
+ #
3
+ # To add color to the command line output.
4
+ # This Library will extend the Kernel module's functions(#print, #puts, #p).
2
5
  # @autor: Kazuya Hotta (nyanko)
3
6
  #
4
7
  require "stringio"
5
8
 
6
9
  module CE
7
- VERSION = "0.1.1"
10
+ VERSION = "0.2.0"
8
11
  CODE_RESET = "\e[0m"
9
12
 
10
- @@use = true
11
- @@enable = false
13
+ @@enable = true
14
+ @@isset = false
12
15
  @@code_bg_color = ""
13
16
  @@code_fg_color = ""
14
17
  @@code_text_attr = ""
18
+ @@rainbow = false
19
+
20
+ @@print = method :print
21
+ @@p = method :p
22
+ @@puts = method :puts
23
+
24
+ @@task = lambda do |*arg|
25
+ if available?
26
+ if @@rainbow && arg.first.instance_of?(String)
27
+ arg = add_rainbow(arg.first)
28
+
29
+ # call original method
30
+ eval("@@#{caller_locations(2).first.label}").call(arg)
31
+
32
+ else
33
+ # change output destination to StringIO Object
34
+ strio = StringIO.new
35
+ $stdout = strio
36
+
37
+ # output color sequence
38
+ $stdout.print get_start_code
39
+
40
+ # call original method
41
+ eval("@@#{caller_locations(2).first.label}").call(arg)
42
+
43
+ # change output destination to STDOUT
44
+ $stdout = STDOUT
45
+
46
+ # output to STDOUT
47
+ $stdout.print add_reset_line_feed(strio.string)
48
+ end
49
+
50
+ # no available "color echo"
51
+ else
52
+ # call original method
53
+ eval("@@#{caller_locations(2).first.label}").call(arg)
54
+ end
55
+ end
15
56
 
16
57
  module ForeGround
17
58
  BLACK = 30
@@ -44,46 +85,50 @@ module CE
44
85
  CONCEALED = 8
45
86
  end
46
87
 
47
- module_function
48
- def enable?
49
- return @@enable && @@use
88
+ public
89
+ def available?
90
+ return @@enable && @@isset
50
91
  end
51
92
 
52
- def disable
53
- @@enable = false
54
- @@code_bg_color = ""
55
- @@code_fg_color = ""
56
- @@code_text_attr = ""
93
+ def isset?
94
+ return @@isset
57
95
  end
58
96
 
59
- def off
60
- disable
97
+ def enable?
98
+ return @@enable
61
99
  end
62
100
 
63
- def reset
64
- disable
101
+ def unuse
102
+ @@enable = false
65
103
  end
66
104
 
67
- def unuse
68
- @@use = false
105
+ def reset
106
+ @@isset = false
107
+ @@rainbow = false
108
+ @@code_bg_color = ""
109
+ @@code_fg_color = ""
110
+ @@code_text_attr = ""
69
111
  end
70
112
 
71
113
  def ch_fg(name)
72
114
  return nil if !name.instance_of?(Symbol)
73
- @@enable = true if !@@enable
74
- @@code_fg_color = get_code("ForeGround", name)
115
+ @@isset = true if !@@isset
116
+ @@rainbow = false if @@rainbow
117
+ @@code_fg_color = convert_to_code("ForeGround", name)
75
118
  end
76
119
 
77
120
  def ch_bg(name)
78
121
  return nil if !name.instance_of?(Symbol)
79
- @@enable = true if !@@enable
80
- @@code_bg_color = get_code("BackGround", name)
122
+ @@isset = true if !@@isset
123
+ @@rainbow = false if @@rainbow
124
+ @@code_bg_color = convert_to_code("BackGround", name)
81
125
  end
82
126
 
83
127
  def ch_tx(name)
84
128
  return nil if !name.instance_of?(Symbol)
85
- @@enable = true if !@@enable
86
- @@code_text_attr = get_code("TextAttr", name)
129
+ @@isset = true if !@@isset
130
+ @@rainbow = false if @@rainbow
131
+ @@code_text_attr = convert_to_code("TextAttr", name)
87
132
  end
88
133
 
89
134
  def ch(fg, bg=nil, tx=nil)
@@ -92,7 +137,7 @@ module CE
92
137
  ch_tx(tx)
93
138
  end
94
139
 
95
- def get_color_code
140
+ def get_start_code
96
141
  return @@code_fg_color + @@code_bg_color + @@code_text_attr
97
142
  end
98
143
 
@@ -100,7 +145,47 @@ module CE
100
145
  return self::CODE_RESET
101
146
  end
102
147
 
103
- def get_code(module_name, name)
148
+ # add reset & start code to line feed code
149
+ def add_reset_line_feed(input)
150
+ input.gsub!(/#{$/}/, CE::get_reset_code + $/ + CE::get_start_code)
151
+ input += CE::get_reset_code
152
+ return input
153
+ end
154
+
155
+ def rainbow
156
+ @@isset = true if !@@isset
157
+ @@rainbow = true
158
+ end
159
+
160
+ def add_rainbow(text)
161
+ cnt = 0
162
+ output = ""
163
+ text.each_char do |char|
164
+ output += $/ if char == $/
165
+
166
+ case cnt
167
+ when 0
168
+ output += "\e[31m" + char + "\e[0m"
169
+ when 1
170
+ output += "\e[32m" + char + "\e[0m"
171
+ when 2
172
+ output += "\e[33m" + char + "\e[0m"
173
+ when 3
174
+ output += "\e[34m" + char + "\e[0m"
175
+ when 4
176
+ output += "\e[35m" + char + "\e[0m"
177
+ when 5
178
+ output += "\e[36m" + char + "\e[0m"
179
+ when 6
180
+ output += "\e[37m" + char + "\e[0m"
181
+ end
182
+ cnt += 1
183
+ cnt = 0 if cnt >= 7
184
+ end
185
+ return output
186
+ end
187
+
188
+ def convert_to_code(module_name, name)
104
189
  begin
105
190
  code = eval(%{#{module_name}::#{name.to_s.swapcase!}})
106
191
  rescue NameError
@@ -108,82 +193,44 @@ module CE
108
193
  end
109
194
  return "\e[#{code}m"
110
195
  end
196
+
197
+ def task
198
+ return @@task
199
+ end
200
+
201
+ alias_method :off, :reset
202
+ alias_method :disable, :reset
203
+ alias_method :fg, :ch_fg
204
+ alias_method :bg, :ch_bg
205
+ alias_method :tx, :ch_tx
206
+
207
+ module_function :available?,
208
+ :isset?,
209
+ :enable?,
210
+ :unuse,
211
+ :reset, :off, :disable,
212
+ :ch_fg, :fg,
213
+ :ch_bg, :bg,
214
+ :ch_tx, :tx,
215
+ :ch,
216
+ :get_start_code,
217
+ :get_reset_code,
218
+ :add_reset_line_feed,
219
+ :add_rainbow,
220
+ :rainbow,
221
+ :convert_to_code,
222
+ :task
111
223
  end
112
224
 
225
+ # override Kernel methods
113
226
  def print(*arg)
114
- if CE::enable?
115
- # change output destination to StringIO Object
116
- strio = StringIO.new
117
- $stdout = strio
118
-
119
- # output color sequence
120
- $stdout.print(CE::get_color_code)
121
-
122
- super
123
-
124
- # change output destination to STDOUT
125
- $stdout = STDOUT
126
-
127
- # add reset & start code to line feed code
128
- output = strio.string
129
- output.gsub!(/#{$/}/, CE::get_reset_code + $/ + CE::get_color_code)
130
- output += CE::get_reset_code
131
-
132
- # output to STDOUT
133
- $stdout.print(output)
134
- else
135
- super
136
- end
227
+ CE.task.call(*arg)
137
228
  end
138
229
 
139
230
  def p(*arg)
140
- if CE::enable?
141
- # change output destination to StringIO Object
142
- strio = StringIO.new
143
- $stdout = strio
144
-
145
- # output color sequence
146
- $stdout.print(CE::get_color_code)
147
-
148
- super
149
-
150
- # change output destination to STDOUT
151
- $stdout = STDOUT
152
-
153
- # add reset & start code to line feed code
154
- output = strio.string
155
- output.gsub!(/#{$/}/, CE::get_reset_code + $/ + CE::get_color_code)
156
- output += CE::get_reset_code
157
-
158
- # output to STDOUT
159
- $stdout.print(output)
160
- else
161
- super
162
- end
231
+ CE.task.call(*arg)
163
232
  end
164
233
 
165
234
  def puts(*arg)
166
- if CE::enable?
167
- # change output destination to StringIO Object
168
- strio = StringIO.new
169
- $stdout = strio
170
-
171
- # output color sequence
172
- $stdout.print(CE::get_color_code)
173
-
174
- super
175
-
176
- # change output destination to STDOUT
177
- $stdout = STDOUT
178
-
179
- # add reset & start code to line feed code
180
- output = strio.string
181
- output.gsub!(/#{$/}/, CE::get_reset_code + $/ + CE::get_color_code)
182
- output += CE::get_reset_code
183
-
184
- # output to STDOUT
185
- $stdout.print(output)
186
- else
187
- super
188
- end
235
+ CE.task.call(*arg)
189
236
  end
metadata CHANGED
@@ -1,17 +1,17 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: color_echo
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - nyanko
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-11-14 00:00:00.000000000 Z
11
+ date: 2014-11-26 00:00:00.000000000 Z
12
12
  dependencies: []
13
- description: 'This Library will extend the Kernel module''s functions(#print, #puts,
14
- #p). required StringIO.'
13
+ description: 'To add color to the command line output.This Library will extend the
14
+ Kernel module''s functions(#print, #puts, #p). required StringIO.'
15
15
  email:
16
16
  - nyanko@dammy
17
17
  executables: []