class OutputMode::Outputs::Tabulated

Attributes

block[R]

@!attribute [r] renderer

@return [Symbol] the renderer type, see: https://github.com/piotrmurach/tty-table#32-renderer

@!attribute [r] block

@return [#call] an optional block of code that configures the renderer

@!attribute [r] colorize

@return [Boolean] enable or disabled the colorization

@!attribute [r] header_color

@return An optional header color or array of colors

@!attribute [r] row_color

@return An optional data color or array of colors
colorize[R]

@!attribute [r] renderer

@return [Symbol] the renderer type, see: https://github.com/piotrmurach/tty-table#32-renderer

@!attribute [r] block

@return [#call] an optional block of code that configures the renderer

@!attribute [r] colorize

@return [Boolean] enable or disabled the colorization

@!attribute [r] header_color

@return An optional header color or array of colors

@!attribute [r] row_color

@return An optional data color or array of colors
default[R]

@!attribute [r] renderer

@return [Symbol] the renderer type, see: https://github.com/piotrmurach/tty-table#32-renderer

@!attribute [r] block

@return [#call] an optional block of code that configures the renderer

@!attribute [r] colorize

@return [Boolean] enable or disabled the colorization

@!attribute [r] header_color

@return An optional header color or array of colors

@!attribute [r] row_color

@return An optional data color or array of colors
header_color[R]

@!attribute [r] renderer

@return [Symbol] the renderer type, see: https://github.com/piotrmurach/tty-table#32-renderer

@!attribute [r] block

@return [#call] an optional block of code that configures the renderer

@!attribute [r] colorize

@return [Boolean] enable or disabled the colorization

@!attribute [r] header_color

@return An optional header color or array of colors

@!attribute [r] row_color

@return An optional data color or array of colors
no[R]

@!attribute [r] renderer

@return [Symbol] the renderer type, see: https://github.com/piotrmurach/tty-table#32-renderer

@!attribute [r] block

@return [#call] an optional block of code that configures the renderer

@!attribute [r] colorize

@return [Boolean] enable or disabled the colorization

@!attribute [r] header_color

@return An optional header color or array of colors

@!attribute [r] row_color

@return An optional data color or array of colors
renderer[R]

@!attribute [r] renderer

@return [Symbol] the renderer type, see: https://github.com/piotrmurach/tty-table#32-renderer

@!attribute [r] block

@return [#call] an optional block of code that configures the renderer

@!attribute [r] colorize

@return [Boolean] enable or disabled the colorization

@!attribute [r] header_color

@return An optional header color or array of colors

@!attribute [r] row_color

@return An optional data color or array of colors
row_color[R]

@!attribute [r] renderer

@return [Symbol] the renderer type, see: https://github.com/piotrmurach/tty-table#32-renderer

@!attribute [r] block

@return [#call] an optional block of code that configures the renderer

@!attribute [r] colorize

@return [Boolean] enable or disabled the colorization

@!attribute [r] header_color

@return An optional header color or array of colors

@!attribute [r] row_color

@return An optional data color or array of colors
yes[R]

@!attribute [r] renderer

@return [Symbol] the renderer type, see: https://github.com/piotrmurach/tty-table#32-renderer

@!attribute [r] block

@return [#call] an optional block of code that configures the renderer

@!attribute [r] colorize

@return [Boolean] enable or disabled the colorization

@!attribute [r] header_color

@return An optional header color or array of colors

@!attribute [r] row_color

@return An optional data color or array of colors

Public Class Methods

new(*procs, renderer: :unicode, colorize: false, header_color: nil, row_color: nil, **config, &block) click to toggle source

@overload initialize(*procs, renderer: nil, **config)

@param [Array] *procs see {OutputMode::Outputs::Base#initialize}
@param [Symbol] :renderer override the default renderer
@param [Hash] **config additional options to the renderer
@yieldparam tty_table_renderer [TTY::Table::Renderer::Base] optional access the underlining TTY::Table renderer
Calls superclass method OutputMode::Output::new
# File lib/output_mode/outputs/tabulated.rb, line 54
def initialize(*procs,
               renderer: :unicode,
               colorize: false,
               header_color: nil,
               row_color: nil,
               **config,
               &block)
  @renderer =  renderer
  @block = block
  @header_color = header_color
  @row_color = row_color
  @colorize = colorize
  super(*procs, **config)
end

Public Instance Methods

config() click to toggle source

@return [Hash] additional options to TTY::Table renderer @see github.com/piotrmurach/tty-table#33-options

Calls superclass method
# File lib/output_mode/outputs/tabulated.rb, line 47
def config; super; end
render(*data) click to toggle source

Implements the render method using TTY::Table @see OutputMode::Outputs::Base#render @see github.com/piotrmurach/tty-table

# File lib/output_mode/outputs/tabulated.rb, line 72
def render(*data)
  table = TTY::Table.new header: processed_header
  data.each { |d| table << process_row(d) }
  table.render(renderer, **config, &block) || ''
end

Private Instance Methods

has_header?() click to toggle source
# File lib/output_mode/outputs/tabulated.rb, line 80
def has_header?
  callables.any? { |c| c.config.key?(:header) }
end
pastel() click to toggle source
# File lib/output_mode/outputs/tabulated.rb, line 117
def pastel
  @pastel ||= Pastel::Color.new(enabled: colorize)
end
process_row(model) click to toggle source

Colorizes the row when requested

# File lib/output_mode/outputs/tabulated.rb, line 102
def process_row(model)
  callables.map do |callable|
    d = callable.generator(self).call(model)
    color = callable.config[:row_color] || row_color
    case color
    when NilClass
      d.to_s
    when Array
      pastel.decorate(d.to_s, *color)
    else
      pastel.decorate(d.to_s, color)
    end
  end
end
processed_header() click to toggle source

Colorizes the header when requested

# File lib/output_mode/outputs/tabulated.rb, line 85
def processed_header
  return nil unless has_header?
  callables.map do |callable|
    header = callable.config.fetch(:header, '')
    color = callable.config.fetch(:header_color, nil) || header_color
    case color
    when nil
      header.to_s
    when Array
      pastel.decorate(header.to_s, *color)
    else
      pastel.decorate(header.to_s, color)
    end
  end
end