33 lines
1.0 KiB
Crystal
33 lines
1.0 KiB
Crystal
module Spectator::Formatting::Components
|
|
# Base type for handling indented output.
|
|
# Indents are tracked and automatically printed.
|
|
# Use `#indent` to increase the indent for the duration of a block.
|
|
# Use `#line` to produce a line with an indentation prefixing it.
|
|
abstract struct Block
|
|
# Default indent amount.
|
|
private INDENT = 2
|
|
|
|
# Creates the block.
|
|
# A default *indent* size can be specified.
|
|
def initialize(*, @indent : Int32 = INDENT)
|
|
end
|
|
|
|
# Increases the indent by the a specific *amount* for the duration of the block.
|
|
private def indent(amount = INDENT, &)
|
|
@indent += amount
|
|
yield
|
|
@indent -= amount
|
|
end
|
|
|
|
# Produces a line of output with an indent before it.
|
|
# The contents of the line should be generated by a block provided to this method.
|
|
# Ensure that _only_ one line is produced by the block,
|
|
# otherwise the indent will be lost.
|
|
private def line(io, &)
|
|
@indent.times { io << ' ' }
|
|
yield
|
|
io.puts
|
|
end
|
|
end
|
|
end
|