Sha256: eada7cbaf568884cada7211bbb76ee8b50a8bd209e7d88511ff484842c0edf0d

Contents?: true

Size: 1.78 KB

Versions: 8

Compression:

Stored size: 1.78 KB

Contents

# typed: strict
# frozen_string_literal: true

require "stringio"

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

    include Colorize

    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: Color).void }
    def print_colored(string, *color)
      return unless string

      string = T.unsafe(self).colorize(string, *color)
      @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: Spoom::Color).returns(String) }
    def colorize(string, *color)
      return string unless @colors

      T.unsafe(self).set_color(string, *color)
    end
  end
end

Version data entries

8 entries across 8 versions & 2 rubygems

Version Path
spoom-1.2.1 lib/spoom/printer.rb
spoom-1.2.0 lib/spoom/printer.rb
spoom-1.1.16 lib/spoom/printer.rb
devcycle-ruby-server-sdk-2.0.0 vendor/bundle/ruby/3.0.0/gems/spoom-1.1.15/lib/spoom/printer.rb
spoom-1.1.15 lib/spoom/printer.rb
spoom-1.1.14 lib/spoom/printer.rb
spoom-1.1.13 lib/spoom/printer.rb
spoom-1.1.12 lib/spoom/printer.rb