lib/output_mode/tldr/show.rb in output_mode-1.4.0 vs lib/output_mode/tldr/show.rb in output_mode-1.5.0

- old
+ new

@@ -33,14 +33,20 @@ # Register a field when displaying a model # @overload register_callable(header:, verbose: true) # @param header: The human readable key to the field, uses the term 'header' for consistency # @param verbose: Whether the field will be shown in the verbose output + # @param interactive: Whether the field will be show in the interactive output # @param section: Define the grouping a callable belongs to. Ignored by default + # @param modes: Additional modes flags for the callable # @yieldparam model The subject the column is describing, some sort of data model - def register_callable(header:, verbose: nil, section: :other, &b) - super(modes: { verbose: verbose }, header: header, section: section, &b) + def register_callable(modes: {}, header:, verbose: nil, interactive: nil, section: :other, &b) + modes = modes.map { |m| [m, true] }.to_h if modes.is_a? Array + super(modes: modes.merge(verbose: verbose, interactive: interactive), + header: header, + section: section, + &b) end alias_method :register_attribute, :register_callable # Creates an new +output+ from the verbosity flag. This method only uses # +$stdout+ as part of it's output class discovery logic. It does not @@ -63,23 +69,35 @@ # # The +template+ overrides the default erb template for the output # # An interative/ non-interactive output can be forced by setting the # +interactive+ flag to +true+/+false+ respectively - def build_output(verbose: nil, ascii: false, interactive: nil, template: nil) + def build_output(verbose: nil, ascii: nil, interactive: nil, template: nil, context: {}) # Set the interactive and verbose flags if not provided interactive = $stdout.tty? if interactive.nil? verbose = !interactive if verbose.nil? + ascii = !interactive if ascii.nil? + # Update the rendering context with the verbosity/interactive settings + context = context.merge(interactive: interactive, verbose: verbose, ascii: ascii) + callables = if verbose # Filter out columns that are explicitly not verbose output_callables.select { |o| o.verbose?(true) } else # Filter out columns that are explicitly verbose output_callables.reject(&:verbose?) end + callables = if interactive + # Filter out columns that are explicitly not interactive + callables.select { |o| o.interactive?(true) } + else + # Filter out columns that are explicitly interactive + callables.reject { |o| o.interactive? } + end + if interactive # Creates the human readable output opts = if ascii { yes: 'yes', no: 'no', colorize: false } else @@ -91,13 +109,15 @@ Outputs::Templated.new(*callables, fields: callables.map { |c| c.config.fetch(:header, 'missing') }, default: '(none)', sections: sections, template: template, + context: context, **opts) else # Creates the machine readable output - Outputs::Delimited.new(*callables, col_sep: "\t", yes: 'yes', no: 'no', default: '') + Outputs::Delimited.new(*callables, col_sep: "\t", yes: 'yes', no: 'no', default: nil, + context: context) end end end end end