Sha256: 158bada3943c7906318c5363a1195e60812f4d586ceb6794f641f7fe61dcb237

Contents?: true

Size: 1.77 KB

Versions: 6

Compression:

Stored size: 1.77 KB

Contents

# typed: strict
# frozen_string_literal: true

require "stringio"

module Spoom
  class Printer
    extend T::Sig
    extend T::Helpers

    abstract!

    sig { returns(T.any(IO, StringIO)) }
    attr_accessor :out

    sig { params(out: T.any(IO, StringIO), colors: T::Boolean, indent_level: Integer).void }
    def initialize(out: $stdout, colors: true, indent_level: 0)
      @out = out
      @colors = colors
      @indent_level = indent_level
    end

    # Increase indent level
    sig { void }
    def indent
      @indent_level += 2
    end

    # Decrease indent level
    sig { void }
    def dedent
      @indent_level -= 2
    end

    # Print `string` into `out`
    sig { params(string: T.nilable(String)).void }
    def print(string)
      return unless string
      @out.print(string)
    end

    # Print `string` colored with `color` into `out`
    #
    # Does not use colors unless `@colors`.
    sig { params(string: T.nilable(String), color: Symbol, colors: Symbol).void }
    def print_colored(string, color, *colors)
      return unless string
      string = colorize(string, color)
      colors.each { |c| string = colorize(string, c) }
      @out.print(string)
    end

    # Print a new line into `out`
    sig { void }
    def printn
      print("\n")
    end

    # Print `string` with indent and newline
    sig { params(string: T.nilable(String)).void }
    def printl(string)
      return unless string
      printt
      print(string)
      printn
    end

    # Print an indent space into `out`
    sig { void }
    def printt
      print(" " * @indent_level)
    end

    # Colorize `string` with color if `@colors`
    sig { params(string: String, color: Symbol).returns(String) }
    def colorize(string, color)
      @colors ? string.colorize(color) : string
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
spoom-1.1.4 lib/spoom/printer.rb
spoom-1.1.3 lib/spoom/printer.rb
spoom-1.1.2 lib/spoom/printer.rb
spoom-1.1.1 lib/spoom/printer.rb
spoom-1.1.0 lib/spoom/printer.rb
spoom-1.0.9 lib/spoom/printer.rb