diff --git a/humanfriendly/tables.py b/humanfriendly/tables.py index 3a32ba3..a980c16 100644 --- a/humanfriendly/tables.py +++ b/humanfriendly/tables.py @@ -261,7 +261,7 @@ def format_robust_table(data, column_names): blocks.append(lines) # Calculate the width of the row delimiter. num_rows, num_columns = find_terminal_size() - longest_line = max(max(map(ansi_width, lines)) for lines in blocks) + longest_line = max([ansi_width(line) for lines in blocks for line in lines] or [0]) delimiter = u"\n%s\n" % ('-' * min(longest_line, num_columns)) # Force a delimiter at the start and end of the table. blocks.insert(0, "") diff --git a/humanfriendly/tests.py b/humanfriendly/tests.py index 72dad99..ad0a589 100644 --- a/humanfriendly/tests.py +++ b/humanfriendly/tests.py @@ -130,6 +130,13 @@ class HumanFriendlyTestCase(TestCase): """Container for the `humanfriendly` test suite.""" + def test_empty_robust_table(self): + """Empty query results produce no rows instead of raising ValueError.""" + from humanfriendly.tables import format_robust_table + assert format_robust_table([], ['name', 'value']) == '' + assert format_robust_table(iter([]), ['name']) == '' + assert format_robust_table([[]], []) == '' + def test_case_insensitive_dict(self): """Test the CaseInsensitiveDict class.""" # Test support for the dict(iterable) signature.