color_echo 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (5) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE.txt +22 -0
  3. data/README.md +105 -0
  4. data/lib/color_echo.rb +162 -0
  5. metadata +48 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 4fb008ce89e82f2f08c590083abfac3efdd76048
4
+ data.tar.gz: 788ae07cbba6fb72fb69e22ed46a61f67d34a793
5
+ SHA512:
6
+ metadata.gz: 8f3ea2c4d3424c360024814d89f917e39f4455eb24e978305b6140c7582f9f85163e2706de3c55cc15b5f99bc66e61a795bb041200891b3db9b3422053ee480f
7
+ data.tar.gz: 07ae5482c36aa0db7b477daa36ca4f219f0da82bddc57876aa175d4e37129e4b1d9be1043649b757b15ae7ceeea3ff11f51b7315e8db7498e89031f56f654a0e
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Kazuya Hotta
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,105 @@
1
+ # ColorEcho
2
+ To add color to the command line output.
3
+ This Library will extend the Kernel module's functions(#print, #puts, #p).
4
+ required StringIO.
5
+
6
+ Version: 0.0.1
7
+ Compliant Rubys Version: 1.9.3, 2.0.0, 2.1.0 (for Linux)
8
+ License: MIT
9
+ Gems repository: http://rubygems.org/gems/color_echo
10
+
11
+ ## Installation
12
+
13
+ Add this line to your application's Gemfile:
14
+
15
+ gem 'color_echo'
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install color_echo
20
+
21
+ ## Usage
22
+
23
+ ### module functions
24
+ #### CE::ch_fg :symbol
25
+ Change the foreground color to the your specified color.
26
+
27
+ * symbol list:
28
+ * black
29
+ * red
30
+ * green
31
+ * yellow
32
+ * blue
33
+ * magenta
34
+ * cyan
35
+ * white
36
+
37
+ ex.) CE::ch_fg :red #=> foreground color will be changed red
38
+
39
+
40
+ #### CE::ch_bg::symbol
41
+ Change the background color to the your specified color.
42
+
43
+ * symbol list:
44
+ * black
45
+ * red
46
+ * green
47
+ * yellow
48
+ * blue
49
+ * magenta
50
+ * cyan
51
+ * white
52
+
53
+ ex.) CE::ch_bg :white #=> background color will be changed white
54
+
55
+ #### CE::ch_tx :symbol
56
+ Change the text attribute to the your specified decoration.
57
+
58
+ * symbol list:
59
+ * bold
60
+ * underscore
61
+ * blink
62
+ * reverse_video
63
+ * concealed
64
+
65
+ ex.) CE::ch_tx :blink #=> text blink on
66
+
67
+ #### CE::disable
68
+ Turn off the function of this library.
69
+
70
+ ### Example
71
+ <pre>
72
+ #
73
+ # Example Code
74
+ #
75
+ # change the foreground color to 'yellow'
76
+ CE::ch_fg :yellow
77
+
78
+ puts "fooooooooo"
79
+ puts "baaaaaaaar"
80
+
81
+ # change the foreground color to 'white'
82
+ CE::ch_fg :white
83
+ # change the foreground color to 'red'
84
+ CE::ch_bg :red
85
+
86
+ print "testtesttest"
87
+
88
+ # change the foreground color to 'black'
89
+ CE::ch_fg :black
90
+ # change the foreground color to 'cyan'
91
+ CE::ch_bg :cyan
92
+ # change the text attribute 'underscore'
93
+ CE::ch_tx :underscore
94
+
95
+ ary = ["aaa", "bbb", "ccc"]
96
+ p ary
97
+ print ary
98
+
99
+ # reset the color sequence
100
+ CE::disable
101
+
102
+ puts "hogehoge"
103
+ </pre>
104
+
105
+
data/lib/color_echo.rb ADDED
@@ -0,0 +1,162 @@
1
+ # ColorEcho - handle the ANSI color sequences.
2
+ # @autor: Kazuya Hotta (nyanko)
3
+ #
4
+ require "stringio"
5
+
6
+ module CE
7
+ VERSION = "0.0.1"
8
+ CODE_RESET = "\e[0m"
9
+
10
+ @@enable = false
11
+ @@code_bg_color = ""
12
+ @@code_fg_color = ""
13
+ @@code_text_attr = ""
14
+
15
+ module ForeGround
16
+ BLACK = 30
17
+ RED = 31
18
+ GREEN = 32
19
+ YELLOW = 33
20
+ BLUE = 34
21
+ MAGENTA = 35
22
+ CYAN = 36
23
+ WHITE = 37
24
+ end
25
+
26
+ module BackGround
27
+ BLACK = 40
28
+ RED = 41
29
+ GREEN = 42
30
+ YELLOW = 43
31
+ BLUE = 44
32
+ MAGENTA = 45
33
+ CYAN = 46
34
+ WHITE = 47
35
+ end
36
+
37
+ module TextAttr
38
+ OFF = 0
39
+ BOLD = 1
40
+ UNDERSCORE = 4
41
+ BLINK = 5
42
+ REVERSE_VIDEO = 7
43
+ CONCEALED = 8
44
+ end
45
+
46
+ module_function
47
+ def enable
48
+ return @@enable
49
+ end
50
+
51
+ def disable
52
+ @@enable = false
53
+ @@code_bg_color = ""
54
+ @@code_fg_color = ""
55
+ @@code_text_attr = ""
56
+ end
57
+
58
+ def ch_fg(name)
59
+ @@enable = true if !@@enable
60
+ @@code_fg_color = get_code("ForeGround", name)
61
+ end
62
+
63
+ def ch_bg(name)
64
+ @@enable = true if !@@enable
65
+ @@code_bg_color = get_code("BackGround", name)
66
+ end
67
+
68
+ def ch_tx(name)
69
+ @@enable = true if !@@enable
70
+ @@code_text_attr = get_code("TextAttr", name)
71
+ end
72
+
73
+ def get_color_code
74
+ return @@code_fg_color + @@code_bg_color + @@code_text_attr
75
+ end
76
+
77
+ def get_reset_code
78
+ return self::CODE_RESET
79
+ end
80
+
81
+ def get_code(module_name, name)
82
+ begin
83
+ code = eval(%{#{module_name}::#{name.to_s.swapcase!}})
84
+ rescue NameError
85
+ raise(NameError ,%{:#{name} is not defined! Please check reference.})
86
+ end
87
+ return "\e[#{code}m"
88
+ end
89
+ end
90
+
91
+ def print(*arg)
92
+ if CE::enable
93
+ $stdout.print(CE::get_color_code)
94
+ super
95
+ $stdout.print(CE::get_reset_code)
96
+
97
+ else
98
+ super
99
+ end
100
+ end
101
+
102
+ def p(*arg)
103
+ if CE::enable
104
+ # change output destination to StringIO Object
105
+ strio = StringIO.new
106
+ $stdout = strio
107
+
108
+ # output color sequence
109
+ $stdout.print(CE::get_color_code)
110
+
111
+ super
112
+
113
+ # change output destination to STDOUT
114
+ $stdout = STDOUT
115
+
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
125
+
126
+ # output to STDOUT
127
+ $stdout.print(output)
128
+ else
129
+ super
130
+ end
131
+ end
132
+
133
+ def puts(*arg)
134
+ if CE::enable
135
+ # change output destination to StringIO Object
136
+ strio = StringIO.new
137
+ $stdout = strio
138
+
139
+ # output color sequence
140
+ $stdout.print(CE::get_color_code)
141
+
142
+ super
143
+
144
+ # change output destination to STDOUT
145
+ $stdout = STDOUT
146
+
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
156
+
157
+ # output to STDOUT
158
+ $stdout.print(output)
159
+ else
160
+ super
161
+ end
162
+ end
metadata ADDED
@@ -0,0 +1,48 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: color_echo
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - nyanko
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-11-11 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: To add color to the command line output.
14
+ email:
15
+ - nyanko@dammy
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - LICENSE.txt
21
+ - README.md
22
+ - lib/color_echo.rb
23
+ homepage: https://github.com/khotta/color_echo
24
+ licenses:
25
+ - MIT
26
+ metadata: {}
27
+ post_install_message: "\n\e[5mThank you for installing! (^-^)\e[0m\nSee also \e[31m\e[47m
28
+ \e[4mhttps://github.com/khotta/color_echo \e[0m\n\n"
29
+ rdoc_options: []
30
+ require_paths:
31
+ - lib
32
+ required_ruby_version: !ruby/object:Gem::Requirement
33
+ requirements:
34
+ - - ">="
35
+ - !ruby/object:Gem::Version
36
+ version: '0'
37
+ required_rubygems_version: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ requirements: []
43
+ rubyforge_project:
44
+ rubygems_version: 2.2.2
45
+ signing_key:
46
+ specification_version: 4
47
+ summary: To add color to the command line output.
48
+ test_files: []