Module: TTY::Option::UsageWrapper

Included in:
AggregateErrors, Formatter
Defined in:
lib/tty/option/usage_wrapper.rb

Class Method Summary collapse

Class Method Details

.wrap(text, width: 80, indent: 2, indent_first: false) ⇒ Object

Wrap a string to a maximum width with indentation

Parameters:

  • text (String)
  • width (Integer) (defaults to: 80)
  • indent (Integer) (defaults to: 2)
  • indent_first (Boolean) (defaults to: false)


14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/tty/option/usage_wrapper.rb', line 14

def wrap(text, width: 80, indent: 2, indent_first: false)
  wrap = width - indent
  lines = []
  indentation = " " * indent

  line, rest = *next_line(text, wrap: wrap)
  lines << (indent_first ? indentation : "") + line

  while !rest.nil?
    line, rest = *next_line(rest, wrap: wrap)
    lines << indentation + line.strip
  end

  lines.join("\n")
end