From f37105af5e3cd341920a37e12443b632e9bed504 Mon Sep 17 00:00:00 2001 From: Michael Miller Date: Thu, 28 Feb 2019 20:14:03 -0700 Subject: [PATCH] Add padding to pairs to right-align key --- src/spectator/formatting/match_data_value_pair.cr | 3 ++- src/spectator/formatting/match_data_values.cr | 7 ++++++- 2 files changed, 8 insertions(+), 2 deletions(-) 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