mirror of
https://github.com/awesome-print/awesome_print
synced 2023-03-27 23:22:34 -04:00
Merge pull request #287 from gerrywastaken/pr-247-rebased
Pr 247 rebased
This commit is contained in:
commit
ee30974f54
1 changed files with 58 additions and 26 deletions
|
@ -3,7 +3,6 @@ require_relative 'base_formatter'
|
||||||
module AwesomePrint
|
module AwesomePrint
|
||||||
module Formatters
|
module Formatters
|
||||||
class HashFormatter < BaseFormatter
|
class HashFormatter < BaseFormatter
|
||||||
|
|
||||||
attr_reader :hash, :inspector, :options
|
attr_reader :hash, :inspector, :options
|
||||||
|
|
||||||
def initialize(hash, inspector)
|
def initialize(hash, inspector)
|
||||||
|
@ -13,20 +12,38 @@ module AwesomePrint
|
||||||
end
|
end
|
||||||
|
|
||||||
def format
|
def format
|
||||||
return '{}' if hash == {}
|
if hash.empty?
|
||||||
|
empty_hash
|
||||||
keys = hash.keys
|
elsif multiline_hash?
|
||||||
keys = keys.sort { |a, b| a.to_s <=> b.to_s } if options[:sort_keys]
|
multiline_hash
|
||||||
data = keys.map do |key|
|
else
|
||||||
plain_single_line do
|
simple_hash
|
||||||
[inspector.awesome(key), hash[key]]
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
width = data.map { |key, _value| key.size }.max || 0
|
private
|
||||||
width += indentation if options[:indent] > 0
|
|
||||||
|
|
||||||
data = data.map do |key, value|
|
def empty_hash
|
||||||
|
'{}'
|
||||||
|
end
|
||||||
|
|
||||||
|
def multiline_hash?
|
||||||
|
options[:multiline]
|
||||||
|
end
|
||||||
|
|
||||||
|
def multiline_hash
|
||||||
|
"{\n" << printable_hash.join(",\n") << "\n#{outdent}}"
|
||||||
|
end
|
||||||
|
|
||||||
|
def simple_hash
|
||||||
|
"{ #{printable_hash.join(', ')} }"
|
||||||
|
end
|
||||||
|
|
||||||
|
def printable_hash
|
||||||
|
data = printable_keys
|
||||||
|
width = left_width(data)
|
||||||
|
|
||||||
|
data.map! do |key, value|
|
||||||
indented do
|
indented do
|
||||||
if options[:ruby19_syntax] && symbol?(key)
|
if options[:ruby19_syntax] && symbol?(key)
|
||||||
ruby19_syntax(key, value, width)
|
ruby19_syntax(key, value, width)
|
||||||
|
@ -36,27 +53,42 @@ module AwesomePrint
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
data = limited(data, width, hash: true) if should_be_limited?
|
should_be_limited? ? limited(data, width, hash: true) : data
|
||||||
if options[:multiline]
|
end
|
||||||
"{\n" << data.join(",\n") << "\n#{outdent}}"
|
|
||||||
else
|
def left_width(keys)
|
||||||
"{ #{data.join(', ')} }"
|
result = max_key_width(keys)
|
||||||
|
result += indentation if options[:indent] > 0
|
||||||
|
result
|
||||||
|
end
|
||||||
|
|
||||||
|
def max_key_width(keys)
|
||||||
|
keys.map { |key, _value| key.size }.max || 0
|
||||||
|
end
|
||||||
|
|
||||||
|
def printable_keys
|
||||||
|
keys = hash.keys
|
||||||
|
|
||||||
|
keys.sort! { |a, b| a.to_s <=> b.to_s } if options[:sort_keys]
|
||||||
|
|
||||||
|
keys.map! do |key|
|
||||||
|
plain_single_line do
|
||||||
|
[inspector.awesome(key), hash[key]]
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
def symbol?(key)
|
||||||
|
key[0] == ':'
|
||||||
def symbol?(k)
|
|
||||||
k[0] == ':'
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def ruby19_syntax(k, v, width)
|
def ruby19_syntax(key, value, width)
|
||||||
k[0] = ''
|
key[0] = ''
|
||||||
align(k, width - 1) << colorize(': ', :hash) << inspector.awesome(v)
|
align(key, width - 1) << colorize(': ', :hash) << inspector.awesome(value)
|
||||||
end
|
end
|
||||||
|
|
||||||
def pre_ruby19_syntax(k, v, width)
|
def pre_ruby19_syntax(key, value, width)
|
||||||
align(k, width) << colorize(' => ', :hash) << inspector.awesome(v)
|
align(key, width) << colorize(' => ', :hash) << inspector.awesome(value)
|
||||||
end
|
end
|
||||||
|
|
||||||
def plain_single_line
|
def plain_single_line
|
||||||
|
|
Loading…
Add table
Reference in a new issue