2013-06-21 17:52:53 +02:00
|
|
|
module Mutant
|
|
|
|
class Reporter
|
|
|
|
class CLI
|
2013-06-21 23:52:57 +02:00
|
|
|
# CLI runner status printer base class
|
2013-06-21 17:52:53 +02:00
|
|
|
class Printer
|
2015-05-03 00:21:30 +00:00
|
|
|
include AbstractType, Delegator, Adamantium::Flat, Concord.new(:output, :object), Procto.call(:run)
|
2013-06-21 23:52:57 +02:00
|
|
|
|
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?
|
2014-08-10 22:09:29 +00:00
|
|
|
|
2015-05-03 00:21:30 +00:00
|
|
|
NL = "\n".freeze
|
2013-06-21 17:52:53 +02:00
|
|
|
|
2014-05-12 13:48:15 +00:00
|
|
|
# Run printer
|
2013-09-07 14:08:07 +02:00
|
|
|
#
|
2014-05-12 13:48:15 +00:00
|
|
|
# @return [self]
|
2013-06-21 23:52:57 +02:00
|
|
|
abstract_method :run
|
|
|
|
|
2013-06-21 17:52:53 +02:00
|
|
|
private
|
|
|
|
|
2015-07-03 15:21:39 +00:00
|
|
|
# Status color
|
2013-06-21 23:52:57 +02:00
|
|
|
#
|
|
|
|
# @return [Color]
|
2014-05-11 12:39:12 +00:00
|
|
|
def status_color
|
2013-06-21 23:52:57 +02:00
|
|
|
success? ? Color::GREEN : Color::RED
|
|
|
|
end
|
|
|
|
|
2014-07-03 21:16:12 +00:00
|
|
|
# Visit a collection of objects
|
|
|
|
#
|
2014-07-17 13:59:25 +00:00
|
|
|
# @return [Class::Printer] printer
|
2014-07-03 21:16:12 +00:00
|
|
|
# @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
|
2014-07-03 21:16:12 +00:00
|
|
|
end
|
|
|
|
|
2013-07-05 00:54:50 +02:00
|
|
|
# Visit object
|
|
|
|
#
|
2014-07-17 13:59:25 +00:00
|
|
|
# @param [Class::Printer] printer
|
2013-07-05 00:54:50 +02:00
|
|
|
# @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)
|
2013-07-05 00:54:50 +02:00
|
|
|
end
|
|
|
|
|
2013-06-21 23:52:57 +02:00
|
|
|
# Print an info line to output
|
|
|
|
#
|
|
|
|
# @return [undefined]
|
|
|
|
def info(string, *arguments)
|
2014-05-27 15:12:36 +00:00
|
|
|
puts(format(string, *arguments))
|
2013-06-21 23:52:57 +02:00
|
|
|
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)))
|
2013-06-21 23:52:57 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
# Print a line to output
|
|
|
|
#
|
|
|
|
# @return [undefined]
|
2014-07-17 13:59:25 +00:00
|
|
|
def puts(string)
|
2013-06-21 17:52:53 +02:00
|
|
|
output.puts(string)
|
|
|
|
end
|
|
|
|
|
2013-06-21 23:52:57 +02:00
|
|
|
# Colorize message
|
|
|
|
#
|
|
|
|
# @param [Color] color
|
|
|
|
# @param [String] message
|
|
|
|
#
|
|
|
|
# @return [String]
|
2013-09-11 20:48:44 +02:00
|
|
|
# if color is enabled
|
|
|
|
# unmodified message otherwise
|
2013-06-21 23:52:57 +02:00
|
|
|
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
|
2013-06-21 23:52:57 +02:00
|
|
|
#
|
2014-06-15 19:27:57 +00:00
|
|
|
# @return [Boolean]
|
2013-06-21 23:52:57 +02:00
|
|
|
def tty?
|
2014-07-17 13:59:25 +00:00
|
|
|
output.tty?
|
2013-06-21 23:52:57 +02:00
|
|
|
end
|
2013-06-21 17:52:53 +02:00
|
|
|
|
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?
|
2013-06-21 17:52:53 +02:00
|
|
|
end # Printer
|
|
|
|
end # CLI
|
|
|
|
end # Reporter
|
|
|
|
end # Mutant
|