Module: Pvectl::Formatters::OutputHelper

Defined in:
lib/pvectl/formatters/output_helper.rb

Overview

Facade for output formatting in commands.

Coordinates Formatter and Presenter to produce formatted output. Handles color flag interpretation and prints to stdout.

Examples:

Usage in a command

def self.execute(global_options)
  service = Pvectl::Services::Vm.new
  vms = service.list
  presenter = Pvectl::Presenters::Vm.new

  OutputHelper.print(
    data: vms,
    presenter: presenter,
    format: global_options[:output],
    color_flag: global_options[:color]
  )
end

Rendering without printing (for testing)

output = OutputHelper.render(
  data: vms,
  presenter: presenter,
  format: "json"
)

Class Method Summary collapse

Class Method Details

This method returns an undefined value.

Formats data and prints to stdout.

Parameters:

  • data (Array, Object)

    collection of models or single model

  • presenter (Presenters::Base)

    presenter for the resource type

  • format (String) (defaults to: "table")

    output format (“table”, “json”, “yaml”, “wide”)

  • color_flag (Boolean, nil) (defaults to: nil)

    color flag from CLI

    • true: –color was passed

    • false: –no-color was passed

    • nil: auto-detect based on TTY

  • describe (Boolean) (defaults to: false)

    whether this is a describe command (default: false)

  • context (Hash)

    additional context passed to presenter



45
46
47
48
# File 'lib/pvectl/formatters/output_helper.rb', line 45

def print(data:, presenter:, format: "table", color_flag: nil, describe: false, **context)
  output = render(data: data, presenter: presenter, format: format, color_flag: color_flag, describe: describe, **context)
  puts output
end

.render(data:, presenter:, format: "table", color_flag: nil, describe: false, **context) ⇒ String

Returns formatted string without printing.

Useful for testing or when you need to manipulate the output before displaying.

Parameters:

  • data (Array, Object)

    collection of models or single model

  • presenter (Presenters::Base)

    presenter for the resource type

  • format (String) (defaults to: "table")

    output format (“table”, “json”, “yaml”, “wide”)

  • color_flag (Boolean, nil) (defaults to: nil)

    color flag from CLI

  • describe (Boolean) (defaults to: false)

    whether this is a describe command

  • context (Hash)

    additional context passed to presenter

Returns:

  • (String)

    formatted output



62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/pvectl/formatters/output_helper.rb', line 62

def render(data:, presenter:, format: "table", color_flag: nil, describe: false, **context)
  formatter = Registry.for(format)
  color_enabled = ColorSupport.enabled?(explicit_flag: color_flag)

  formatter.format(
    data,
    presenter,
    color_enabled: color_enabled,
    describe: describe,
    **context
  )
end