prettys 0.0.2 → 0.0.3
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.
- checksums.yaml +4 -4
- data/lib/colorizer.rb +71 -0
- data/lib/matcher.rb +15 -0
- data/lib/prettys.rb +8 -4
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 524b43c2fff5dfe3b7fb931b0a02f2fe56622b2b
|
4
|
+
data.tar.gz: 594bdff6580a5e1ccf8fa1918e6af238550895c2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 510247031a3a15f42cfe1f58037ee2c7115649af7f53c323a2529dd5716825b95f41551ac02c70a90c2473d89045b3a3e30d52bbd177b7706a23fd7e6bc6e403
|
7
|
+
data.tar.gz: b3b1f461a08f66d5c94bf66e5f1f7dec3af4d7da24dcbd40c65e2905a220157bb49947415656cf6f31a68f86ea7bf0fce7c9a45dabdcf487686d4d7a206ffabe
|
data/lib/colorizer.rb
ADDED
@@ -0,0 +1,71 @@
|
|
1
|
+
require 'matcher'
|
2
|
+
|
3
|
+
module Prettys
|
4
|
+
class Colorizer
|
5
|
+
|
6
|
+
COLOR_NAMES = [
|
7
|
+
:black,
|
8
|
+
:red,
|
9
|
+
:green,
|
10
|
+
:yellow,
|
11
|
+
:blue,
|
12
|
+
:magenta,
|
13
|
+
:cyan,
|
14
|
+
:white
|
15
|
+
]
|
16
|
+
|
17
|
+
CHROMATIC_COLOR_NAMES = COLOR_NAMES[1...-1]
|
18
|
+
|
19
|
+
COLORS = {}
|
20
|
+
COLOR_NAMES.each_with_index do |color_name, index|
|
21
|
+
COLORS[color_name] = index + 30
|
22
|
+
end
|
23
|
+
|
24
|
+
private_constant :COLOR_NAMES, :CHROMATIC_COLOR_NAMES, :COLORS
|
25
|
+
|
26
|
+
def escape_sequence(options)
|
27
|
+
options = add_default_options(options)
|
28
|
+
color_code = COLORS[options[:color]]
|
29
|
+
color_code += 10 if options[:type] == :background
|
30
|
+
"\e[#{color_code.to_s};#{(options[:bold] ? 1 : 2).to_s}m"
|
31
|
+
end
|
32
|
+
|
33
|
+
def end_of_escape_sequence
|
34
|
+
"\e[0m"
|
35
|
+
end
|
36
|
+
|
37
|
+
def escaped_string(options)
|
38
|
+
escape_sequence(options) + options[:string] + end_of_escape_sequence
|
39
|
+
end
|
40
|
+
|
41
|
+
def colorize(options = {})
|
42
|
+
options = add_default_options(options)
|
43
|
+
unless [:background, :foreground].include?(options[:type])
|
44
|
+
raise(ArgumentError, "Type must be a :background or :foreground")
|
45
|
+
end
|
46
|
+
marked_strings = Matcher.marked_strings(options[:string], options[:pattern])
|
47
|
+
marked_strings.map do |ms|
|
48
|
+
if ms[:marked]
|
49
|
+
escaped_string({
|
50
|
+
string: ms[:string],
|
51
|
+
pattern: options[:pattern],
|
52
|
+
type: options[:type],
|
53
|
+
bold: options[:bold],
|
54
|
+
color: options[:color]
|
55
|
+
})
|
56
|
+
else
|
57
|
+
ms[:string]
|
58
|
+
end
|
59
|
+
end.join
|
60
|
+
end
|
61
|
+
|
62
|
+
private
|
63
|
+
|
64
|
+
def add_default_options(options)
|
65
|
+
options[:type] = :foreground unless options[:type]
|
66
|
+
options[:bold] = false unless options[:bold]
|
67
|
+
options[:color] = COLOR_NAMES[1] unless options[:color]
|
68
|
+
return options
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
data/lib/matcher.rb
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
module Prettys
|
2
|
+
module Matcher
|
3
|
+
|
4
|
+
def Matcher.marked_strings(string, pattern)
|
5
|
+
matched = string.scan(pattern).map { |s| { string: s, marked: true } }
|
6
|
+
not_matched = string.split(pattern).map { |s| { string: s, marked: false } }
|
7
|
+
if (string.match(pattern).begin(0))
|
8
|
+
not_matched.zip(matched)
|
9
|
+
else
|
10
|
+
matched.zip(not_matched)
|
11
|
+
end.flatten.compact
|
12
|
+
end
|
13
|
+
|
14
|
+
end
|
15
|
+
end
|
data/lib/prettys.rb
CHANGED
@@ -3,8 +3,10 @@ require 'yaml'
|
|
3
3
|
|
4
4
|
module Prettys
|
5
5
|
require 'converter'
|
6
|
+
require 'colorizer'
|
6
7
|
|
7
8
|
@converter = Converter.new(:json)
|
9
|
+
@colorizer = Colorizer.new
|
8
10
|
|
9
11
|
class << self
|
10
12
|
def format
|
@@ -15,14 +17,16 @@ module Prettys
|
|
15
17
|
@converter.format = format
|
16
18
|
end
|
17
19
|
|
18
|
-
def prettys(object)
|
19
|
-
@converter.convert(object)
|
20
|
+
def prettys(object, options)
|
21
|
+
options[:string] = @converter.convert(object)
|
22
|
+
@colorizer.colorize(options)
|
20
23
|
end
|
21
24
|
end
|
22
25
|
end
|
23
26
|
|
24
27
|
class Object
|
25
|
-
def prettys
|
26
|
-
|
28
|
+
def prettys(pattern, options = {})
|
29
|
+
options[:pattern] = pattern
|
30
|
+
Prettys.prettys(self, options)
|
27
31
|
end
|
28
32
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: prettys
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Maciej Ciemborowicz
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-
|
11
|
+
date: 2013-11-08 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: Colorful inspector for Ruby objects.
|
14
14
|
email: pub@ciemborowicz.pl
|
@@ -18,6 +18,8 @@ extra_rdoc_files: []
|
|
18
18
|
files:
|
19
19
|
- lib/prettys.rb
|
20
20
|
- lib/converter.rb
|
21
|
+
- lib/colorizer.rb
|
22
|
+
- lib/matcher.rb
|
21
23
|
homepage: http://rubygems.org/gems/prettys
|
22
24
|
licenses:
|
23
25
|
- MIT
|