module OutputMode::TLDR::Index

Public Instance Methods

build_output(verbose: nil, ascii: nil, interactive: nil, header_color: [:blue, :bold], row_color: :green, context: {}) click to toggle source

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 output 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 {OutputMode::Outputs::Tabulated}. This is intended for human consumption and will obey the provided verbose flag.

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

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

# File lib/output_mode/tldr/index.rb, line 74
def build_output(verbose: nil, ascii: nil, interactive: nil, header_color: [:blue, :bold], row_color: :green, 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', renderer: :ascii }
           else
             {
               yes: '✓', no: '✕', renderer: :unicode, colorize: TTY::Color.color?,
               header_color: header_color,
               row_color: row_color
             }
           end

    Outputs::Tabulated.new(*callables,
                           rotate: false,
                           padding: [0,1],
                           default: '(none)',
                           context: context,
                           **opts
                           )
  else
    # Creates the machine readable output
    Outputs::Delimited.new(*callables, col_sep: "\t", yes: 'yes', no: 'no', default: nil,
                          context: context)
  end
end
register_callable(modes: {}, header:, verbose: nil, interactive: nil, header_color: nil, row_color: nil, &b) click to toggle source

Register a new column in the Index table @overload register_callable(header:, verbose: true)

@param header: The column's header field when displaying to humans
@param verbose: Whether the column will be shown in the verbose output
@param interactive: Whether the field will be show in the interactive output
@param header_color: Override the default color for the header
@param row_color: Override the default color for the row
@param modes: Additional modes flags for the callable
@yieldparam model The subject the column is describing, some sort of data model
# File lib/output_mode/tldr/index.rb, line 43
def register_callable(modes: {}, header:, verbose: nil, interactive: nil, header_color: nil, row_color: nil, &b)
  modes = modes.map { |m| [m, true] }.to_h if modes.is_a? Array
  super(modes: modes.merge(verbose: verbose, interactive: interactive),
        header: header,
        header_color: header_color,
        row_color: row_color,
        &b)
end
Also aliased as: register_column
register_column(modes: {}, header:, verbose: nil, interactive: nil, header_color: nil, row_color: nil, &b)
Alias for: register_callable