Module: OutputMode::TLDR::Show

Includes:
BuilderDSL
Defined in:
lib/output_mode/tldr/show.rb

Instance Method Summary collapse

Methods included from BuilderDSL

#output_callables

Instance Method Details

#build_output(verbose: nil, ascii: nil, interactive: nil, context: {}, template: OutputMode::DEFAULT_ERB, non_interactive_template: OutputMode::NON_INTERACTIVE_ERB) ⇒ Object

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 print to the io directly

The ascii flag disables the unicode formatting in interactive shells. Non interactive shells use ASCII by default.

The verbose flag toggles the simplified and verbose outputs in the interactive output. Non-interactive outputs are always verbose

If $stdout is an interactive shell (aka a TTY), then it will display using Outputs::Templated. This is intended for human consumption and will obey the provided verbose flag.

If $stdout is non-interactive, then it will display using Outputs::Delimited using tab delimiters. This is intended for consumption by machines. This output ignores the provided verbose flag as it is always verbose.

The template overrides the default erb template for interactive sessions. The non_interactive_template overrides the template for non-interactive sessions.

An interative/ non-interactive output can be forced by setting the interactive flag to true/false respectively



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/output_mode/tldr/show.rb', line 78

def build_output(verbose: nil, ascii: nil, interactive: nil, context: {},
                 template: OutputMode::DEFAULT_ERB,
                 non_interactive_template: OutputMode::NON_INTERACTIVE_ERB)
  # 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

  # Define the templating parameters
  opts =  if ascii && interactive
            { yes: 'yes', no: 'no', colorize: false, default: '(none)', template: template }
          elsif interactive
            { yes: '', no: '', colorize: TTY::Color.color?, default: '(none)', template: template }
          else
            { yes: 'yes', no: 'no', colorize: false, default: '', template: non_interactive_template }
          end

  sections = callables.map { |o| o.config[:section] }

  Outputs::Templated.new(*callables,
                         fields: callables.map { |c| c.config.fetch(:header, 'missing') },
                         sections: sections,
                         context: context,
                         **opts)
end

#register_callable(header: , verbose: true) {|model| ... } ⇒ Object Also known as: register_attribute

Register a field when displaying a model

Parameters:

  • header: (defaults to: )

    The human readable key to the field, uses the term ‘header’ for consistency

  • verbose: (defaults to: true)

    Whether the field will be shown in the verbose output

  • interactive:

    Whether the field will be show in the interactive output

  • section:

    Define the grouping a callable belongs to. Ignored by default

  • modes:

    Additional modes flags for the callable

Yield Parameters:

  • model

    The subject the column is describing, some sort of data model



44
45
46
47
48
49
50
# File 'lib/output_mode/tldr/show.rb', line 44

def register_callable(modes: {}, header:, verbose: nil, interactive: nil, section: :default, &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