class OutputMode::Outputs::Tabulated
Attributes
@!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
@!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
@!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
@!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
@!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
@!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
@!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
@!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
@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
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
@return [Hash] additional options to TTY::Table
renderer @see github.com/piotrmurach/tty-table#33-options
# File lib/output_mode/outputs/tabulated.rb, line 47 def config; super; end
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
# File lib/output_mode/outputs/tabulated.rb, line 80 def has_header? callables.any? { |c| c.config.key?(:header) } end
# File lib/output_mode/outputs/tabulated.rb, line 117 def pastel @pastel ||= Pastel::Color.new(enabled: colorize) end
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
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