From 9e79a33504bb97ab5c60a214ee6918f6ea8aa1b3 Mon Sep 17 00:00:00 2001 From: James McKinney <26463+jpmckinney@users.noreply.github.com> Date: Tue, 17 Oct 2023 23:51:44 -0400 Subject: [PATCH] feat: Add text_truncation_chars and number_truncation_chars configurations, #738 --- CHANGELOG.rst | 6 ++++++ agate/config.py | 8 ++++++++ agate/table/print_html.py | 4 +++- agate/table/print_table.py | 6 ++++-- agate/utils.py | 3 ++- docs/conf.py | 2 +- setup.py | 2 +- 7 files changed, 25 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 3b80f421..e69ba537 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -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 ------------------------ diff --git a/agate/config.py b/agate/config.py index b20b275e..f79ee875 100644 --- a/agate/config.py +++ b/agate/config.py @@ -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 | '…' | ++-------------------------+------------------------------------------+-----------------------------------------+ """ @@ -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': '…', } diff --git a/agate/table/print_html.py b/agate/table/print_html.py index 610516a2..41c0837b 100644 --- a/agate/table/print_html.py +++ b/agate/table/print_html.py @@ -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) @@ -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) diff --git a/agate/table/print_table.py b/agate/table/print_table.py index 3490b1e0..d066488a 100644 --- a/agate/table/print_table.py +++ b/agate/table/print_table.py @@ -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') @@ -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) @@ -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) diff --git a/agate/utils.py b/agate/utils.py index fc290c18..69bb01eb 100644 --- a/agate/utils.py +++ b/agate/utils.py @@ -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 @@ -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]) diff --git a/docs/conf.py b/docs/conf.py index fd23c0bb..5907e574 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -12,7 +12,7 @@ project = 'agate' copyright = '2017, Christopher Groskopf' -version = '1.8.0' +version = '1.9.0' release = version # -- General configuration --------------------------------------------------- diff --git a/setup.py b/setup.py index 8d77a5f8..9b1c5383 100644 --- a/setup.py +++ b/setup.py @@ -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',