module CommandKit::Printing::Indent

Adds the ability to automatically indent all calls to `puts`.

## Examples

include Printing::Indent

def main
  puts "regular output:"

  indent(4) do
    puts "indented output"
    puts "..."
  end

  puts "back to regular output"
end

Public Class Methods

new(**kwargs) click to toggle source

Initializes the indentation level to zero.

Calls superclass method
# File lib/command_kit/printing/indent.rb, line 25
def initialize(**kwargs)
  @indent = 0

  super(**kwargs)
end

Public Instance Methods

indent(n=2) { || ... } click to toggle source

Increases the indentation level by two, yields, then restores the indentation level.

@param [Integer] n

How much to increase the indentation level by.

@yield []

The given block will be called after the indentation level has been
increased.

@return [Integer]

If no block is given, the indentation level will be returned.

@example

puts "values:"
indent do
  values.each do |key,value|
    puts "#{key}: #{value}"
  end
end

@example

puts "Code:"
puts
puts "```"
indent(4) do
  code.each_line do |line|
    puts line
  end
end
puts "```"

@api public

# File lib/command_kit/printing/indent.rb, line 66
def indent(n=2)
  if block_given?
    begin
      original_indent = @indent
      @indent += n

      yield
    ensure
      @indent = original_indent
    end
  else
    @indent
  end
end
puts(*lines) click to toggle source

Indents and prints the lines to stdout.

@param [Array<String>] lines

The lines to indent and print.

@api public

Calls superclass method
# 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