Module: CommandKit::Printing::Indent
Overview
Adds the ability to automatically indent all calls to puts
.
Examples
include Printing::Indent
def run
puts "regular output:"
indent(4) do
puts "indented output"
puts "..."
end
puts "back to regular output"
end
Instance Method Summary collapse
-
#indent(n = 2) { ... } ⇒ Integer
Increases the indentation level by two, yields, then restores the indentation level.
-
#initialize(**kwargs) ⇒ Object
Initializes the indentation level to zero.
-
#puts(*lines) ⇒ Object
Indents and prints the lines to stdout.
Instance Method Details
#indent(n = 2) { ... } ⇒ Integer
Increases the indentation level by two, yields, then restores the indentation level.
66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/command_kit/printing/indent.rb', line 66 def indent(n=2) if block_given? original_indent = @indent begin @indent += n yield ensure @indent = original_indent end else @indent end end |
#initialize(**kwargs) ⇒ Object
Initializes the indentation level to zero.
25 26 27 28 29 |
# File 'lib/command_kit/printing/indent.rb', line 25 def initialize(**kwargs) @indent = 0 super(**kwargs) end |
#puts(*lines) ⇒ Object
Indents and prints the lines to stdout.
89 90 91 92 93 94 95 96 97 |
# File 'lib/command_kit/printing/indent.rb', line 89 def puts(*lines) if (@indent > 0 && !lines.empty?) padding = " " * @indent super(*lines.map { |line| "#{padding}#{line}" }) else super(*lines) end end |