Sha256: f1324a3475c01c534c02bf5906c36f8c743a3f25036631706cc45bad5b09e5c4

Contents?: true

Size: 1.99 KB

Versions: 6

Compression:

Stored size: 1.99 KB

Contents

module Overcommit
  # Encapsulates all communication to an output source.
  class Logger
    # Helper for creating a logger which outputs nothing.
    def self.silent
      new(File.open('/dev/null', 'w'))
    end

    # Creates a logger that will write to the given output stream.
    #
    # @param out [IO]
    def initialize(out)
      @out = out
    end

    # Write output without a trailing newline.
    def partial(*args)
      @out.print(*args)
    end

    # Prints a newline character (alias for readability).
    def newline
      log
    end

    # Write a line of output.
    #
    # A newline character will always be appended.
    def log(*args)
      @out.puts(*args)
    end

    # Write a line of output that is intended to be emphasized.
    def bold(*args)
      color('1', *args)
    end

    # Write a line of output indicating a problem or error.
    def error(*args)
      color(31, *args)
    end

    # Write a line of output indicating a problem or error which is emphasized
    # over a regular problem or error.
    def bold_error(*args)
      color('1;31', *args)
    end

    # Write a line of output indicating a successful or noteworthy event.
    def success(*args)
      color(32, *args)
    end

    # Write a line of output indicating a potential cause for concern, but not
    # an actual error.
    def warning(*args)
      color(33, *args)
    end

    # Write a line of output indicating a potential cause for concern, but with
    # greater emphasize compared to other warnings.
    def bold_warning(*args)
      color('1;33', *args)
    end

    private

    # Outputs text wrapped in ANSI escape code necessary to produce a given
    # color/text display.
    #
    # @param code [String] ANSI escape code, e.g. '1;33' for "bold yellow"
    # @param str [String] string to wrap
    # @param partial [true,false] whether to omit a newline
    def color(code, str, partial = false)
      send(partial ? :partial : :log,
           @out.tty? ? "\033[#{code}m#{str}\033[0m" : str)
    end
  end
end

Version data entries

6 entries across 6 versions & 2 rubygems

Version Path
overcommit-0.24.0 lib/overcommit/logger.rb
overcommit-0.23.0 lib/overcommit/logger.rb
overcommit-0.22.0 lib/overcommit/logger.rb
jawshooah-overcommit-0.22.0 lib/overcommit/logger.rb
overcommit-0.21.0 lib/overcommit/logger.rb
overcommit-0.20.0 lib/overcommit/logger.rb