color_echo 0.0.1 → 0.1.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 +35 -5
  3. data/lib/color_echo.rb +52 -25
  4. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 4fb008ce89e82f2f08c590083abfac3efdd76048
4
- data.tar.gz: 788ae07cbba6fb72fb69e22ed46a61f67d34a793
3
+ metadata.gz: 4f141517813f0c4403368ca4efe8a32cda457617
4
+ data.tar.gz: 53b0606c73544514134a8c1154fcee1453e29ed8
5
5
  SHA512:
6
- metadata.gz: 8f3ea2c4d3424c360024814d89f917e39f4455eb24e978305b6140c7582f9f85163e2706de3c55cc15b5f99bc66e61a795bb041200891b3db9b3422053ee480f
7
- data.tar.gz: 07ae5482c36aa0db7b477daa36ca4f219f0da82bddc57876aa175d4e37129e4b1d9be1043649b757b15ae7ceeea3ff11f51b7315e8db7498e89031f56f654a0e
6
+ metadata.gz: 2048cd7a0df691e92fcbde6bd24890bc4933a9635a62ea9fb1b470882a2fca28c1fe52daa69f1540431a049e74e24936e65e824baa932b9b2f0aa6e46d422eb2
7
+ data.tar.gz: 9fe9745597d108c64a022b3c72c12b5e0cdf7334d5c90be7bed184d5b75e0407f400e5e7cfc9c3e53ad75a900756703ebe1e232d30df17b34786dd14c1acb604
data/README.md CHANGED
@@ -1,9 +1,9 @@
1
- # ColorEcho
1
+ # color_echo
2
2
  To add color to the command line output.
3
3
  This Library will extend the Kernel module's functions(#print, #puts, #p).
4
4
  required StringIO.
5
5
 
6
- Version: 0.0.1
6
+ Version: 0.1.0
7
7
  Compliant Rubys Version: 1.9.3, 2.0.0, 2.1.0 (for Linux)
8
8
  License: MIT
9
9
  Gems repository: http://rubygems.org/gems/color_echo
@@ -37,7 +37,7 @@ Change the foreground color to the your specified color.
37
37
  ex.) CE::ch_fg :red #=> foreground color will be changed red
38
38
 
39
39
 
40
- #### CE::ch_bg::symbol
40
+ #### CE::ch_bg :symbol
41
41
  Change the background color to the your specified color.
42
42
 
43
43
  * symbol list:
@@ -64,14 +64,30 @@ Change the text attribute to the your specified decoration.
64
64
 
65
65
  ex.) CE::ch_tx :blink #=> text blink on
66
66
 
67
+ #### CE::ch :foreground [,:background [,:text_attribute]]
68
+ Change collectively.
69
+ This method is available in version 0.1.0.
70
+ ex.) CE:ch :white, :green
71
+
67
72
  #### CE::disable
68
- Turn off the function of this library.
73
+ Reset to set the color sequence.
74
+ Alias is, CE::off, CE::reset
75
+ This method alias is available in version 0.1.0.
76
+
77
+ #### CE::not_use
78
+ Force ignore the function of this library.
79
+ This method is available in version 0.1.0.
69
80
 
70
81
  ### Example
71
82
  <pre>
72
83
  #
73
84
  # Example Code
74
85
  #
86
+ require "color_echo"
87
+
88
+ # force ignore the function of this library
89
+ #CE::not_use
90
+
75
91
  # change the foreground color to 'yellow'
76
92
  CE::ch_fg :yellow
77
93
 
@@ -96,10 +112,24 @@ ary = ["aaa", "bbb", "ccc"]
96
112
  p ary
97
113
  print ary
98
114
 
115
+ CE::ch_bg :blue
116
+ CE::ch_fg :white
117
+ p "aaaaa", "bbbbb", "ccccc", "ddddd", "eeeee"
118
+
99
119
  # reset the color sequence
100
- CE::disable
120
+ CE::off
101
121
 
102
122
  puts "hogehoge"
123
+
124
+ # to change collectively
125
+ CE::ch :black, :white, :blink
126
+
127
+ puts <<EOM
128
+ Lorem ipsum dolor sit amet,
129
+ consectetur adipisicing elit,
130
+ sed do eiusmod tempor incididunt
131
+ ut labore et dolore magna aliqua.
132
+ EOM
103
133
  </pre>
104
134
 
105
135
 
data/lib/color_echo.rb CHANGED
@@ -4,9 +4,10 @@
4
4
  require "stringio"
5
5
 
6
6
  module CE
7
- VERSION = "0.0.1"
7
+ VERSION = "0.1.0"
8
8
  CODE_RESET = "\e[0m"
9
9
 
10
+ @@use = true
10
11
  @@enable = false
11
12
  @@code_bg_color = ""
12
13
  @@code_fg_color = ""
@@ -44,8 +45,8 @@ module CE
44
45
  end
45
46
 
46
47
  module_function
47
- def enable
48
- return @@enable
48
+ def enable?
49
+ return @@enable && @@use
49
50
  end
50
51
 
51
52
  def disable
@@ -55,21 +56,42 @@ module CE
55
56
  @@code_text_attr = ""
56
57
  end
57
58
 
59
+ def off
60
+ disable
61
+ end
62
+
63
+ def reset
64
+ disable
65
+ end
66
+
67
+ def not_use
68
+ @@use = false
69
+ end
70
+
58
71
  def ch_fg(name)
72
+ return nil if !name.instance_of?(Symbol)
59
73
  @@enable = true if !@@enable
60
74
  @@code_fg_color = get_code("ForeGround", name)
61
75
  end
62
76
 
63
77
  def ch_bg(name)
78
+ return nil if !name.instance_of?(Symbol)
64
79
  @@enable = true if !@@enable
65
80
  @@code_bg_color = get_code("BackGround", name)
66
81
  end
67
82
 
68
83
  def ch_tx(name)
84
+ return nil if !name.instance_of?(Symbol)
69
85
  @@enable = true if !@@enable
70
86
  @@code_text_attr = get_code("TextAttr", name)
71
87
  end
72
88
 
89
+ def ch(fg, bg=nil, tx=nil)
90
+ ch_fg(fg)
91
+ ch_bg(bg)
92
+ ch_tx(tx)
93
+ end
94
+
73
95
  def get_color_code
74
96
  return @@code_fg_color + @@code_bg_color + @@code_text_attr
75
97
  end
@@ -89,18 +111,33 @@ module CE
89
111
  end
90
112
 
91
113
  def print(*arg)
92
- if CE::enable
114
+ if CE::enable?
115
+ # change output destination to StringIO Object
116
+ strio = StringIO.new
117
+ $stdout = strio
118
+
119
+ # output color sequence
93
120
  $stdout.print(CE::get_color_code)
121
+
94
122
  super
95
- $stdout.print(CE::get_reset_code)
96
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)
97
134
  else
98
135
  super
99
136
  end
100
137
  end
101
138
 
102
139
  def p(*arg)
103
- if CE::enable
140
+ if CE::enable?
104
141
  # change output destination to StringIO Object
105
142
  strio = StringIO.new
106
143
  $stdout = strio
@@ -113,15 +150,10 @@ def p(*arg)
113
150
  # change output destination to STDOUT
114
151
  $stdout = STDOUT
115
152
 
116
- # add reset sequence
117
- if (/#{$/}$/ =~ strio.string)
118
- output = strio.string.chomp
119
- output.gsub!(/#{$/}/, CE::get_reset_code + $/ + CE::get_color_code)
120
- output += CE::get_reset_code + $/
121
- else
122
- output.gsub!(/#{$/}/, CE::get_reset_code + $/ + CE::get_color_code)
123
- output += $/
124
- end
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
125
157
 
126
158
  # output to STDOUT
127
159
  $stdout.print(output)
@@ -131,7 +163,7 @@ def p(*arg)
131
163
  end
132
164
 
133
165
  def puts(*arg)
134
- if CE::enable
166
+ if CE::enable?
135
167
  # change output destination to StringIO Object
136
168
  strio = StringIO.new
137
169
  $stdout = strio
@@ -144,15 +176,10 @@ def puts(*arg)
144
176
  # change output destination to STDOUT
145
177
  $stdout = STDOUT
146
178
 
147
- # add reset sequence
148
- if (/#{$/}$/ =~ strio.string)
149
- output = strio.string.chomp
150
- output.gsub!(/#{$/}/, CE::get_reset_code + $/ + CE::get_color_code)
151
- output += CE::get_reset_code + $/
152
- else
153
- output.gsub!(/#{$/}/, CE::get_reset_code + $/ + CE::get_color_code)
154
- output += $/
155
- end
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
156
183
 
157
184
  # output to STDOUT
158
185
  $stdout.print(output)
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.0.1
4
+ version: 0.1.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-11 00:00:00.000000000 Z
11
+ date: 2014-11-14 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: To add color to the command line output.
14
14
  email: