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..d1273fc5 100644 --- a/fire/formatting_test.py +++ b/fire/formatting_test.py @@ -49,6 +49,15 @@ def test_wrap_multiple_items(self): 'chicken |', 'cheese'], lines) + def test_wrap_oversized_first_item(self): + self.assertEqual(['long_command'], + formatting.WrappedJoin(['long_command'], width=5)) + self.assertEqual(['first |', 'next'], + formatting.WrappedJoin(['first', 'next'], width=5)) + + def test_wrap_empty_items(self): + self.assertEqual([''], formatting.WrappedJoin([], width=5)) + def test_ellipsis_truncate(self): text = 'This is a string' truncated_text = formatting.EllipsisTruncate(