class Overcommit::Logger

Encapsulates all communication to an output source.

Public Class Methods

new(out) click to toggle source

Creates a logger that will write to the given output stream.

@param out [IO]

# File lib/overcommit/logger.rb, line 14
def initialize(out)
  @out = out
  @colorize =
    if ENV.key?('OVERCOMMIT_COLOR')
      !%w[0 false no].include?(ENV['OVERCOMMIT_COLOR'])
    else
      @out.tty?
    end
end
silent() click to toggle source

Helper for creating a logger which outputs nothing.

# File lib/overcommit/logger.rb, line 7
def self.silent
  new(File.open(File::NULL, 'w'))
end

Public Instance Methods

bold(*args) click to toggle source

Write a line of output that is intended to be emphasized.

# File lib/overcommit/logger.rb, line 47
def bold(*args)
  color('1', *args)
end
bold_error(*args) click to toggle source

Write a line of output indicating a problem or error which is emphasized over a regular problem or error.

# File lib/overcommit/logger.rb, line 58
def bold_error(*args)
  color('1;31', *args)
end
bold_warning(*args) click to toggle source

Write a line of output indicating a potential cause for concern, but with greater emphasize compared to other warnings.

# File lib/overcommit/logger.rb, line 75
def bold_warning(*args)
  color('1;33', *args)
end
debug(*args) click to toggle source

Write a line of output if debug mode is enabled.

# File lib/overcommit/logger.rb, line 42
def debug(*args)
  color('35', *args) unless ENV.fetch('OVERCOMMIT_DEBUG') { '' }.empty?
end
error(*args) click to toggle source

Write a line of output indicating a problem or error.

# File lib/overcommit/logger.rb, line 52
def error(*args)
  color(31, *args)
end
log(*args) click to toggle source

Write a line of output.

A newline character will always be appended.

# File lib/overcommit/logger.rb, line 37
def log(*args)
  @out.puts(*args)
end
newline() click to toggle source

Prints a newline character (alias for readability).

# File lib/overcommit/logger.rb, line 30
def newline
  log
end
partial(*args) click to toggle source

Write output without a trailing newline.

# File lib/overcommit/logger.rb, line 25
def partial(*args)
  @out.print(*args)
end
success(*args) click to toggle source

Write a line of output indicating a successful or noteworthy event.

# File lib/overcommit/logger.rb, line 63
def success(*args)
  color(32, *args)
end
warning(*args) click to toggle source

Write a line of output indicating a potential cause for concern, but not an actual error.

# File lib/overcommit/logger.rb, line 69
def warning(*args)
  color(33, *args)
end

Private Instance Methods

color(code, str, partial = false) click to toggle source

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

# File lib/overcommit/logger.rb, line 87
def color(code, str, partial = false)
  send(partial ? :partial : :log,
       @colorize ? "\033[#{code}m#{str}\033[0m" : str)
end