lib/output_mode/formatter.rb in output_mode-1.7.0 vs lib/output_mode/formatter.rb in output_mode-1.7.1
- old
+ new
@@ -26,36 +26,33 @@
require 'tty-color'
module OutputMode
class Formatter
- def self.constructor(&block)
- @constructor ||= block
- end
-
def self.build(*objects, **opts)
- new(*objects, **opts).tap do |policy|
- next unless constructor
- policy.instance_exec(&constructor)
- end
+ new(*objects, **opts).tap(&:register_all)
end
def self.render(*objects, **opts)
build(*objects, **opts).render
end
- def initialize(*objects, verbose: nil, ascii: nil, interactive: nil, color: nil)
+ def initialize(*objects, verbose: nil, ascii: nil, humanize: nil, color: nil)
@verbose = verbose
@ascii = ascii
- @interactive = interactive
+ @humanize = humanize
@color = color
# NOTE: This is intentionally not exposed on the base class
# It is up to the individual implementations to expose it
@objects = objects
end
+ # Override in sub-classes to define the attributes
+ def register_all
+ end
+
def callables
@callables ||= Callables.new
end
def register(**config, &block)
@@ -87,39 +84,39 @@
def render
build_output.render(*@objects)
end
- def interactive?
- if @interactive.nil?
+ def humanize?
+ if @humanize.nil?
$stdout.tty?
else
- @interactive
+ @humanize
end
end
def color?
- if @color.nil? && (ascii? || !interactive?)
+ if @color.nil? && (ascii? || !humanize?)
false
elsif @color.nil?
TTY::Color.color?
else
@color
end
end
def ascii?
if @ascii.nil?
- !interactive?
+ !humanize?
else
@ascii
end
end
def verbose?
if @verbose.nil?
- !interactive?
+ !humanize?
else
@verbose
end
end
@@ -133,10 +130,10 @@
@no ? @no : (ascii? ? 'no' : '✕')
end
def default(value = nil)
@default = value unless value.nil?
- @default ? @default : (interactive? ? '(none)' : '')
+ @default ? @default : (humanize? ? '(none)' : '')
end
def time(value = nil)
@time = value unless value.nil?
@time ? @time : (verbose? ? "%Y-%m-%dT%H:%M:%S%:z" : "%d/%m/%y %H:%M")