1
0
Fork 0
mirror of https://github.com/awesome-print/awesome_print synced 2023-03-27 23:22:34 -04:00

Add base formatter and colorize

This commit is contained in:
Waldyr de Souza 2016-05-13 00:20:07 -03:00
parent fa28bdb42d
commit 35c2c5f17f
2 changed files with 151 additions and 0 deletions

View file

@ -0,0 +1,24 @@
autoload :CGI, "cgi"
module AwesomePrint
module Colorize
# Pick the color and apply it to the given string as necessary.
#------------------------------------------------------------------------------
def colorize(str, type)
str = CGI.escapeHTML(str) if options[:html]
if options[:plain] || !options[:color][type] || !inspector.colorize?
str
#
# Check if the string color method is defined by awesome_print and accepts
# html parameter or it has been overriden by some gem such as colorize.
#
elsif str.method(options[:color][type]).arity == -1 # Accepts html parameter.
str.send(options[:color][type], options[:html])
else
str = %Q|<kbd style="color:#{options[:color][type]}">#{str}</kbd>| if options[:html]
str.send(options[:color][type])
end
end
end
end

View file

@ -0,0 +1,127 @@
require_relative "../colorize"
module AwesomePrint
module Formatters
class BaseFormatter
include Colorize
DEFAULT_LIMIT_SIZE = 7
# 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
# ]
#------------------------------------------------------------------------------
def should_be_limited?
options[:limit] or (options[:limit].is_a?(Fixnum) and options[:limit] > 0)
end
def get_limit_size
options[:limit] ? DEFAULT_LIMIT_SIZE : options[:limit]
end
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]
if is_hash
temp[head] = "#{indent}#{data[head].strip} .. #{data[data.length - tail - 1].strip}"
else
temp[head] = "#{indent}[#{head.to_s.rjust(width)}] .. [#{data.length - tail - 1}]"
end
temp
end
end
def align(value, width)
if options[:multiline]
if options[:indent] > 0
value.rjust(width)
elsif options[:indent] == 0
indent + value.ljust(width)
else
indent[0, indentation + options[:indent]] + value.ljust(width)
end
else
value
end
end
def indented
@indentation += options[:indent].abs
yield
ensure
@indentation -= options[:indent].abs
end
def indent
' ' * indentation
end
def outdent
' ' * (indentation - options[:indent].abs)
end
def method_tuple(method)
if method.respond_to?(:parameters) # Ruby 1.9.2+
# See http://ruby.runpaint.org/methods#method-objects-parameters
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 :opt, :rest then "*#{name}"
when :block then "&#{name}"
else '?'
end
end
else # See http://ruby-doc.org/core/classes/Method.html#M001902
args = (1..method.arity.abs).map { |i| "arg#{i}" }
args[-1] = "*#{args[-1]}" if method.arity < 0
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>
# #<UnboundMethod: Hello#world>
#
if method.to_s =~ /(Unbound)*Method: (.*)[#\.]/
unbound, klass = $1 && '(unbound)', $2
if klass && klass =~ /(\(\w+:\s.*?\))/ # Is this ActiveRecord-style class?
klass.sub!($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
end
end
end