diff --git a/src/spectator/formatting/match_data_value_pair.cr b/src/spectator/formatting/match_data_value_pair.cr index c9cc3f6..46823c3 100644 --- a/src/spectator/formatting/match_data_value_pair.cr +++ b/src/spectator/formatting/match_data_value_pair.cr @@ -2,11 +2,12 @@ module Spectator::Formatting # A single key-value pair from the `Spectator::Matchers::MatchData#value` method. private struct MatchDataValuePair(T) # Creates the pair formatter. - def initialize(@key : Symbol, @value : T) + def initialize(@key : Symbol, @value : T, @padding : Int32) end # Appends the pair to the output. def to_s(io) + @padding.times { io << ' ' } io << @key io << ": " @value.inspect(io) diff --git a/src/spectator/formatting/match_data_values.cr b/src/spectator/formatting/match_data_values.cr index b1a584f..d8d6301 100644 --- a/src/spectator/formatting/match_data_values.cr +++ b/src/spectator/formatting/match_data_values.cr @@ -4,14 +4,19 @@ module Spectator::Formatting private struct MatchDataValues(T) include Enumerable(MatchDataValuePair) + @max_key_length : Int32 + # Creates the values mapper. def initialize(@values : T) + @max_key_length = T.types.keys.map(&.to_s.size).max end # Yields pairs that can be printed to output. def each @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