mirror of
https://gitea.invidious.io/iv-org/shard-spectator.git
synced 2024-08-15 00:53:35 +00:00
Add padding to pairs to right-align key
This commit is contained in:
parent
cc3392022e
commit
f37105af5e
2 changed files with 8 additions and 2 deletions
|
@ -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)
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue