Class: AmazingPrint::Formatters::BaseFormatter
- Includes:
- Colorize
- Defined in:
- lib/amazing_print/formatters/base_formatter.rb
Direct Known Subclasses
ArrayFormatter, ClassFormatter, DirFormatter, FileFormatter, HashFormatter, MethodFormatter, ObjectFormatter, SimpleFormatter, StructFormatter
Constant Summary collapse
- DEFAULT_LIMIT_SIZE =
7
- INDENT_CACHE =
precompute common indentations
(0..100).map { |i| ' ' * i }.map(&:freeze).freeze
Instance Method Summary collapse
- #align(value, width) ⇒ Object
- #colorless_size(string) ⇒ Object
- #get_limit_size ⇒ Object
- #indent(n = indentation) ⇒ Object
-
#indentation ⇒ Object
Indentation related methods —————————————–.
- #indented(&blk) ⇒ Object
- #limited(data, width, is_hash: false) ⇒ Object
- #method_tuple(method) ⇒ Object
- #outdent ⇒ Object
-
#should_be_limited? ⇒ Boolean
To support limited output, for example:.
Methods included from Colorize
Instance Method Details
#align(value, width) ⇒ Object
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 [:multiline] indent_option = [: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_size(string) ⇒ Object
146 147 148 |
# File 'lib/amazing_print/formatters/base_formatter.rb', line 146 def colorless_size(string) string.gsub(/\e\[[\d;]+m/, '').size end |
#get_limit_size ⇒ Object
36 37 38 39 40 41 42 43 |
# File 'lib/amazing_print/formatters/base_formatter.rb', line 36 def get_limit_size case [:limit] when true DEFAULT_LIMIT_SIZE else [:limit] end end |
#indent(n = indentation) ⇒ Object
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
109 110 111 |
# File 'lib/amazing_print/formatters/base_formatter.rb', line 109 def indentation inspector.current_indentation end |
#indented(&blk) ⇒ Object
113 114 115 |
# File 'lib/amazing_print/formatters/base_formatter.rb', line 113 def indented(&blk) inspector.increase_indentation(&blk) end |
#limited(data, width, is_hash: false) ⇒ Object
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 # Calculate how many elements to be displayed above and below the separator. head = limit / 2 tail = head - ((limit - 1) % 2) # Add the proper elements to the temp array and format the separator. 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
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) # See http://readruby.chengguangnan.com/methods#method-objects-parameters # (mirror: http://archive.is/XguCA#selection-3381.1-3381.11) 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 # method.to_s formats to handle: # # #<Method: Fixnum#zero?> # #<Method: Fixnum(Integer)#years> # #<Method: User(#<Module:0x00000103207c00>)#_username> # #<Method: User(id: integer, username: string).table_name> # #<Method: User(id: integer, username: string)(ActiveRecord::Base).current> # #<Method: #<Class:0x100c567f>(ActiveRecord::Querying)#first> # #<UnboundMethod: Hello#world> # #<UnboundMethod: Hello#world() /home/hs/code/amazing_print/spec/methods_spec.rb:68> # if method.to_s =~ %r{(Unbound)*Method: ((#<)?[^/#]*)[#.]} unbound = Regexp.last_match(1) && '(unbound)' klass = Regexp.last_match(2) if klass && klass =~ /(\(\w+:\s.*?\))/ # Is this ActiveRecord-style class? klass.sub!(Regexp.last_match(1), '') # Yes, strip the fields leaving class name only. end owner = "#{klass}#{unbound}".gsub('(', ' (') end [method.name.to_s, "(#{args.join(', ')})", owner.to_s] end |
#outdent ⇒ Object
124 125 126 127 128 |
# File 'lib/amazing_print/formatters/base_formatter.rb', line 124 def outdent i = indentation - [: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
]
32 33 34 |
# File 'lib/amazing_print/formatters/base_formatter.rb', line 32 def should_be_limited? [:limit] || ([:limit].is_a?(Integer) && [:limit].positive?) end |