Review changes
diff --git a/lib/src/usage.dart b/lib/src/usage.dart
index 9898b44..cc84e6c 100644
--- a/lib/src/usage.dart
+++ b/lib/src/usage.dart
@@ -176,11 +176,14 @@
numHelpLines = 0;
}
- /// Wrap a single line of text into lines no longer than length.
- /// Try to split at whitespace, but if that's not good enough to keep it
- /// under the length, then split in the middle of a word.
- List<String> wrap(String text, int length) {
- assert(length > 0, 'Wrap length must be larger than zero.');
+ // Wrap a single line of text into lines no longer than maxLineLength,
+ // starting at the "start" column.
+ // Try to split at whitespace, but if that's not good enough to keep it
+ // under the maxLineLength, then split in the middle of a word.
+ List<String> _wrap(String text, int start) {
+ assert(start >= 0);
+ const int minColumnWidth = 10;
+ final int length = max(maxLineLength - start, minColumnWidth);
text = text.trim();
if (text.length <= length) {
return [text];
@@ -210,15 +213,15 @@
}
void write(int column, String text) {
- const int minColumnWidth = 10;
var lines = text.split('\n');
+ // If we are writing the last column, word wrap it to fit.
if (column == columnWidths.length && maxLineLength != null) {
var wrappedLines = <String>[];
- var start = 0;
- columnWidths.sublist(0, column).forEach((int width) => start += width);
+ int start = columnWidths
+ .sublist(0, column)
+ .reduce((int start, int width) => start += width);
for (var line in lines) {
- wrappedLines
- .addAll(wrap(line, max(maxLineLength - start, minColumnWidth)));
+ wrappedLines.addAll(_wrap(line, start));
}
lines = wrappedLines;
}
@@ -313,17 +316,17 @@
bool isWhitespace(String text, int index) {
final int rune = text.codeUnitAt(index);
- return ((rune >= 0x0009 && rune <= 0x000D) ||
+ return rune >= 0x0009 && rune <= 0x000D ||
rune == 0x0020 ||
rune == 0x0085 ||
rune == 0x00A0 ||
rune == 0x1680 ||
rune == 0x180E ||
- (rune >= 0x2000 && rune <= 0x200A) ||
+ rune >= 0x2000 && rune <= 0x200A ||
rune == 0x2028 ||
rune == 0x2029 ||
rune == 0x202F ||
rune == 0x205F ||
rune == 0x3000 ||
- rune == 0xFEFF);
+ rune == 0xFEFF;
}
diff --git a/test/usage_test.dart b/test/usage_test.dart
index 3bd228c..d7ee3dc 100644
--- a/test/usage_test.dart
+++ b/test/usage_test.dart
@@ -370,43 +370,65 @@
parser.addFlag('solid',
help:
'The-flag-with-no-whitespace-that-will-be-wrapped-by-splitting-a-word.');
+ parser.addFlag('longWhitespace',
+ help: ' The flag with a really long help text and whitespace at the start.');
+ parser.addFlag('longTrailspace',
+ help: 'The flag with a really long help text and whitespace at the end. ');
parser.addFlag('small1', help: ' a ');
parser.addFlag('small2', help: ' a');
parser.addFlag('small3', help: 'a ');
validateUsage(parser, '''
- --[no-]long The flag
- with a
- really
- long help
- text that
- will be
- wrapped.
+ --[no-]long The flag
+ with a
+ really
+ long help
+ text that
+ will be
+ wrapped.
- --[no-]longNewline The flag
- with a
- really
- long help
- text and
- newlines
-
- that will
- still be
- wrapped
- because it
- is really
- long.
+ --[no-]longNewline The flag
+ with a
+ really
+ long help
+ text and
+ newlines
+
+ that will
+ still be
+ wrapped
+ because it
+ is really
+ long.
- --[no-]solid The-flag-w
- ith-no-whi
- tespace-th
- at-will-be
- -wrapped-b
- y-splittin
- g-a-word.
+ --[no-]solid The-flag-w
+ ith-no-whi
+ tespace-th
+ at-will-be
+ -wrapped-b
+ y-splittin
+ g-a-word.
- --[no-]small1 a
- --[no-]small2 a
- --[no-]small3 a
+ --[no-]longWhitespace The flag
+ with a
+ really
+ long help
+ text and
+ whitespace
+ at the
+ start.
+
+ --[no-]longTrailspace The flag
+ with a
+ really
+ long help
+ text and
+ whitespace
+ at the
+ end.
+
+ --[no-]small1 a
+ --[no-]small2 a
+ --[no-]small3 a
''');
});