Class: AmazingPrint::Formatters::BaseFormatter
- Inherits:
-
Object
- Object
- AmazingPrint::Formatters::BaseFormatter
show all
- Includes:
- Colorize
- Defined in:
- lib/amazing_print/formatters/base_formatter.rb
Constant Summary
collapse
- DEFAULT_LIMIT_SIZE =
7
- INDENT_CACHE =
precompute common indentations
(0..100).map { |i| ' ' * i }.map(&:freeze).freeze
Instance Method Summary
collapse
Methods included from Colorize
#colorize
Instance Method Details
#align(value, width) ⇒ Object
[View source]
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
|
# File 'lib/amazing_print/formatters/base_formatter.rb', line 130
def align(value, width)
if options[:multiline]
indent_option = options[:indent]
effective_width = width + value.size - colorless_size(value)
if indent_option.positive?
value.rjust(effective_width)
elsif indent_option.zero?
"#{indent}#{value.ljust(effective_width)}"
else
"#{indent(indentation + indent_option)}#{value.ljust(effective_width)}"
end
else
value
end
end
|
#colorless(string) ⇒ Object
[View source]
146
147
148
|
# File 'lib/amazing_print/formatters/base_formatter.rb', line 146
def colorless(string)
string.gsub(/\e\[[\d;]+m/, '')
end
|
#colorless_size(string) ⇒ Object
[View source]
150
151
152
|
# File 'lib/amazing_print/formatters/base_formatter.rb', line 150
def colorless_size(string)
colorless(string).size
end
|
#get_limit_size ⇒ Object
[View source]
36
37
38
39
40
41
42
43
|
# File 'lib/amazing_print/formatters/base_formatter.rb', line 36
def get_limit_size
case options[:limit]
when true
DEFAULT_LIMIT_SIZE
else
options[:limit]
end
end
|
#indent(n = indentation) ⇒ Object
[View source]
120
121
122
|
# File 'lib/amazing_print/formatters/base_formatter.rb', line 120
def indent(n = indentation)
INDENT_CACHE[n] || (' ' * n)
end
|
#indentation ⇒ Object
Indentation related methods
[View source]
109
110
111
|
# File 'lib/amazing_print/formatters/base_formatter.rb', line 109
def indentation
inspector.current_indentation
end
|
[View source]
113
114
115
|
# File 'lib/amazing_print/formatters/base_formatter.rb', line 113
def indented(&)
inspector.increase_indentation(&)
end
|
#limited(data, width, is_hash: false) ⇒ Object
[View source]
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
|
# File 'lib/amazing_print/formatters/base_formatter.rb', line 45
def limited(data, width, is_hash: false)
limit = get_limit_size
if data.length <= limit
data
else
head = limit / 2
tail = head - ((limit - 1) % 2)
temp = data[0, head] + [nil] + data[-tail, tail]
temp[head] = if is_hash
"#{indent}#{data[head].strip} .. #{data[data.length - tail - 1].strip}"
else
"#{indent}[#{head.to_s.rjust(width)}] .. [#{data.length - tail - 1}]"
end
temp
end
end
|
#method_tuple(method) ⇒ Object
[View source]
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
|
# File 'lib/amazing_print/formatters/base_formatter.rb', line 67
def method_tuple(method)
args = method.parameters.inject([]) do |arr, (type, name)|
name ||= (type == :block ? 'block' : "arg#{arr.size + 1}")
arr << case type
when :req then name.to_s
when :keyreq then "#{name}:"
when :key then "*#{name}:"
when :opt, :rest then "*#{name}"
when :block then "&#{name}"
else '?'
end
end
if method.to_s =~ %r{(Unbound)*Method: ((#<)?[^/#]*)[#.]}
unbound = Regexp.last_match(1) && '(unbound)'
klass = Regexp.last_match(2)
if klass && klass =~ /(\(\w+:\s.*?\))/ klass.sub!(Regexp.last_match(1), '') end
owner = "#{klass}#{unbound}".gsub('(', ' (')
end
[method.name.to_s, "(#{args.join(', ')})", owner.to_s]
end
|
[View source]
124
125
126
127
128
|
# File 'lib/amazing_print/formatters/base_formatter.rb', line 124
def outdent
i = indentation - options[:indent].abs
INDENT_CACHE[i] || (' ' * i)
end
|
#should_be_limited? ⇒ Boolean
To support limited output, for example:
ap (‘a’..‘z’).to_a, :limit => 3 [
[ 0] "a",
[ 1] .. [24],
[25] "z"
]
ap (1..100).to_a, :limit => true # Default limit is 7. [
[ 0] 1,
[ 1] 2,
[ 2] 3,
[ 3] .. [96],
[97] 98,
[98] 99,
[99] 100
]
[View source]
32
33
34
|
# File 'lib/amazing_print/formatters/base_formatter.rb', line 32
def should_be_limited?
options[:limit] || (options[:limit].is_a?(Integer) && options[:limit].positive?)
end
|