free_mutant/lib/mutant/reporter/cli/printer.rb

101 lines
2.3 KiB
Ruby
Raw Normal View History

module Mutant
class Reporter
class CLI
# CLI runner status printer base class
class Printer
2015-05-03 00:21:30 +00:00
include AbstractType, Delegator, Adamantium::Flat, Concord.new(:output, :object), Procto.call(:run)
2015-05-03 00:21:30 +00:00
private_class_method :new
2014-10-23 11:37:53 +00:00
2015-05-03 00:21:30 +00:00
delegate :success?
2015-05-03 00:21:30 +00:00
NL = "\n".freeze
# Run printer
#
# @return [self]
abstract_method :run
private
# Status color
#
# @return [Color]
2014-05-11 12:39:12 +00:00
def status_color
success? ? Color::GREEN : Color::RED
end
# Visit a collection of objects
#
2014-07-17 13:59:25 +00:00
# @return [Class::Printer] printer
# @return [Enumerable<Object>] collection
#
# @return [undefined]
2014-07-17 13:59:25 +00:00
def visit_collection(printer, collection)
collection.each do |object|
visit(printer, object)
end
end
# Visit object
#
2014-07-17 13:59:25 +00:00
# @param [Class::Printer] printer
# @param [Object] object
#
# @return [undefined]
2014-07-17 13:59:25 +00:00
def visit(printer, object)
2015-05-03 00:21:30 +00:00
printer.call(output, object)
end
# Print an info line to output
#
# @return [undefined]
def info(string, *arguments)
2014-05-27 15:12:36 +00:00
puts(format(string, *arguments))
end
# Print a status line to output
#
# @return [undefined]
def status(string, *arguments)
2014-05-27 15:12:36 +00:00
puts(colorize(status_color, format(string, *arguments)))
end
# Print a line to output
#
# @return [undefined]
2014-07-17 13:59:25 +00:00
def puts(string)
output.puts(string)
end
# Colorize message
#
# @param [Color] color
# @param [String] message
#
# @return [String]
# if color is enabled
# unmodified message otherwise
def colorize(color, message)
color = Color::NONE unless tty?
color.format(message)
end
2014-07-17 13:59:25 +00:00
# Test if output is a tty
#
2014-06-15 19:27:57 +00:00
# @return [Boolean]
def tty?
2014-07-17 13:59:25 +00:00
output.tty?
end
2014-07-17 13:59:25 +00:00
# Test if output can be colored
#
# @return [Boolean]
2015-12-20 19:25:03 +00:00
#
# @api private
2014-07-17 13:59:25 +00:00
alias_method :color?, :tty?
end # Printer
end # CLI
end # Reporter
end # Mutant