Skip to content

Commit

Permalink
Change to use configurable width for usage display
Browse files Browse the repository at this point in the history
  • Loading branch information
piotrmurach committed May 2, 2020
1 parent 31617c4 commit 595bec5
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
10 changes: 6 additions & 4 deletions lib/tty/option/formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
module TTY
module Option
class Formatter
include UsageWrapper

SHORT_OPT_LENGTH = 4
DEFAULT_WIDTH = 80
NEWLINE = "\n"
Expand Down Expand Up @@ -111,7 +113,7 @@ def format_usage
output << " [#{@param_display.("environment")}]" if @parameters.environments?
output << " #{format_arguments_usage}" if @parameters.arguments?
output << " #{format_keywords_usage}" if @parameters.keywords?
usage + UsageWrapper.wrap(output.join, indent: usage.length)
usage + wrap(output.join, indent: usage.length, width: width)
end

# Format arguments
Expand Down Expand Up @@ -238,7 +240,7 @@ def format_section_parameter(param, longest_param, name_selector)
desc << default
end

line << UsageWrapper.wrap(desc.join, indent: indent)
line << wrap(desc.join, indent: indent, width: width)
line.join
end

Expand Down Expand Up @@ -310,7 +312,7 @@ def format_option(option, longest_length, any_short)
desc << default
end

line << UsageWrapper.wrap(desc.join, indent: indent)
line << wrap(desc.join, indent: indent, width: width)

line.join
end
Expand Down Expand Up @@ -343,7 +345,7 @@ def format_multiline(lines, indent)
lines.map.with_index do |line, i|
line.map do |part|
part.split(NEWLINE).map do |p|
UsageWrapper.wrap(p, indent: indent, width: width, indent_first: true)
wrap(p, indent: indent, width: width, indent_first: true)
end.join(NEWLINE)
end.join(NEWLINE) + (last_index != i ? NEWLINE : "")
end.join(NEWLINE)
Expand Down
10 changes: 5 additions & 5 deletions spec/unit/help_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -176,19 +176,19 @@ def new_command(&block)
[BAR-SUPER-LONG-ARGUMENT-NAME] [QUX-LONG-NAME]
Arguments:
bar-super-long-argument-name Some description that goes on and on and never
finishes explaining (default
bar-super-long-argument-name Some description that goes on and on and
never finishes explaining (default
"default-is-way-too-long-as-well")
foo-long-argument-name Some multiline
description with newlines
qux-long-name Some description that
breaks into multiline
on newlines (permitted:
one,two,three,four,five,six) (default "some
long default on many lines")
one,two,three,four,five,six) (default
"some long default on many lines")
EOS

expect(cmd.help).to eq(expected_output)
expect(cmd.help(width: 75)).to eq(expected_output)
end

it "formats banner with any arguments and options" do
Expand Down

0 comments on commit 595bec5

Please sign in to comment.