2014-08-10 22:09:29 +00:00
|
|
|
module Mutant
|
|
|
|
class Reporter
|
|
|
|
class CLI
|
|
|
|
# CLI output format
|
|
|
|
class Format
|
|
|
|
include AbstractType, Anima.new(:tty)
|
|
|
|
|
2015-07-03 15:21:39 +00:00
|
|
|
# Start representation
|
2014-08-10 22:33:36 +00:00
|
|
|
#
|
2015-11-16 01:07:31 +00:00
|
|
|
# @param [Env::Bootstrap] env
|
2014-08-10 22:33:36 +00:00
|
|
|
#
|
|
|
|
# @return [String]
|
|
|
|
abstract_method :start
|
|
|
|
|
2015-07-03 15:21:39 +00:00
|
|
|
# Progress representation
|
2014-08-10 22:09:29 +00:00
|
|
|
#
|
2014-10-23 11:37:53 +00:00
|
|
|
# @param [Runner::Status] status
|
2014-08-10 22:09:29 +00:00
|
|
|
#
|
|
|
|
# @return [String]
|
|
|
|
abstract_method :progress
|
|
|
|
|
2015-07-03 15:21:39 +00:00
|
|
|
# Report delay in seconds
|
2014-12-08 18:13:49 +00:00
|
|
|
#
|
|
|
|
# @return [Float]
|
|
|
|
def delay
|
|
|
|
self.class::REPORT_DELAY
|
|
|
|
end
|
|
|
|
|
2014-08-10 22:09:29 +00:00
|
|
|
# Output abstraction to decouple tty? from buffer
|
|
|
|
class Output
|
|
|
|
include Concord.new(:tty, :buffer)
|
|
|
|
|
|
|
|
# Test if output is a tty
|
|
|
|
#
|
|
|
|
# @return [Boolean]
|
2015-10-31 03:59:29 +00:00
|
|
|
alias_method :tty?, :tty
|
|
|
|
public :tty?
|
2014-08-10 22:09:29 +00:00
|
|
|
|
2015-05-31 20:44:09 +00:00
|
|
|
%i[puts write].each do |name|
|
2014-08-10 22:09:29 +00:00
|
|
|
define_method(name) do |*args, &block|
|
|
|
|
buffer.public_send(name, *args, &block)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end # Output
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
# Format object with printer
|
|
|
|
#
|
|
|
|
# @param [Class:Printer] printer
|
|
|
|
# @param [Object] object
|
|
|
|
#
|
|
|
|
# @return [String]
|
|
|
|
def format(printer, object)
|
|
|
|
buffer = new_buffer
|
2015-05-03 00:21:30 +00:00
|
|
|
printer.call(Output.new(tty, buffer), object)
|
2014-08-10 22:09:29 +00:00
|
|
|
buffer.rewind
|
|
|
|
buffer.read
|
|
|
|
end
|
|
|
|
|
2014-08-10 22:33:36 +00:00
|
|
|
# Format for progressive non rewindable output
|
|
|
|
class Progressive < self
|
|
|
|
|
2014-12-08 18:13:49 +00:00
|
|
|
REPORT_FREQUENCY = 1.0
|
|
|
|
REPORT_DELAY = 1 / REPORT_FREQUENCY
|
2014-10-23 11:37:53 +00:00
|
|
|
|
2015-07-03 15:21:39 +00:00
|
|
|
# Start representation
|
2014-08-10 22:33:36 +00:00
|
|
|
#
|
|
|
|
# @return [String]
|
|
|
|
def start(env)
|
|
|
|
format(Printer::Config, env.config)
|
|
|
|
end
|
|
|
|
|
2015-07-03 15:21:39 +00:00
|
|
|
# Progress representation
|
2014-08-10 22:33:36 +00:00
|
|
|
#
|
|
|
|
# @return [String]
|
2014-10-23 11:37:53 +00:00
|
|
|
def progress(status)
|
2014-12-08 18:13:49 +00:00
|
|
|
format(Printer::StatusProgressive, status)
|
2014-08-10 22:33:36 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2015-07-03 15:21:39 +00:00
|
|
|
# New buffer
|
2014-08-10 22:33:36 +00:00
|
|
|
#
|
|
|
|
# @return [StringIO]
|
|
|
|
def new_buffer
|
|
|
|
StringIO.new
|
|
|
|
end
|
|
|
|
|
|
|
|
end # Progressive
|
|
|
|
|
2014-08-10 22:09:29 +00:00
|
|
|
# Format for framed rewindable output
|
|
|
|
class Framed < self
|
|
|
|
include anima.add(:tput)
|
|
|
|
|
|
|
|
BUFFER_FLAGS = 'a+'.freeze
|
|
|
|
|
2014-12-08 18:13:49 +00:00
|
|
|
REPORT_FREQUENCY = 20.0
|
|
|
|
REPORT_DELAY = 1 / REPORT_FREQUENCY
|
|
|
|
|
2014-08-10 22:09:29 +00:00
|
|
|
# Format start
|
|
|
|
#
|
2015-11-16 01:07:31 +00:00
|
|
|
# @param [Env::Bootstrap] env
|
2014-08-10 22:09:29 +00:00
|
|
|
#
|
|
|
|
# @return [String]
|
|
|
|
def start(_env)
|
|
|
|
tput.prepare
|
|
|
|
end
|
|
|
|
|
|
|
|
# Format progress
|
|
|
|
#
|
2014-10-23 11:37:53 +00:00
|
|
|
# @param [Runner::Status] status
|
2014-08-10 22:09:29 +00:00
|
|
|
#
|
|
|
|
# @return [String]
|
2014-10-23 11:37:53 +00:00
|
|
|
def progress(status)
|
|
|
|
format(Printer::Status, status)
|
2014-08-10 22:09:29 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2015-07-03 15:21:39 +00:00
|
|
|
# New buffer
|
2014-08-10 22:09:29 +00:00
|
|
|
#
|
|
|
|
# @return [StringIO]
|
|
|
|
def new_buffer
|
2015-11-18 15:06:22 -08:00
|
|
|
# For some reason this raises an Errno::EACCESS error:
|
2014-08-10 22:09:29 +00:00
|
|
|
#
|
|
|
|
# StringIO.new(Tput::INSTANCE.restore, BUFFER_FLAGS)
|
|
|
|
#
|
2014-08-10 22:16:53 +00:00
|
|
|
buffer = StringIO.new
|
2014-08-10 22:09:29 +00:00
|
|
|
buffer << tput.restore
|
|
|
|
end
|
|
|
|
|
|
|
|
end # Framed
|
|
|
|
end # Format
|
|
|
|
end # CLI
|
|
|
|
end # Reporter
|
|
|
|
end # Mutant
|