epitools 0.3.0 → 0.3.1
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION +1 -1
- data/epitools.gemspec +2 -1
- data/lib/epitools/colored.rb +224 -0
- metadata +3 -2
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.3.
|
1
|
+
0.3.1
|
data/epitools.gemspec
CHANGED
@@ -5,7 +5,7 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{epitools}
|
8
|
-
s.version = "0.3.
|
8
|
+
s.version = "0.3.1"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["epitron"]
|
@@ -28,6 +28,7 @@ Gem::Specification.new do |s|
|
|
28
28
|
"lib/epitools/browser.rb",
|
29
29
|
"lib/epitools/browser/browser_cache.rb",
|
30
30
|
"lib/epitools/browser/mechanize_progressbar.rb",
|
31
|
+
"lib/epitools/colored.rb",
|
31
32
|
"lib/epitools/hexdump.rb",
|
32
33
|
"lib/epitools/highlight.rb",
|
33
34
|
"lib/epitools/http.rb",
|
@@ -0,0 +1,224 @@
|
|
1
|
+
require 'Win32/Console/ANSI' if RUBY_PLATFORM =~ /win32/
|
2
|
+
require 'set'
|
3
|
+
|
4
|
+
##
|
5
|
+
# cute.
|
6
|
+
#
|
7
|
+
# >> "this is red".red
|
8
|
+
#
|
9
|
+
# >> "this is red with a blue background (read: ugly)".red_on_blue
|
10
|
+
#
|
11
|
+
# >> "this is red with an underline".red.underline
|
12
|
+
#
|
13
|
+
# >> "this is really bold and really blue".bold.blue
|
14
|
+
#
|
15
|
+
# >> Colored.red "This is red" # but this part is mostly untested
|
16
|
+
module Colored
|
17
|
+
extend self
|
18
|
+
|
19
|
+
###########################################################################
|
20
|
+
|
21
|
+
@@is_tty = STDOUT.isatty
|
22
|
+
|
23
|
+
COLORS = {
|
24
|
+
'black' => 30,
|
25
|
+
'red' => 31,
|
26
|
+
'green' => 32,
|
27
|
+
'yellow' => 33,
|
28
|
+
'blue' => 34,
|
29
|
+
'magenta' => 35,
|
30
|
+
'cyan' => 36,
|
31
|
+
'white' => 37
|
32
|
+
}
|
33
|
+
|
34
|
+
EXTRAS = {
|
35
|
+
'clear' => 0,
|
36
|
+
'bold' => 1,
|
37
|
+
'light' => 1,
|
38
|
+
'underline' => 4,
|
39
|
+
'reversed' => 7
|
40
|
+
}
|
41
|
+
|
42
|
+
#
|
43
|
+
# BBS-style numeric color codes.
|
44
|
+
#
|
45
|
+
BBS_COLOR_TABLE = {
|
46
|
+
0 => :black,
|
47
|
+
1 => :blue,
|
48
|
+
2 => :green,
|
49
|
+
3 => :cyan,
|
50
|
+
4 => :red,
|
51
|
+
5 => :magenta,
|
52
|
+
6 => :yellow,
|
53
|
+
7 => :white,
|
54
|
+
8 => :light_black,
|
55
|
+
9 => :light_blue,
|
56
|
+
10 => :light_green,
|
57
|
+
11 => :light_cyan,
|
58
|
+
12 => :light_red,
|
59
|
+
13 => :light_magenta,
|
60
|
+
14 => :light_yellow,
|
61
|
+
15 => :light_white,
|
62
|
+
}
|
63
|
+
|
64
|
+
VALID_COLORS = Set.new(
|
65
|
+
COLORS.keys +
|
66
|
+
COLORS.map { |k,v| "light_#{k}" } +
|
67
|
+
COLORS.map { |k,v| "bold_#{k}" }
|
68
|
+
)
|
69
|
+
|
70
|
+
###########################################################################
|
71
|
+
|
72
|
+
COLORS.each do |color, value|
|
73
|
+
define_method(color) do
|
74
|
+
colorize(self, :foreground => color)
|
75
|
+
end
|
76
|
+
|
77
|
+
define_method("on_#{color}") do
|
78
|
+
colorize(self, :background => color)
|
79
|
+
end
|
80
|
+
|
81
|
+
define_method("light_#{color}") do
|
82
|
+
colorize(self, :foreground => color, :extra => 'bold')
|
83
|
+
end
|
84
|
+
|
85
|
+
COLORS.each do |highlight, value|
|
86
|
+
next if color == highlight
|
87
|
+
define_method("#{color}_on_#{highlight}") do
|
88
|
+
colorize(self, :foreground => color, :background => highlight)
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
EXTRAS.each do |extra, value|
|
94
|
+
next if extra == 'clear'
|
95
|
+
define_method(extra) do
|
96
|
+
colorize(self, :extra => extra)
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
define_method(:to_eol) do
|
101
|
+
tmp = sub(/^(\e\[[\[\e0-9;m]+m)/, "\\1\e[2K")
|
102
|
+
if tmp == self
|
103
|
+
return "\e[2K" << self
|
104
|
+
end
|
105
|
+
tmp
|
106
|
+
end
|
107
|
+
|
108
|
+
###########################################################################
|
109
|
+
|
110
|
+
def colorize(string=nil, options = {})
|
111
|
+
if string == nil
|
112
|
+
return tagged_colors(self)
|
113
|
+
end
|
114
|
+
|
115
|
+
if @@is_tty
|
116
|
+
colored = [color(options[:foreground]), color("on_#{options[:background]}"), extra(options[:extra])].compact * ''
|
117
|
+
colored << string
|
118
|
+
colored << extra(:clear)
|
119
|
+
else
|
120
|
+
string
|
121
|
+
end
|
122
|
+
end
|
123
|
+
|
124
|
+
def colors
|
125
|
+
@@colors ||= COLORS.keys.sort
|
126
|
+
end
|
127
|
+
|
128
|
+
def extra(extra_name)
|
129
|
+
extra_name = extra_name.to_s
|
130
|
+
"\e[#{EXTRAS[extra_name]}m" if EXTRAS[extra_name]
|
131
|
+
end
|
132
|
+
|
133
|
+
def color(color_name)
|
134
|
+
background = color_name.to_s =~ /on_/
|
135
|
+
color_name = color_name.to_s.sub('on_', '')
|
136
|
+
return unless color_name && COLORS[color_name]
|
137
|
+
"\e[#{COLORS[color_name] + (background ? 10 : 0)}m"
|
138
|
+
end
|
139
|
+
|
140
|
+
###########################################################################
|
141
|
+
|
142
|
+
def enable!
|
143
|
+
@@is_tty = true
|
144
|
+
end
|
145
|
+
|
146
|
+
alias_method :force!, :enable!
|
147
|
+
|
148
|
+
def disable!
|
149
|
+
@@is_tty = false
|
150
|
+
end
|
151
|
+
|
152
|
+
def is_tty?
|
153
|
+
@@is_tty
|
154
|
+
end
|
155
|
+
|
156
|
+
#
|
157
|
+
# Is this string legal?
|
158
|
+
#
|
159
|
+
def valid_tag?(tag)
|
160
|
+
VALID_COLORS.include?(tag) or
|
161
|
+
(
|
162
|
+
string =~ /^\d+$/ and
|
163
|
+
BBS_COLOR_TABLE.include?(tag.to_i)
|
164
|
+
)
|
165
|
+
end
|
166
|
+
|
167
|
+
#
|
168
|
+
# Colorize a string that has "color tags".
|
169
|
+
#
|
170
|
+
# Examples:
|
171
|
+
#
|
172
|
+
# Colors as words:
|
173
|
+
# puts "<light_yellow><light_white>*</light_white> Hey mom! I am <light_green>SO</light_green> colourized right now.</light_yellow>".colorize
|
174
|
+
#
|
175
|
+
# Numeric ANSI colors (from the BBS days):
|
176
|
+
# puts "<10><5>*</5> Hey mom! I am <9>SO</9> colourized right now.</10>".colorize
|
177
|
+
#
|
178
|
+
def tagged_colors(string)
|
179
|
+
stack = []
|
180
|
+
|
181
|
+
# split the string into tags and literal strings
|
182
|
+
tokens = string.split(/(<\/?[\w\d_]+>)/)
|
183
|
+
tokens.delete_if { |token| token.size == 0 }
|
184
|
+
|
185
|
+
result = ""
|
186
|
+
|
187
|
+
tokens.each do |token|
|
188
|
+
|
189
|
+
# token is an opening tag!
|
190
|
+
|
191
|
+
if /<([\w\d_]+)>/ =~ token and valid_tag?($1)
|
192
|
+
stack.push $1
|
193
|
+
|
194
|
+
# token is a closing tag!
|
195
|
+
|
196
|
+
elsif /<\/([\w\d_]+)>/ =~ token and valid_tag?($1)
|
197
|
+
|
198
|
+
# if this color is on the stack somwehere...
|
199
|
+
if pos = stack.rindex($1)
|
200
|
+
# close the tag by removing it from the stack
|
201
|
+
stack.delete_at pos
|
202
|
+
else
|
203
|
+
raise "Error: tried to close an unopened color tag -- #{token}"
|
204
|
+
end
|
205
|
+
|
206
|
+
# token is a literal string!
|
207
|
+
|
208
|
+
else
|
209
|
+
|
210
|
+
color = (stack.last || "white")
|
211
|
+
color = BBS_COLOR_TABLE[color.to_i] if color =~ /^\d+$/
|
212
|
+
result << token.send(color)
|
213
|
+
|
214
|
+
end
|
215
|
+
|
216
|
+
end
|
217
|
+
|
218
|
+
result
|
219
|
+
end
|
220
|
+
|
221
|
+
|
222
|
+
end unless Object.const_defined? :Colored
|
223
|
+
|
224
|
+
String.send(:include, Colored)
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 3
|
8
|
-
-
|
9
|
-
version: 0.3.
|
8
|
+
- 1
|
9
|
+
version: 0.3.1
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- epitron
|
@@ -81,6 +81,7 @@ files:
|
|
81
81
|
- lib/epitools/browser.rb
|
82
82
|
- lib/epitools/browser/browser_cache.rb
|
83
83
|
- lib/epitools/browser/mechanize_progressbar.rb
|
84
|
+
- lib/epitools/colored.rb
|
84
85
|
- lib/epitools/hexdump.rb
|
85
86
|
- lib/epitools/highlight.rb
|
86
87
|
- lib/epitools/http.rb
|