Skip to content
Browse files

Added the ability to provide multi-paragraph descriptions.

Also trims trailing whitespace which revealed issues in some of the features (features
were wrong; there shouldn't be trailing whitespace)
  • Loading branch information...
1 parent 0b25b42 commit 7195d1d8b19c588b7a721166d16e71683ed7182c @gshutler gshutler committed with
Showing with 131 additions and 27 deletions.
  1. +1 −1 features/gli_executable.feature
  2. +2 −2 features/gli_init.feature
  3. +20 −22 lib/support/help.rb
  4. +108 −2 test/tc_command.rb
View
2 features/gli_executable.feature
@@ -43,7 +43,7 @@ Feature: The GLI executable works as intended
init [command options] project_name [command[ command]*]
Create a new GLI-based project
- This will create a scaffold command line project that uses GLI for command
+ This will create a scaffold command line project that uses GLI for command
line processing. Specifically, this will create an executable ready to go,
as well as a lib and test directory, all inside the directory named for your
project
View
4 features/gli_init.feature
@@ -49,7 +49,7 @@ Feature: The scaffold GLI generates works
Version: 0.0.1
Global Options:
- -f, --flagname=The name of the argument - Describe some flag here (default:
+ -f, --flagname=The name of the argument - Describe some flag here (default:
the default)
--help - Show this message
-s, --switch - Describe some switch here
@@ -69,7 +69,7 @@ Feature: The scaffold GLI generates works
Version: 0.0.1
Global Options:
- -f, --flagname=The name of the argument - Describe some flag here (default:
+ -f, --flagname=The name of the argument - Describe some flag here (default:
the default)
--help - Show this message
-s, --switch - Describe some switch here
View
42 lib/support/help.rb
@@ -150,32 +150,30 @@ def wrap(line,pad_length=0,line_length=nil)
line_length = Terminal.instance.size[0]
end
line_padding = sprintf("%#{pad_length}s",'')
- words = line.split(/\s+/)
- return line if !words || words.empty?
- wrapped = ''
- while wrapped.length + line_padding.length < line_length
- wrapped += ' ' if wrapped.length > 0
- word = words.shift
- if (wrapped.length + line_padding.length + word.length > line_length)
- words.unshift word
- break;
- end
- wrapped += word
- return wrapped if words.empty?
+ paragraphs = line.split("\n\n").map { |l| l.chomp }.reject { |l| l.empty? }
+ wrapped = paragraphs.map do |para|
+ paragraph_lines(para, line_length - pad_length).join("\n#{line_padding}")
end
- wrapped += "\n"
- this_line = line_padding
- words.each do |word|
- if this_line.length + word.length >= line_length
- wrapped += this_line
- wrapped += "\n"
- this_line = line_padding + word
+ wrapped.join("\n\n#{line_padding}")
+ end
+
+ # Creates lines of the given column length using the words from the
+ # provided paragraph.
+ def paragraph_lines(paragraph,line_length)
+ lines = []
+ this_line = ''
+
+ paragraph.split(/\s+/).each do |word|
+ if this_line.length + word.length + 1 > line_length
+ lines << this_line
+ this_line = word
else
- this_line += ' ' if this_line.length > line_padding.length
+ this_line += ' ' unless this_line.empty?
this_line += word
end
end
- wrapped.chomp!
- wrapped + "\n" + this_line
+
+ lines << this_line
end
+
end
View
110 test/tc_command.rb
@@ -52,6 +52,61 @@ def setup
GLI.command [:test_wrap] do |c|
c.action {}
end
+ GLI.desc "Testing paragraph help wrapping"
+ GLI.long_desc <<-EOS
+ This will create a scaffold command line project that uses GLI
+ for command line processing. Specifically, this will create
+ an executable ready to go, as well as a lib and test directory, all
+ inside the directory named for your project.
+
+ However, it should maintain the whitespace between paragraphs even
+ when fixing the wrapping.
+ EOS
+ GLI.command [:test_para] do |c|
+ c.action {}
+ end
+
+ GLI.desc "Testing paragraph help wrapping - multiple lines between paragraphs"
+ GLI.long_desc <<-EOS
+ This will create a scaffold command line project that uses GLI
+ for command line processing. Specifically, this will create
+ an executable ready to go, as well as a lib and test directory, all
+ inside the directory named for your project.
+
+
+
+ However, it should maintain the whitespace between paragraphs even
+ when fixing the wrapping. Ignoring multiple blank lines.
+ EOS
+ GLI.command [:test_para_multi] do |c|
+ c.action {}
+ end
+
+ GLI.desc "Testing line and paragraph wrapping - bounds checks"
+ GLI.long_desc <<-EOS
+ Many short sentences
+ Trailing spaces
+ Leading spaces
+ Multi spaces
+ Single line
+
+ 77wide lobortis elit arcu fringilla dictum. Pellentesque volutpat felis vitae
+
+ 76wide lobortis elit arcu fringilla dictum. Pellentesque volutpat felis vita
+
+ Roll over properly with no trailing spaces a a a a a a a a a a a a a a a a a a
+ 74wide lobortis elit arcu fringilla dictum. Pellentesque volutpat felis vi
+
+ 75wide lobortis elit arcu fringilla dictum. Pellentesque volutpat felis vit
+
+ 75wide lobortis elit arcu fringilla dictum. Pellentesque volutpat felis vit
+ 77wide lobortis elit arcu fringilla dictum. Pellentesque volutpat felis vitae
+ 76wide lobortis elit arcu fringilla dictum. Pellentesque volutpat felis vita
+ EOS
+ GLI.command [:test_para_bounds] do |c|
+ c.action {}
+ end
+
@fake_stdout = FakeStdOut.new
@fake_stderr = FakeStdOut.new
DefaultHelpCommand.output_device=@fake_stdout
@@ -247,6 +302,57 @@ def test_long_help_wrapping
end
end
+ def test_paragraph_help_wrapping
+ ENV['COLUMNS'] = '80'
+ ENV['LINES'] = '24'
+ args = %w(help test_para)
+ GLI.run(args)
+ @fake_stdout.strings.each do |str|
+ lines = str.split("\n")
+ lines.each do |line|
+ assert(line.size <= ENV['COLUMNS'].to_i,
+ "Help message should not exceed #{ENV['COLUMNS']} columns, but was #{line.size}")
+ end
+ end
+ total_lines = @fake_stdout.strings.inject(0) { |total, string| total + string.split("\n").size }
+ expected_lines = 9
+ assert_equal expected_lines, total_lines, "Help message should maintain paragraphs and span #{expected_lines} lines, but spanned #{total_lines} lines"
+ end
+
+ def test_paragraph_help_wrapping_multi_line
+ ENV['COLUMNS'] = '80'
+ ENV['LINES'] = '24'
+ args = %w(help test_para_multi)
+ GLI.run(args)
+ @fake_stdout.strings.each do |str|
+ lines = str.split("\n")
+ lines.each do |line|
+ assert(line.size <= ENV['COLUMNS'].to_i,
+ "Help message should not exceed #{ENV['COLUMNS']} columns, but was #{line.size}")
+ end
+ end
+ total_lines = @fake_stdout.strings.inject(0) { |total, string| total + string.split("\n").size }
+ expected_lines = 9
+ assert_equal expected_lines, total_lines, "Help message should maintain paragraphs, ignoring multiple blank lines, and span #{expected_lines} lines, but spanned #{total_lines} lines"
+ end
+
+ def test_paragraph_wrapping_bounds
+ ENV['COLUMNS'] = '80'
+ ENV['LINES'] = '24'
+ args = %w(help test_para_bounds)
+ GLI.run(args)
+ @fake_stdout.strings.each do |str|
+ lines = str.split("\n")
+ lines.each do |line|
+ assert(line.size <= ENV['COLUMNS'].to_i,
+ "Help message should not exceed #{ENV['COLUMNS']} columns, but was #{line.size}")
+ end
+ end
+ total_lines = @fake_stdout.strings.inject(0) { |total, string| total + string.split("\n").size }
+ expected_lines = 18
+ assert_equal expected_lines, total_lines, "Help message should maintain paragraphs, ignoring varying line lengths, multiple spaces and span #{expected_lines} lines, but spanned #{total_lines} lines"
+ end
+
def test_version
GLI.command :foo, :bar do |c|; end
GLI.command :ls, :list do |c|; end
@@ -269,8 +375,8 @@ def test_help_completion
GLI.command :ls, :list do |c|; end
args = %w(help -c)
GLI.run(args)
- assert_equal 8,@fake_stdout.strings.size
- assert_equal ['bar','basic','bs','foo','help','list','ls', 'test_wrap'],@fake_stdout.strings
+ assert_equal 11,@fake_stdout.strings.size
+ assert_equal ['bar','basic','bs','foo','help','list','ls', 'test_para', 'test_para_bounds', 'test_para_multi', 'test_wrap'],@fake_stdout.strings
end
def test_help_completion_partial

0 comments on commit 7195d1d

Please sign in to comment.
Something went wrong with that request. Please try again.