ohboyohboyohboy-say 1.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/Gemfile +2 -0
- data/History.txt +6 -0
- data/README.txt +50 -0
- data/Rakefile +13 -0
- data/bin/say +71 -0
- data/lib/say.rb +380 -0
- data/lib/say/import.rb +15 -0
- data/test/test_say.rb +8 -0
- metadata +85 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 5202ec89d7cb2b153d5dfabc079237d69e4546bb
|
4
|
+
data.tar.gz: 54ea033d409160d822e11246af7aa964c0566db1
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: a978bbd6a84223d40561b3f06676916a187976c83d4b272c8bfc3279dbad4571523debbcc1a4ca234fc54398e231d18a775cac4e9f72448546a5e4f83793e013
|
7
|
+
data.tar.gz: e56fecc49f745e928b77e599a0323ce02b9f27a1cbe3e4dfac20f54d04bb21ed49e323d96285b064a226a17b8831522a33322a076266d8d8917ebb7c17494458
|
data/Gemfile
ADDED
data/History.txt
ADDED
data/README.txt
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
= ohboyohboyohboy-say
|
2
|
+
|
3
|
+
* http://gems.ohboyohboyohboy.org/ohboyohboyohboy-say
|
4
|
+
|
5
|
+
== DESCRIPTION:
|
6
|
+
|
7
|
+
Add ANSI color codes to strings using a simple markup.
|
8
|
+
|
9
|
+
== SYNOPSIS:
|
10
|
+
|
11
|
+
say '<red>ERROR:</red> %(_)s' $message
|
12
|
+
|
13
|
+
|
14
|
+
== INSTALL:
|
15
|
+
|
16
|
+
sudo gem install ohboyohboyohboy-say
|
17
|
+
|
18
|
+
== DEVELOPERS:
|
19
|
+
|
20
|
+
After checking out the source, run:
|
21
|
+
|
22
|
+
$ rake newb
|
23
|
+
|
24
|
+
This task will install any missing dependencies, run the tests/specs,
|
25
|
+
and generate the RDoc.
|
26
|
+
|
27
|
+
== LICENSE:
|
28
|
+
|
29
|
+
(The MIT License)
|
30
|
+
|
31
|
+
Copyright (c) 2013-2018
|
32
|
+
|
33
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
34
|
+
a copy of this software and associated documentation files (the
|
35
|
+
'Software'), to deal in the Software without restriction, including
|
36
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
37
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
38
|
+
permit persons to whom the Software is furnished to do so, subject to
|
39
|
+
the following conditions:
|
40
|
+
|
41
|
+
The above copyright notice and this permission notice shall be
|
42
|
+
included in all copies or substantial portions of the Software.
|
43
|
+
|
44
|
+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
45
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
46
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
47
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
48
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
49
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
50
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Rakefile
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
#!/usr/bin/ruby
|
2
|
+
# encoding: utf-8
|
3
|
+
#
|
4
|
+
|
5
|
+
PROJECT_NAME = "ohboyohboyohboy-say"
|
6
|
+
|
7
|
+
require "rubygems"
|
8
|
+
require "hoe"
|
9
|
+
|
10
|
+
Hoe.spec PROJECT_NAME do
|
11
|
+
developer( "Kyle Yetter", "kyle@ohboyohboyohboy.org")
|
12
|
+
license "MIT" # this should match the license in the README
|
13
|
+
end
|
data/bin/say
ADDED
@@ -0,0 +1,71 @@
|
|
1
|
+
#!/usr/bin/ruby
|
2
|
+
# encoding: utf-8
|
3
|
+
#
|
4
|
+
# author: Kyle Yetter
|
5
|
+
#
|
6
|
+
|
7
|
+
lib_dir = File.expand_path( "../../lib", __FILE__ )
|
8
|
+
$LOAD_PATH.unshift( lib_dir ) if test( ?d, lib_dir )
|
9
|
+
|
10
|
+
require 'optparse'
|
11
|
+
require 'ostruct'
|
12
|
+
require 'say/import'
|
13
|
+
|
14
|
+
$options =
|
15
|
+
OpenStruct.new(
|
16
|
+
:add_newline => true
|
17
|
+
)
|
18
|
+
|
19
|
+
OptionParser.new do |o|
|
20
|
+
program = File.basename( $0 )
|
21
|
+
|
22
|
+
o.set_program_name program
|
23
|
+
o.release = Say::VERSION
|
24
|
+
|
25
|
+
|
26
|
+
o.set_banner( <<-END.gsub( /^\s*\| ?/, '' ).chomp )
|
27
|
+
| #{ program }: print a printf-style formatted string with ansi color code markup
|
28
|
+
|
|
29
|
+
| USAGE:
|
30
|
+
| #{ program } [opts] format_string [args ...]
|
31
|
+
END
|
32
|
+
|
33
|
+
o.separator ""
|
34
|
+
o.separator "OPTIONS:"
|
35
|
+
|
36
|
+
o.on( "-n", "--no-newline", "Do not print a newline after printing the arguments" ) do
|
37
|
+
$options.add_newline = false
|
38
|
+
end
|
39
|
+
|
40
|
+
o.on( "-v", "--version", "Print version number and exit" ) do
|
41
|
+
puts Say::VERSION
|
42
|
+
exit
|
43
|
+
end
|
44
|
+
|
45
|
+
o.on( "-h", "--help", "Print help for this command" ) do
|
46
|
+
puts o
|
47
|
+
puts( <<-END.gsub( /^\s*\| ?/, '' ).chomp )
|
48
|
+
| ANSI MARK UP EXAMPLES:
|
49
|
+
| styles: <_>underline</_> <~>invert</~> <!>blink</!> <*>bold</*>
|
50
|
+
| colors: <fg_color></fg_color> <@bg_color></@bg_color> <fg@bg></fg@bg>
|
51
|
+
END
|
52
|
+
exit
|
53
|
+
end
|
54
|
+
|
55
|
+
o.separator ""
|
56
|
+
|
57
|
+
o.parse!
|
58
|
+
end
|
59
|
+
|
60
|
+
################################################################################
|
61
|
+
##################################### Main #####################################
|
62
|
+
################################################################################
|
63
|
+
|
64
|
+
format = ARGV.shift.to_s.dup
|
65
|
+
|
66
|
+
if $options.add_newline
|
67
|
+
say( format, *ARGV )
|
68
|
+
else
|
69
|
+
say_print( format, *ARGV )
|
70
|
+
end
|
71
|
+
|
data/lib/say.rb
ADDED
@@ -0,0 +1,380 @@
|
|
1
|
+
#!/usr/bin/ruby
|
2
|
+
# encoding: utf-8
|
3
|
+
#
|
4
|
+
# author: Kyle Yetter
|
5
|
+
#
|
6
|
+
|
7
|
+
module Say
|
8
|
+
VERSION = "1.2.0"
|
9
|
+
FormatError = Class.new( StandardError )
|
10
|
+
|
11
|
+
module XTerm256Colors
|
12
|
+
############################################################################
|
13
|
+
######################### XTERM 256 COLOR CONSTANTS ########################
|
14
|
+
############################################################################
|
15
|
+
X256_COLOR_TABLE =
|
16
|
+
[
|
17
|
+
0x000000, 0x00005f, 0x000087, 0x0000af, 0x0000d7, 0x0000ff,
|
18
|
+
0x005f00, 0x005f5f, 0x005f87, 0x005faf, 0x005fd7, 0x005fff,
|
19
|
+
0x008700, 0x00875f, 0x008787, 0x0087af, 0x0087d7, 0x0087ff,
|
20
|
+
0x00af00, 0x00af5f, 0x00af87, 0x00afaf, 0x00afd7, 0x00afff,
|
21
|
+
0x00d700, 0x00d75f, 0x00d787, 0x00d7af, 0x00d7d7, 0x00d7ff,
|
22
|
+
0x00ff00, 0x00ff5f, 0x00ff87, 0x00ffaf, 0x00ffd7, 0x00ffff,
|
23
|
+
0x5f0000, 0x5f005f, 0x5f0087, 0x5f00af, 0x5f00d7, 0x5f00ff,
|
24
|
+
0x5f5f00, 0x5f5f5f, 0x5f5f87, 0x5f5faf, 0x5f5fd7, 0x5f5fff,
|
25
|
+
0x5f8700, 0x5f875f, 0x5f8787, 0x5f87af, 0x5f87d7, 0x5f87ff,
|
26
|
+
0x5faf00, 0x5faf5f, 0x5faf87, 0x5fafaf, 0x5fafd7, 0x5fafff,
|
27
|
+
0x5fd700, 0x5fd75f, 0x5fd787, 0x5fd7af, 0x5fd7d7, 0x5fd7ff,
|
28
|
+
0x5fff00, 0x5fff5f, 0x5fff87, 0x5fffaf, 0x5fffd7, 0x5fffff,
|
29
|
+
0x870000, 0x87005f, 0x870087, 0x8700af, 0x8700d7, 0x8700ff,
|
30
|
+
0x875f00, 0x875f5f, 0x875f87, 0x875faf, 0x875fd7, 0x875fff,
|
31
|
+
0x878700, 0x87875f, 0x878787, 0x8787af, 0x8787d7, 0x8787ff,
|
32
|
+
0x87af00, 0x87af5f, 0x87af87, 0x87afaf, 0x87afd7, 0x87afff,
|
33
|
+
0x87d700, 0x87d75f, 0x87d787, 0x87d7af, 0x87d7d7, 0x87d7ff,
|
34
|
+
0x87ff00, 0x87ff5f, 0x87ff87, 0x87ffaf, 0x87ffd7, 0x87ffff,
|
35
|
+
0xaf0000, 0xaf005f, 0xaf0087, 0xaf00af, 0xaf00d7, 0xaf00ff,
|
36
|
+
0xaf5f00, 0xaf5f5f, 0xaf5f87, 0xaf5faf, 0xaf5fd7, 0xaf5fff,
|
37
|
+
0xaf8700, 0xaf875f, 0xaf8787, 0xaf87af, 0xaf87d7, 0xaf87ff,
|
38
|
+
0xafaf00, 0xafaf5f, 0xafaf87, 0xafafaf, 0xafafd7, 0xafafff,
|
39
|
+
0xafd700, 0xafd75f, 0xafd787, 0xafd7af, 0xafd7d7, 0xafd7ff,
|
40
|
+
0xafff00, 0xafff5f, 0xafff87, 0xafffaf, 0xafffd7, 0xafffff,
|
41
|
+
0xd70000, 0xd7005f, 0xd70087, 0xd700af, 0xd700d7, 0xd700ff,
|
42
|
+
0xd75f00, 0xd75f5f, 0xd75f87, 0xd75faf, 0xd75fd7, 0xd75fff,
|
43
|
+
0xd78700, 0xd7875f, 0xd78787, 0xd787af, 0xd787d7, 0xd787ff,
|
44
|
+
0xd7af00, 0xd7af5f, 0xd7af87, 0xd7afaf, 0xd7afd7, 0xd7afff,
|
45
|
+
0xd7d700, 0xd7d75f, 0xd7d787, 0xd7d7af, 0xd7d7d7, 0xd7d7ff,
|
46
|
+
0xd7ff00, 0xd7ff5f, 0xd7ff87, 0xd7ffaf, 0xd7ffd7, 0xd7ffff,
|
47
|
+
0xff0000, 0xff005f, 0xff0087, 0xff00af, 0xff00d7, 0xff00ff,
|
48
|
+
0xff5f00, 0xff5f5f, 0xff5f87, 0xff5faf, 0xff5fd7, 0xff5fff,
|
49
|
+
0xff8700, 0xff875f, 0xff8787, 0xff87af, 0xff87d7, 0xff87ff,
|
50
|
+
0xffaf00, 0xffaf5f, 0xffaf87, 0xffafaf, 0xffafd7, 0xffafff,
|
51
|
+
0xffd700, 0xffd75f, 0xffd787, 0xffd7af, 0xffd7d7, 0xffd7ff,
|
52
|
+
0xffff00, 0xffff5f, 0xffff87, 0xffffaf, 0xffffd7, 0xffffff,
|
53
|
+
0x080808, 0x121212, 0x1c1c1c, 0x262626, 0x303030, 0x3a3a3a,
|
54
|
+
0x444444, 0x4e4e4e, 0x585858, 0x626262, 0x6c6c6c, 0x767676,
|
55
|
+
0x808080, 0x8a8a8a, 0x949494, 0x9e9e9e, 0xa8a8a8, 0xb2b2b2,
|
56
|
+
0xbcbcbc, 0xc6c6c6, 0xd0d0d0, 0xdadada, 0xe4e4e4, 0xeeeeee
|
57
|
+
].freeze
|
58
|
+
|
59
|
+
X256_FG_MASK = "\e[38;5;%im".freeze
|
60
|
+
X256_BG_MASK = "\e[48;5;%im".freeze
|
61
|
+
X256_RX_RGB_HEX_3 = %r<\A \s* 0? x? (\h) (\h) (\h) \s* \z>ix
|
62
|
+
X256_RX_RGB_HEX_6 = %r<\A \s* 0? x? (\h{2}) (\h{2}) (\h{2}) \s* \z>ix
|
63
|
+
|
64
|
+
class << self
|
65
|
+
def xterm256_color_index(color)
|
66
|
+
xterm_256_cache.fetch(color) do
|
67
|
+
xterm_256_cache[color] = compute_xterm256_color_index(color)
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
def xterm256_fg(color)
|
72
|
+
sprintf(X256_FG_MASK, xterm256_color_index(color))
|
73
|
+
end
|
74
|
+
|
75
|
+
def xterm256_bg(color)
|
76
|
+
sprintf(X256_BG_MASK, xterm256_color_index(color))
|
77
|
+
end
|
78
|
+
|
79
|
+
private
|
80
|
+
|
81
|
+
def xterm_256_cache
|
82
|
+
@xterm_256_cache ||= {}
|
83
|
+
end
|
84
|
+
|
85
|
+
def xterm_256_rgb_vectors
|
86
|
+
@xterm_256_rgb_vectors ||= X256_COLOR_TABLE.map { |int| integer_to_rgb(int) }.freeze
|
87
|
+
end
|
88
|
+
|
89
|
+
def compute_xterm256_color_index(color)
|
90
|
+
color_integer = normalize_rgb_integer(color)
|
91
|
+
target_rgb = integer_to_rgb(color_integer)
|
92
|
+
|
93
|
+
best_distance = 0XFFFFFF
|
94
|
+
best_index = -1
|
95
|
+
|
96
|
+
xterm_256_rgb_vectors.each_with_index do |rgb, index|
|
97
|
+
metric = rgb_vector_distance_squared(target_rgb, rgb)
|
98
|
+
if metric < best_distance
|
99
|
+
best_distance = metric
|
100
|
+
best_index = index
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
return best_index + 16
|
105
|
+
end
|
106
|
+
|
107
|
+
def normalize_rgb_integer(value)
|
108
|
+
integer_value =
|
109
|
+
case value
|
110
|
+
when Array then rgb_to_integer(*value)
|
111
|
+
when String then parse_rgb_string(value)
|
112
|
+
else value.to_i
|
113
|
+
end
|
114
|
+
|
115
|
+
integer_value & 0xFFFFFF
|
116
|
+
end
|
117
|
+
|
118
|
+
def parse_rgb_string(string)
|
119
|
+
if string =~ X256_RX_RGB_HEX_3
|
120
|
+
string = "#{ $1 }#{ $1 }#{ $2 }#{ $2 }#{ $3 }#{ $3 }"
|
121
|
+
end
|
122
|
+
|
123
|
+
value =
|
124
|
+
if string =~ X256_RX_RGB_HEX_6
|
125
|
+
rgb_to_integer($1.to_i(16), $2.to_i(16), $3.to_i(16))
|
126
|
+
else
|
127
|
+
fail ArgumentError, "Invalid RGB color string value: #{ string.inspect }"
|
128
|
+
end
|
129
|
+
|
130
|
+
return value
|
131
|
+
end
|
132
|
+
|
133
|
+
def rgb_to_integer(r = 0, g = 0, b = 0)
|
134
|
+
((r.to_i & 0xFF) << 16) | ((g.to_i & 0xFF) << 8) | (b.to_i & 0xFF)
|
135
|
+
end
|
136
|
+
|
137
|
+
def integer_to_rgb(integer)
|
138
|
+
[
|
139
|
+
(integer >> 16) & 0xFF,
|
140
|
+
(integer >> 8) & 0xFF,
|
141
|
+
integer & 0xFF
|
142
|
+
].freeze
|
143
|
+
end
|
144
|
+
|
145
|
+
def rgb_vector_distance_squared(rgb_1, rgb_2)
|
146
|
+
r1, g1, b1 = rgb_1
|
147
|
+
r2, g2, b2 = rgb_2
|
148
|
+
|
149
|
+
return (
|
150
|
+
(r1 - r2) ** 2 +
|
151
|
+
(b1 - b2) ** 2 +
|
152
|
+
(g1 - g2) ** 2
|
153
|
+
)
|
154
|
+
end
|
155
|
+
end
|
156
|
+
|
157
|
+
%i( xterm256_color_index xterm256_fg xterm256_bg ).each do |method_name|
|
158
|
+
define_method(method_name) do |color_value|
|
159
|
+
XTerm256Colors.send(method_name, color_value)
|
160
|
+
end
|
161
|
+
|
162
|
+
private method_name
|
163
|
+
end
|
164
|
+
end
|
165
|
+
|
166
|
+
extend XTerm256Colors
|
167
|
+
include XTerm256Colors
|
168
|
+
|
169
|
+
############################################################################
|
170
|
+
##################### Basic ANSI Color Escape Constants ####################
|
171
|
+
############################################################################
|
172
|
+
|
173
|
+
ESCAPE_MAP =
|
174
|
+
Hash.new { | h, k | k }.
|
175
|
+
update(
|
176
|
+
'n' => "\n",
|
177
|
+
't' => "\t",
|
178
|
+
'r' => "\r",
|
179
|
+
'e' => "\e",
|
180
|
+
'\\' => "\\",
|
181
|
+
'a' => "\a",
|
182
|
+
"s" => "\s",
|
183
|
+
'b' => "\b",
|
184
|
+
'%' => '%%'
|
185
|
+
)
|
186
|
+
|
187
|
+
BACKGROUND_CODE = "\e[4%im"
|
188
|
+
FOREGROUND_CODE = "\e[3%im"
|
189
|
+
COLOR_VALUES =
|
190
|
+
{
|
191
|
+
"white" => 7,
|
192
|
+
"black" => 0,
|
193
|
+
"cyan" => 6,
|
194
|
+
"blue" => 4,
|
195
|
+
"green" => 2,
|
196
|
+
"red" => 1,
|
197
|
+
"magenta" => 5,
|
198
|
+
"yellow" => 3
|
199
|
+
}
|
200
|
+
|
201
|
+
COLOR_NAMES =
|
202
|
+
{
|
203
|
+
"bla" => "black", "gree" => "green", "w" => "white",
|
204
|
+
"blac" => "black", "green" => "green", "wh" => "white",
|
205
|
+
"black" => "black", "m" => "magenta", "whi" => "white",
|
206
|
+
"blu" => "blue", "ma" => "magenta", "whit" => "white",
|
207
|
+
"blue" => "blue", "mag" => "magenta", "white" => "white",
|
208
|
+
"c" => "cyan", "mage" => "magenta", "y" => "yellow",
|
209
|
+
"cy" => "cyan", "magen" => "magenta", "ye" => "yellow",
|
210
|
+
"cya" => "cyan", "magent" => "magenta", "yel" => "yellow",
|
211
|
+
"cyan" => "cyan", "magenta" => "magenta", "yell" => "yellow",
|
212
|
+
"g" => "green", "r" => "red", "yello" => "yellow",
|
213
|
+
"gr" => "green", "re" => "red", "yellow" => "yellow",
|
214
|
+
"gre" => "green", "red" => "red"
|
215
|
+
}
|
216
|
+
|
217
|
+
STYLE_CODES =
|
218
|
+
{
|
219
|
+
'*' => "\e[1m",
|
220
|
+
'_' => "\e[4m",
|
221
|
+
'~' => "\e[7m",
|
222
|
+
'!' => "\e[5m"
|
223
|
+
}
|
224
|
+
|
225
|
+
##################################################################################################
|
226
|
+
######################################## Regular Expressions #####################################
|
227
|
+
##################################################################################################
|
228
|
+
|
229
|
+
RX_COLOR_WORD = %r< [a-z]+ >xi
|
230
|
+
RX_HEX_CODE = %r< 0? x? (?: \h{6} | \h{3} ) >xi
|
231
|
+
RX_COLOR_VALUE = Regexp.union(RX_COLOR_WORD, RX_HEX_CODE)
|
232
|
+
|
233
|
+
RX_TAGS =
|
234
|
+
%r`
|
235
|
+
( [\*~!_]* )
|
236
|
+
| ( #{ RX_COLOR_VALUE } (?: @ #{ RX_COLOR_VALUE } )?
|
237
|
+
| @ #{ RX_COLOR_VALUE }
|
238
|
+
)
|
239
|
+
`xi
|
240
|
+
|
241
|
+
RX_COLOR_ARG =
|
242
|
+
%r`
|
243
|
+
% \( ( #{ RX_TAGS } ) \)
|
244
|
+
( [\-\+0\s\#]? \d* (?:\.\d+)? [hlI]? [bcdEefGgiopsuXx] )
|
245
|
+
`xi
|
246
|
+
|
247
|
+
RX_ESCAPE = /^\\(.)/
|
248
|
+
RX_CLOSE_TAG = %r(^</#{ RX_TAGS }>)
|
249
|
+
RX_OPEN_TAG = %r(^<#{ RX_TAGS }>)
|
250
|
+
RX_TEXT = %r(^(.+?)(?=(?:\\.|</?#{ RX_TAGS }>|\Z)))m
|
251
|
+
|
252
|
+
BAD_CLOSE = %(bad format close tag: expected `%s', but got `%s')
|
253
|
+
|
254
|
+
##################################################################################################
|
255
|
+
############################################## Methods ###########################################
|
256
|
+
##################################################################################################
|
257
|
+
|
258
|
+
def self.color_value( name )
|
259
|
+
if name and not name.empty?
|
260
|
+
full_name = COLOR_NAMES.fetch( name.downcase ) do | i |
|
261
|
+
raise( FormatError, "`%s' is not a valid color name" % name )
|
262
|
+
end
|
263
|
+
COLOR_VALUES[ full_name ]
|
264
|
+
end
|
265
|
+
end
|
266
|
+
|
267
|
+
def self.color_escape_code(fg_or_bg, color_name)
|
268
|
+
if color_name && !color_name.empty?
|
269
|
+
if color_name =~ RX_HEX_CODE
|
270
|
+
fg_or_bg == :bg ? xterm256_bg(color_name) : xterm256_fg(color_name)
|
271
|
+
else
|
272
|
+
full_name =
|
273
|
+
COLOR_NAMES.fetch(color_name.downcase) do | i |
|
274
|
+
fail FormatError, "`%s' is not a valid color name" % color_name
|
275
|
+
end
|
276
|
+
sprintf(fg_or_bg == :bg ? BACKGROUND_CODE : FOREGROUND_CODE, COLOR_VALUES[full_name])
|
277
|
+
end
|
278
|
+
end
|
279
|
+
end
|
280
|
+
|
281
|
+
def self.color_scan( text )
|
282
|
+
foreground = []
|
283
|
+
background = []
|
284
|
+
styles = []
|
285
|
+
out = ''
|
286
|
+
|
287
|
+
text = text.gsub( RX_COLOR_ARG, '<\1>%\4</\1>' )
|
288
|
+
|
289
|
+
until text.nil? or text.empty?
|
290
|
+
print_escape = false
|
291
|
+
|
292
|
+
case text
|
293
|
+
when RX_ESCAPE
|
294
|
+
text = $'
|
295
|
+
out << ESCAPE_MAP[ $1 ]
|
296
|
+
|
297
|
+
when RX_CLOSE_TAG
|
298
|
+
text = $'
|
299
|
+
|
300
|
+
if style_char = $1
|
301
|
+
style_char.each_char { | c | styles.delete( STYLE_CODES.fetch(c) ) }
|
302
|
+
elsif colors = $2
|
303
|
+
fg, bg = colors.split( '@', 2 )
|
304
|
+
|
305
|
+
if value = color_escape_code(:fg, fg)
|
306
|
+
if current = foreground.last and current != value
|
307
|
+
display_map = COLOR_VALUES.invert
|
308
|
+
expected = display_map[ current ]
|
309
|
+
got = display_map[ value ]
|
310
|
+
fail FormatError, BAD_CLOSE % [ expected, got ]
|
311
|
+
end
|
312
|
+
foreground.pop
|
313
|
+
end
|
314
|
+
|
315
|
+
if value = color_escape_code(:bg, bg)
|
316
|
+
if current = background.last and current != value
|
317
|
+
display_map = COLOR_VALUES.invert
|
318
|
+
expected = display_map[current]
|
319
|
+
got = display_map[value]
|
320
|
+
fail FormatError, BAD_CLOSE % [expected, got]
|
321
|
+
end
|
322
|
+
background.pop
|
323
|
+
end
|
324
|
+
end
|
325
|
+
|
326
|
+
print_escape = true
|
327
|
+
|
328
|
+
when RX_OPEN_TAG
|
329
|
+
text = $'
|
330
|
+
|
331
|
+
if style_char = $1
|
332
|
+
style_char.each_char { |c| styles << STYLE_CODES.fetch(c) }
|
333
|
+
elsif colors = $2
|
334
|
+
fg, bg = colors.split('@', 2)
|
335
|
+
value = color_escape_code(:fg, fg) and foreground << value
|
336
|
+
value = color_escape_code(:bg, bg) and background << value
|
337
|
+
end
|
338
|
+
|
339
|
+
print_escape = true
|
340
|
+
|
341
|
+
when RX_TEXT
|
342
|
+
text = $'
|
343
|
+
out << $1
|
344
|
+
|
345
|
+
else
|
346
|
+
raise "this shouldn't happen: (#{ __FILE__ }@#{ __LINE__ })"
|
347
|
+
|
348
|
+
end
|
349
|
+
|
350
|
+
if print_escape
|
351
|
+
out << "\e[0m"
|
352
|
+
value = foreground.last and out << value
|
353
|
+
value = background.last and out << value
|
354
|
+
out << styles.join('')
|
355
|
+
end
|
356
|
+
end
|
357
|
+
|
358
|
+
if foreground.length + background.length + styles.length > 0
|
359
|
+
out << "\e[0m"
|
360
|
+
end
|
361
|
+
|
362
|
+
return out
|
363
|
+
end
|
364
|
+
|
365
|
+
def say_format( *args )
|
366
|
+
args = [ args ].flatten!
|
367
|
+
format = args.shift.to_s
|
368
|
+
sprintf( Say.color_scan( format ), *args )
|
369
|
+
end
|
370
|
+
|
371
|
+
def say_print( *args )
|
372
|
+
print( say_format( *args ) )
|
373
|
+
end
|
374
|
+
|
375
|
+
def say( *args )
|
376
|
+
puts( say_format( *args ) )
|
377
|
+
end
|
378
|
+
|
379
|
+
module_function :say_format, :say, :say_print
|
380
|
+
end
|
data/lib/say/import.rb
ADDED
data/test/test_say.rb
ADDED
metadata
ADDED
@@ -0,0 +1,85 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ohboyohboyohboy-say
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.2.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Kyle Yetter
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-04-30 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rdoc
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '4.0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '4.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: hoe
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '3.16'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '3.16'
|
41
|
+
description: Add ANSI color codes to strings using a simple markup.
|
42
|
+
email:
|
43
|
+
- kyle@ohboyohboyohboy.org
|
44
|
+
executables:
|
45
|
+
- say
|
46
|
+
extensions: []
|
47
|
+
extra_rdoc_files:
|
48
|
+
- History.txt
|
49
|
+
- README.txt
|
50
|
+
files:
|
51
|
+
- Gemfile
|
52
|
+
- History.txt
|
53
|
+
- README.txt
|
54
|
+
- Rakefile
|
55
|
+
- bin/say
|
56
|
+
- lib/say.rb
|
57
|
+
- lib/say/import.rb
|
58
|
+
- test/test_say.rb
|
59
|
+
homepage: http://gems.ohboyohboyohboy.org/ohboyohboyohboy-say
|
60
|
+
licenses:
|
61
|
+
- MIT
|
62
|
+
metadata: {}
|
63
|
+
post_install_message:
|
64
|
+
rdoc_options:
|
65
|
+
- "--main"
|
66
|
+
- README.txt
|
67
|
+
require_paths:
|
68
|
+
- lib
|
69
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
70
|
+
requirements:
|
71
|
+
- - ">="
|
72
|
+
- !ruby/object:Gem::Version
|
73
|
+
version: '0'
|
74
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
75
|
+
requirements:
|
76
|
+
- - ">="
|
77
|
+
- !ruby/object:Gem::Version
|
78
|
+
version: '0'
|
79
|
+
requirements: []
|
80
|
+
rubyforge_project:
|
81
|
+
rubygems_version: 2.4.8
|
82
|
+
signing_key:
|
83
|
+
specification_version: 4
|
84
|
+
summary: Add ANSI color codes to strings using a simple markup.
|
85
|
+
test_files: []
|