From 6d6021d0043dcd132ce6073cd8efb96565ed6ef4 Mon Sep 17 00:00:00 2001 From: Mateeh Ullah Date: Mon, 26 Oct 2026 16:36:40 +0500 Subject: [PATCH 1/2] fix: handle middle truncation boundary widths (#722) --- fire/formatting.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/fire/formatting.py b/fire/formatting.py index 68484c279..e6ff04e21 100644 --- a/fire/formatting.py +++ b/fire/formatting.py @@ -81,12 +81,13 @@ def EllipsisMiddleTruncate(text, available_space, line_length): """Truncates text from the middle with ellipsis.""" if available_space < len(ELLIPSIS): available_space = line_length - if len(text) < available_space: + if len(text) <= available_space: return text available_string_len = available_space - len(ELLIPSIS) first_half_len = int(available_string_len / 2) # start from middle second_half_len = available_string_len - first_half_len - return text[:first_half_len] + ELLIPSIS + text[-second_half_len:] + return (text[:first_half_len] + ELLIPSIS + + (text[-second_half_len:] if second_half_len else '')) def DoubleQuote(text): From e929212848f7572f0193c8ed858d35e743d4bddb Mon Sep 17 00:00:00 2001 From: Mateeh Ullah Date: Mon, 26 Oct 2026 16:36:52 +0500 Subject: [PATCH 2/2] test: cover middle truncation edge cases (#722) --- fire/formatting_test.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/fire/formatting_test.py b/fire/formatting_test.py index e0f6699d2..a87e913b1 100644 --- a/fire/formatting_test.py +++ b/fire/formatting_test.py @@ -67,6 +67,18 @@ def test_ellipsis_middle_truncate(self): text=text, available_space=7, line_length=LINE_LENGTH) self.assertEqual('10...0L', truncated_text) + def test_ellipsis_middle_truncate_ellipsis_only(self): + self.assertEqual('...', formatting.EllipsisMiddleTruncate( + 'abcdefghij', available_space=3, line_length=LINE_LENGTH)) + + def test_ellipsis_middle_truncate_exact_fit(self): + self.assertEqual('abcd', formatting.EllipsisMiddleTruncate( + 'abcd', available_space=4, line_length=LINE_LENGTH)) + + def test_ellipsis_middle_truncate_short_width(self): + self.assertEqual('...j', formatting.EllipsisMiddleTruncate( + 'abcdefghij', available_space=4, line_length=LINE_LENGTH)) + def test_ellipsis_middle_truncate_not_enough_space(self): text = '1000000000L' truncated_text = formatting.EllipsisMiddleTruncate(