Add padding to pairs to right-align key

This commit is contained in:
Michael Miller 2019-02-28 20:14:03 -07:00
parent cc3392022e
commit f37105af5e
2 changed files with 8 additions and 2 deletions

View file

@ -2,11 +2,12 @@ module Spectator::Formatting
# A single key-value pair from the `Spectator::Matchers::MatchData#value` method. # A single key-value pair from the `Spectator::Matchers::MatchData#value` method.
private struct MatchDataValuePair(T) private struct MatchDataValuePair(T)
# Creates the pair formatter. # Creates the pair formatter.
def initialize(@key : Symbol, @value : T) def initialize(@key : Symbol, @value : T, @padding : Int32)
end end
# Appends the pair to the output. # Appends the pair to the output.
def to_s(io) def to_s(io)
@padding.times { io << ' ' }
io << @key io << @key
io << ": " io << ": "
@value.inspect(io) @value.inspect(io)

View file

@ -4,14 +4,19 @@ module Spectator::Formatting
private struct MatchDataValues(T) private struct MatchDataValues(T)
include Enumerable(MatchDataValuePair) include Enumerable(MatchDataValuePair)
@max_key_length : Int32
# Creates the values mapper. # Creates the values mapper.
def initialize(@values : T) def initialize(@values : T)
@max_key_length = T.types.keys.map(&.to_s.size).max
end end
# Yields pairs that can be printed to output. # Yields pairs that can be printed to output.
def each def each
@values.each do |key, value| @values.each do |key, value|
yield MatchDataValuePair.new(key, value) key_length = key.to_s.size
padding = @max_key_length - key_length
yield MatchDataValuePair.new(key, value, padding)
end end
end end
end end