diff --git a/fire/formatting.py b/fire/formatting.py index 68484c27..e5daf684 100644 --- a/fire/formatting.py +++ b/fire/formatting.py @@ -50,13 +50,15 @@ def WrappedJoin(items, separator=' | ', width=80): if len(current_line) + len(item) <= width: current_line += item else: - lines.append(current_line.rstrip()) + if current_line: + lines.append(current_line.rstrip()) current_line = item else: if len(current_line) + len(item) + len(separator) <= width: current_line += item + separator else: - lines.append(current_line.rstrip()) + if current_line: + lines.append(current_line.rstrip()) current_line = item + separator lines.append(current_line) diff --git a/fire/formatting_test.py b/fire/formatting_test.py index e0f6699d..c123829e 100644 --- a/fire/formatting_test.py +++ b/fire/formatting_test.py @@ -49,6 +49,19 @@ def test_wrap_multiple_items(self): 'chicken |', 'cheese'], lines) + def test_wrap_first_item_longer_than_width(self): + self.assertEqual( + ['long_command'], + formatting.WrappedJoin(['long_command'], width=5)) + self.assertEqual( + ['long_command |', 'next'], + formatting.WrappedJoin(['long_command', 'next'], width=5)) + + def test_wrap_first_item_separator_exceeds_width(self): + self.assertEqual( + ['first |', 'next'], + formatting.WrappedJoin(['first', 'next'], width=5)) + def test_ellipsis_truncate(self): text = 'This is a string' truncated_text = formatting.EllipsisTruncate( diff --git a/fire/helptext_test.py b/fire/helptext_test.py index c7098fc4..9b4d6704 100644 --- a/fire/helptext_test.py +++ b/fire/helptext_test.py @@ -430,6 +430,13 @@ def testHelpTextMultipleKeywoardArgumentsWithShortArgs(self): class UsageTest(testutils.BaseTestCase): + def testAvailabilityLineWithLongFirstCommand(self): + # pylint: disable=protected-access + availability_line = helptext._CreateAvailabilityLine( + 'COMMANDS', ['long_command'], line_length=30) + self.assertEqual( + ' COMMANDS' + ' ' * 15 + 'long_command\n', availability_line) + def testUsageOutput(self): component = tc.NoDefaults() t = trace.FireTrace(component, name='NoDefaults')