Class: GLI::Commands::HelpModules::ListFormatter

Inherits:
Object
  • Object
show all
Defined in:
lib/gli/commands/help_modules/list_formatter.rb

Overview

Given a list of two-element lists, formats on the terminal

Instance Method Summary collapse

Constructor Details

#initialize(list, wrapper_class = TextWrapper) ⇒ ListFormatter

Returns a new instance of ListFormatter.



6
7
8
9
# File 'lib/gli/commands/help_modules/list_formatter.rb', line 6

def initialize(list,wrapper_class=TextWrapper)
  @list = list
  @wrapper_class = wrapper_class
end

Instance Method Details

#output(output_device) ⇒ Object

Output the list to the output_device



12
13
14
15
16
17
18
19
# File 'lib/gli/commands/help_modules/list_formatter.rb', line 12

def output(output_device)
  return if @list.empty?
  max_width = @list.map { |_| _[0].length }.max
  wrapper = @wrapper_class.new(Terminal.instance.size[0],4 + max_width + 3)
  @list.each do |(name,description)|
    output_device.printf("    %-#{max_width}s - %s\n",name,wrapper.wrap(String(description).strip))
  end
end