Sha256: 5a1feb348945a002ee8105944ed1e74f67d991ae7c6a2dd5511ecbafcf19ed8d

Contents?: true

Size: 1.24 KB

Versions: 8

Compression:

Stored size: 1.24 KB

Contents

# frozen_string_literal: true

require 'stringio'
require 'forwardable'

module DiverDown
  class Web
    class IndentedStringIo
      extend ::Forwardable

      def_delegators :@io, :rewind, :string

      attr_accessor :indent

      # @param tab [String]
      def initialize(tab: '  ')
        @io = StringIO.new
        @indent = 0
        @tab = tab
      end

      # @param contents [Array<String>]
      # @param indent [Boolean] Enable or disable indentation
      # @return [void]
      def write(*contents, indent: true)
        indent_string = if indent
                          @tab * @indent
                        else
                          ''
                        end

        string = contents.join
        lines = string.lines
        lines.each do |line|
          if line == "\n"
            @io.write "\n"
          else
            @io.write "#{indent_string}#{line}"
          end
        end
      end

      # @param content [String]
      # @return [void]
      def puts(*contents, indent: true)
        write("#{contents.join("\n")}\n", indent:)
        nil
      end

      # increase the indent level for the block
      def indented
        @indent += 1
        yield
      ensure
        @indent -= 1
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
diver_down-0.0.1.alpha17 lib/diver_down/web/indented_string_io.rb
diver_down-0.0.1.alpha16 lib/diver_down/web/indented_string_io.rb
diver_down-0.0.1.alpha15 lib/diver_down/web/indented_string_io.rb
diver_down-0.0.1.alpha14 lib/diver_down/web/indented_string_io.rb
diver_down-0.0.1.alpha13 lib/diver_down/web/indented_string_io.rb
diver_down-0.0.1.alpha3 lib/diver_down/web/indented_string_io.rb
diver_down-0.0.1.alpha2 lib/diver_down/web/indented_string_io.rb
diver_down-0.0.1.alpha1 lib/diver_down/web/indented_string_io.rb