Skip to content

Commit

Permalink
feat: Add text_truncation_chars and number_truncation_chars configura…
Browse files Browse the repository at this point in the history
…tions, #738
  • Loading branch information
jpmckinney committed Oct 18, 2023
1 parent 01f094b commit 9e79a33
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 6 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
1.9.0 - October 17, 2023
------------------------

* feat: Add a ``text_truncation_chars`` configuration for values that exceed ``max_column_width`` in :meth:`.Table.print_table` and :meth:`.Table.print_html`.
* feat: Add a ``number_truncation_chars`` configuration for values that exceed ``max_precision`` in :meth:`.Table.print_table` and :meth:`.Table.print_html`.

1.8.0 - October 10, 2023
------------------------

Expand Down
8 changes: 8 additions & 0 deletions agate/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@
+-------------------------+------------------------------------------+-----------------------------------------+
| ellipsis_chars | Characters to render for ellipsis | '...' |
+-------------------------+------------------------------------------+-----------------------------------------+
| text_truncation_chars | Characters for truncated text values | '...' |
+-------------------------+------------------------------------------+-----------------------------------------+
| number_truncation_chars | Characters for truncated number values | '…' |
+-------------------------+------------------------------------------+-----------------------------------------+
"""

Expand All @@ -50,6 +54,10 @@
'tick_char': '+',
#: Characters to render for ellipsis
'ellipsis_chars': '...',
#: Characters for truncated text values
'text_truncation_chars': '...',
#: Characters for truncated number values
'number_truncation_chars': '…',
}


Expand Down
4 changes: 3 additions & 1 deletion agate/table/print_html.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ def print_html(self, max_rows=20, max_columns=6, output=sys.stdout, max_column_w
max_precision = float('inf')

ellipsis = config.get_option('ellipsis_chars')
truncation = config.get_option('text_truncation_chars')
len_truncation = len(truncation)
locale = locale or config.get_option('default_locale')

rows_truncated = max_rows < len(self._rows)
Expand Down Expand Up @@ -93,7 +95,7 @@ def print_html(self, max_rows=20, max_columns=6, output=sys.stdout, max_column_w
v = str(v)

if max_column_width is not None and len(v) > max_column_width:
v = '%s...' % v[:max_column_width - 3]
v = '%s%s' % (v[:max_column_width - len_truncation], truncation)

formatted_row.append(v)

Expand Down
6 changes: 4 additions & 2 deletions agate/table/print_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ def print_table(self, max_rows=20, max_columns=6, output=sys.stdout, max_column_
max_precision = float('inf')

ellipsis = config.get_option('ellipsis_chars')
truncation = config.get_option('text_truncation_chars')
len_truncation = len(truncation)
h_line = config.get_option('horizontal_line_char')
v_line = config.get_option('vertical_line_char')
locale = locale or config.get_option('default_locale')
Expand All @@ -54,7 +56,7 @@ def print_table(self, max_rows=20, max_columns=6, output=sys.stdout, max_column_
column_names = []
for column_name in self.column_names[:max_columns]:
if max_column_width is not None and len(column_name) > max_column_width:
column_names.append('%s...' % column_name[:max_column_width - 3])
column_names.append('%s%s' % (column_name[:max_column_width - len_truncation], truncation))
else:
column_names.append(column_name)

Expand Down Expand Up @@ -102,7 +104,7 @@ def print_table(self, max_rows=20, max_columns=6, output=sys.stdout, max_column_
v = str(v)

if max_column_width is not None and len(v) > max_column_width:
v = '%s...' % v[:max_column_width - 3]
v = '%s%s' % (v[:max_column_width - len_truncation], truncation)

if len(v) > widths[j]:
widths[j] = len(v)
Expand Down
3 changes: 2 additions & 1 deletion agate/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

from slugify import slugify as pslugify

from agate import config
from agate.warns import warn_duplicate_column, warn_unnamed_column

#: Sentinal for use when `None` is an valid argument value
Expand Down Expand Up @@ -161,7 +162,7 @@ def make_number_formatter(decimal_places, add_ellipsis=False):
Optionally add an ellipsis symbol at the end of a number
"""
fraction = '0' * decimal_places
ellipsis = '…' if add_ellipsis else ''
ellipsis = config.get_option('number_truncation_chars') if add_ellipsis else ''
return ''.join(['#,##0.', fraction, ellipsis, ';-#,##0.', fraction, ellipsis])


Expand Down
2 changes: 1 addition & 1 deletion docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

project = 'agate'
copyright = '2017, Christopher Groskopf'
version = '1.8.0'
version = '1.9.0'
release = version

# -- General configuration ---------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setup(
name='agate',
version='1.8.0',
version='1.9.0',
description='A data analysis library that is optimized for humans instead of machines.',
long_description=long_description,
long_description_content_type='text/x-rst',
Expand Down

0 comments on commit 9e79a33

Please sign in to comment.