cli-colorize 1.1.1 → 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/cli-colorize.rb +46 -15
- metadata +2 -2
data/lib/cli-colorize.rb
CHANGED
@@ -83,7 +83,6 @@ module CLIColorize
|
|
83
83
|
"#{CTRLSTR_START+control_string+CTRLSTR_DELIM}#{text}#{CTRLSTR_END}"
|
84
84
|
end
|
85
85
|
end
|
86
|
-
|
87
86
|
# Instance method that exposes the class method colorize to classes that `include 'CLIColorize'`
|
88
87
|
def colorize(text, color=nil)
|
89
88
|
CLIColorize.colorize(text, color)
|
@@ -93,19 +92,25 @@ module CLIColorize
|
|
93
92
|
def CLIColorize.puts(text, color=nil)
|
94
93
|
STDOUT.puts CLIColorize.colorize(text, color)
|
95
94
|
end
|
96
|
-
|
95
|
+
|
97
96
|
# Call STDOUT.print with the colorized text.
|
98
97
|
def CLIColorize.print(text, color=nil)
|
99
98
|
STDOUT.print CLIColorize.colorize(text, color)
|
100
99
|
end
|
100
|
+
|
101
|
+
def CLIColorize.colorize_if_tty(text, color=nil)
|
102
|
+
STDOUT.isatty ? CLIColorize.colorize(text, color) : text
|
103
|
+
end
|
104
|
+
def colorize_if_tty(text, color=nil)
|
105
|
+
CLIColorize.colorize_if_tty(text, color)
|
106
|
+
end
|
101
107
|
|
102
108
|
# Call STDOUT.puts with the colorized text if STDOUT is a tty device.
|
103
109
|
# (If STDOUT has been redirected to a file, it will be a block device,
|
104
110
|
# not a tty device, and we wouldn't want the ANSI codes inserted.
|
105
111
|
def CLIColorize.puts_colorized_if_tty(text, color=nil)
|
106
|
-
STDOUT.puts(
|
112
|
+
STDOUT.puts(CLIColorize.colorize_if_tty(text, color))
|
107
113
|
end
|
108
|
-
|
109
114
|
# instance method delegating to class method, see CLIColorize.puts_if_tty
|
110
115
|
def puts_colorized_if_tty(text, color=nil)
|
111
116
|
CLIColorize.puts_colorized_if_tty(text, color)
|
@@ -115,15 +120,13 @@ module CLIColorize
|
|
115
120
|
# (If STDOUT has been redirected to a file, it will be a block device,
|
116
121
|
# not a tty device, and we wouldn't want the ANSI codes inserted.
|
117
122
|
def CLIColorize.print_colorized_if_tty(text, color=nil)
|
118
|
-
STDOUT.print(
|
123
|
+
STDOUT.print(CLIColorize.colorize_if_tty(text, color))
|
119
124
|
end
|
120
|
-
|
121
125
|
# instance method delegating to class method, see CLIColorize.print_if_tty
|
122
126
|
def print_colorized_if_tty(text, color=nil)
|
123
127
|
CLIColorize.print_colorized_if_tty(text, color)
|
124
128
|
end
|
125
129
|
|
126
|
-
|
127
130
|
# Use safe_colorize in conjunction with CLIColorize.off and CLIColorize.on to conditionally
|
128
131
|
# determine whether or not output will be given the control characters for colorization.
|
129
132
|
# This is designed to work with a command-line switch to the script that uses this module.
|
@@ -131,7 +134,6 @@ module CLIColorize
|
|
131
134
|
return text if @@off
|
132
135
|
colorize(text, color)
|
133
136
|
end
|
134
|
-
|
135
137
|
# Instance method that exposes the class method safe_colorize to classes that `include 'CLIColorize'`
|
136
138
|
def safe_colorize(text, color=nil)
|
137
139
|
CLIColorize.safe_colorize(text, color)
|
@@ -153,27 +155,26 @@ module CLIColorize
|
|
153
155
|
def CLIColorize.safe_puts(text, color=nil)
|
154
156
|
STDOUT.puts CLIColorize.safe_colorize(text, color)
|
155
157
|
end
|
156
|
-
|
157
|
-
# Call STDOUT.print with the colorized text.
|
158
|
-
def CLIColorize.safe_print(text, color=nil)
|
159
|
-
STDOUT.print CLIColorize.safe_colorize(text, color)
|
160
|
-
end
|
161
|
-
|
162
158
|
# Instance method that exposes the class method safe_puts to classes that `include 'CLIColorize'`
|
163
159
|
def safe_puts(text, color=nil)
|
164
160
|
CLIColorize.safe_puts(text, color)
|
165
161
|
end
|
166
162
|
|
163
|
+
# Call STDOUT.print with the colorized text.
|
164
|
+
def CLIColorize.safe_print(text, color=nil)
|
165
|
+
STDOUT.print CLIColorize.safe_colorize(text, color)
|
166
|
+
end
|
167
167
|
# Instance method that exposes the class method safe_puts to classes that `include 'CLIColorize'`
|
168
168
|
def safe_print(text, color=nil)
|
169
169
|
CLIColorize.safe_print(text, color)
|
170
170
|
end
|
171
171
|
|
172
172
|
def CLIColorize.default_color; @@default_color; end
|
173
|
+
def default_color; CLIColorize.default_color; end
|
174
|
+
|
173
175
|
def CLIColorize.default_color=(color)
|
174
176
|
@@default_color = color.to_sym
|
175
177
|
end
|
176
|
-
def default_color; CLIColorize.default_color; end
|
177
178
|
def default_color=(color)
|
178
179
|
CLIColorize.default_color=(color)
|
179
180
|
end
|
@@ -189,3 +190,33 @@ module CLIColorize
|
|
189
190
|
def CLIColorize.on; @@off = false; nil; end
|
190
191
|
|
191
192
|
end
|
193
|
+
|
194
|
+
# Monkey monkey! I got a monkey, his name is Patches!
|
195
|
+
# __
|
196
|
+
# w c(..)o (
|
197
|
+
# \__(-) __)
|
198
|
+
# /\ (
|
199
|
+
# /(_)___)
|
200
|
+
# w /|
|
201
|
+
# | \
|
202
|
+
#ejm97 m m
|
203
|
+
#
|
204
|
+
class String
|
205
|
+
CLIColorize::CONFIG.keys.each do |action|
|
206
|
+
define_method action do
|
207
|
+
return CLIColorize.colorize_if_tty(self, {:config => action})
|
208
|
+
end
|
209
|
+
end
|
210
|
+
|
211
|
+
CLIColorize::SET_FORE.keys.each do |foreground|
|
212
|
+
define_method foreground do
|
213
|
+
return CLIColorize.colorize_if_tty(self, {:foreground => foreground})
|
214
|
+
end
|
215
|
+
end
|
216
|
+
|
217
|
+
CLIColorize::SET_BACK.keys.each do |background|
|
218
|
+
define_method "bg_#{background.to_s}".to_sym do
|
219
|
+
return CLIColorize.colorize_if_tty(self, {:background => background})
|
220
|
+
end
|
221
|
+
end
|
222
|
+
end
|
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: cli-colorize
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version:
|
5
|
+
version: 2.0.0
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Chris St. John
|
@@ -10,7 +10,7 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date: 2011-
|
13
|
+
date: 2011-07-24 00:00:00 -04:00
|
14
14
|
default_executable:
|
15
15
|
dependencies: []
|
16
16
|
|