Skip to content

Commit

Permalink
Fixes #10 : handle printing empty table
Browse files Browse the repository at this point in the history
get_table_width now returns 0 on empty table
get_string now return empty string on empty table
printing empty row now prints an empty string
  • Loading branch information
pri22296 committed May 16, 2017
1 parent ca21e37 commit aa56328
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 18 deletions.
29 changes: 20 additions & 9 deletions beautifultable/beautifultable.py
Expand Up @@ -654,7 +654,7 @@ def pop_column(self, index=-1):
if self._column_count == 1:
# This is the last column. So we should clear the table to avoid
# empty rows
self.clear(clear_column_properties=True)
self.clear(clear_metadata=True)
else:
# Not the last column. safe to pop from row
self._column_count -= 1
Expand Down Expand Up @@ -835,19 +835,19 @@ def append_column(self, header, column):
"""
self.insert_column(self._column_count, header, column)

def clear(self, clear_column_properties=False):
def clear(self, clear_metadata=False):
"""Clear the contents of the table.
Clear all rows of the table, and if specified clears all column specific data.
Parameters
----------
clear_column_properties : bool, optional
clear_metadata : bool, optional
If it is true(default False), all metadata of columns such as their alignment,
padding, width, etc. are also cleared and number of columns is set to 0.
"""
self._table.clear()
if clear_column_properties:
if clear_metadata:
self._initialize_table(0)

def _get_horizontal_line(self, char):
Expand All @@ -874,24 +874,31 @@ def _get_horizontal_line(self, char):
line = list(char * (int(table_width/len(char)) + 1))[:table_width]
except ZeroDivisionError:
line = [' '] * table_width
# Only if Special Intersection is enabled and horizontal line is visible

if len(line) == 0:
return ''
# Only if Special Intersection is enabled and horizontal line is
# visible
if self.intersection_char and not char.isspace():
# If left border is enabled and it is visible
if self.left_border_char and not self.left_border_char.isspace():
length = min(len(self.left_border_char), len(self.intersection_char))
length = min(len(self.left_border_char),
len(self.intersection_char))
for i in range(length):
line[i] = intersection[i]
# If right border is enabled and it is visible
if self.right_border_char and not self.right_border_char.isspace():
length = min(len(self.right_border_char), len(self.intersection_char))
length = min(len(self.right_border_char),
len(self.intersection_char))
for i in range(length):
line[-i-1] = intersection[-i-1]
# If column seperator is enabled and it is visible
if self.column_seperator_char and not self.column_seperator_char.isspace():
index = len(self.left_border_char)
for i in range(self._column_count-1):
index += (self._column_widths[i])
length = min(len(self.column_seperator_char), len(self.intersection_char))
length = min(len(self.column_seperator_char),
len(self.intersection_char))
for i in range(length):
line[index+i] = intersection[i]
index += len(self.column_seperator_char)
Expand Down Expand Up @@ -956,8 +963,9 @@ def get_table_width(self):
int
Width of the table as number of characters.
"""
if self.column_count == 0:
return 0
width = sum(self._column_widths)

width += ((self._column_count - 1)
* len(self.column_seperator_char))
width += len(self.left_border_char)
Expand All @@ -984,6 +992,9 @@ def get_string(self, recalculate_width=True):
if recalculate_width or sum(self._column_widths) == 0:
self.auto_calculate_width()

if len(self._table) == 0:
return ''

string_ = []

if self.top_border_char:
Expand Down
19 changes: 10 additions & 9 deletions beautifultable/rows.py
Expand Up @@ -102,15 +102,16 @@ def __str__(self):
except ValueError:
row[i] = str(row[i])
string = []
list_of_rows = self._get_row_within_width(row)
for row_ in list_of_rows:
for i in range(table.column_count):
row_[i] = '{:{align}{width}}'.format(
str(row_[i]), align=align[i].value, width=width[i])
content = table.column_seperator_char.join(row_)
content = table.left_border_char + content
content += table.right_border_char
string.append(content)
if len(row) > 0:
list_of_rows = self._get_row_within_width(row)
for row_ in list_of_rows:
for i in range(table.column_count):
row_[i] = '{:{align}{width}}'.format(
str(row_[i]), align=align[i].value, width=width[i])
content = table.column_seperator_char.join(row_)
content = table.left_border_char + content
content += table.right_border_char
string.append(content)
return '\n'.join(string)


Expand Down

0 comments on commit aa56328

Please sign in to comment.