Sha256: 4d5ab2a51bf9f736fe830cd2eceb399d056f812a4718138ff77f51428e8ae329

Contents?: true

Size: 1.66 KB

Versions: 3

Compression:

Stored size: 1.66 KB

Contents

module CommandKit
  module Printing
    #
    # 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
    #
    module Indent
      #
      # Initializes the indententation level to zero.
      #
      def initialize(**kwargs)
        @indent = 0

        super(**kwargs)
      end

      #
      # 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 indententation level will be returned.
      #
      def indent(n=2)
        if block_given?
          begin
            original_indent = @indent
            @indent += n

            yield
          ensure
            @indent = original_indent
          end
        else
          @indent
        end
      end

      #
      # Indents and prints the lines to stdout.
      #
      # @param [Array<String>] lines
      #   The lines to indent and print.
      #
      def puts(*lines)
        if (@indent > 0 && !lines.empty?)
          padding = " " * @indent

          super(*lines.map { |line| "#{padding}#{line}" })
        else
          super(*lines)
        end
      end

    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
command_kit-0.1.0.rc1 lib/command_kit/printing/indent.rb
command_kit-0.1.0.pre2 lib/command_kit/printing/indent.rb
command_kit-0.1.0.pre1 lib/command_kit/printing/indent.rb