mirror of
https://gitea.invidious.io/iv-org/shard-spectator.git
synced 2024-08-15 00:53:35 +00:00
Create indentation guide type
This commit is contained in:
parent
e722cc4f1f
commit
66c1dc3bce
1 changed files with 45 additions and 0 deletions
45
src/spectator/formatting/indent.cr
Normal file
45
src/spectator/formatting/indent.cr
Normal file
|
@ -0,0 +1,45 @@
|
|||
module Spectator::Formatting
|
||||
# Tracks indentation for text output.
|
||||
# To use, create an instance and call `#increase` when a block should be indented.
|
||||
# The `#increase` method yields, so additional `#increase` and `#line` methods can be called.
|
||||
# Then call `#line` to produce a line of text at the current indent.
|
||||
# ```
|
||||
# indent = Indent.new(io)
|
||||
# indent.increase do
|
||||
# indent.line("Text")
|
||||
# indent.increase do
|
||||
# indent.line("More text")
|
||||
# end
|
||||
# end
|
||||
# ```
|
||||
private struct Indent
|
||||
# Default number of spaces to indent by.
|
||||
INDENT_SIZE = 2
|
||||
|
||||
# Creates the identation tracker.
|
||||
# The *io* is the stream to output to.
|
||||
# The *indent_size* is how much (number of spaces) to indent at each level.
|
||||
# The *initial_indent* is what the ident should be set to.
|
||||
def initialize(@io : IO, @indent_size = INDENT_SIZE, @inital_indent = 0)
|
||||
end
|
||||
|
||||
# Indents the text and yields.
|
||||
def increase(&block)
|
||||
increase(@indent_size, &block)
|
||||
end
|
||||
|
||||
# Indents the text by a specified amount and yields.
|
||||
def increase(amount) : Nil
|
||||
@indent_size += amount
|
||||
yield
|
||||
ensure
|
||||
@indent_size -= amount
|
||||
end
|
||||
|
||||
# Produces a line of indented text.
|
||||
def line(text)
|
||||
@indent_size.times { io << ' ' }
|
||||
@io.puts text
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Reference in a new issue