From b6b5dd27ccdc8b831205c7c7e35ecd3a32475059 Mon Sep 17 00:00:00 2001 From: JM Lopez Date: Tue, 22 Oct 2019 22:24:37 -0400 Subject: [PATCH] Improving --column-width to 0: auto, -1: disable any other integer will be taken as fixed column width. Default value is -1: disable since truncating columns without user consent might be a little too much. 0: auto does a basic calculation based on the terminal size shutil.get_terminal_size() and the number of columns in the result. --- homeassistant_cli/cli.py | 9 +++------ homeassistant_cli/const.py | 4 +++- homeassistant_cli/helper.py | 13 ++++++++++--- tests/fixtures/basic_entities_table_fixed_width.txt | 6 +++--- 4 files changed, 19 insertions(+), 13 deletions(-) diff --git a/homeassistant_cli/cli.py b/homeassistant_cli/cli.py index ff9012c..5e3c8be 100644 --- a/homeassistant_cli/cli.py +++ b/homeassistant_cli/cli.py @@ -190,14 +190,11 @@ def _default_token() -> Optional[str]: ) @click.option( '--columns-width', - default=None, + default=-1, type=click.INT, envvar='HASS_COL_WIDTH', - show_envvar=True, - help=( - 'Columns custom width. ' - 'If specified truncates column values (default: auto)' - ), + show_default=True, + help='Truncates column values (0: auto, -1: disable)', ) @click.option( '--no-headers', diff --git a/homeassistant_cli/const.py b/homeassistant_cli/const.py index 1827e58..4c7eac9 100644 --- a/homeassistant_cli/const.py +++ b/homeassistant_cli/const.py @@ -19,4 +19,6 @@ ('CHANGED', 'last_changed'), ] COLUMNS_SERVICES = [('DOMAIN', 'domain'), ("SERVICE", "domain.services[*]")] -COLUMNS_WIDTH_STR = "..." + +COLUMNS_WIDTH_DEFAULT = -1 +COLUMNS_WIDTH_STR = "\u2026" diff --git a/homeassistant_cli/helper.py b/homeassistant_cli/helper.py index 14cdaa2..c2bc4f4 100644 --- a/homeassistant_cli/helper.py +++ b/homeassistant_cli/helper.py @@ -4,6 +4,7 @@ import json import logging import shlex +import shutil from typing import Any, Dict, Generator, List, Optional, Tuple, Union, cast from ruamel.yaml import YAML @@ -51,7 +52,7 @@ def raw_format_output( data: Union[Dict[str, Any], List[Dict[str, Any]]], yamlparser: YAML, columns: Optional[List] = None, - columns_width: Optional[int] = None, + columns_width: Optional[int] = -1, no_headers: bool = False, table_format: str = 'plain', sort_by: Optional[str] = None, @@ -101,7 +102,14 @@ def raw_format_output( row.append(", ".join(map(str, val))) result.append(row) # Truncates data - if columns_width: + if not columns_width: + columns_width = const.COLUMNS_WIDTH_DEFAULT + + if columns_width > -1: + if columns_width == 0: # calculate size + terminal_size = shutil.get_terminal_size() + number_c = min([len(r) for r in result]) + columns_width = int(terminal_size.columns / number_c) max_str = columns_width - len(const.COLUMNS_WIDTH_STR) result = [ [ @@ -144,7 +152,6 @@ def format_output( ctx: Configuration, data: List[Dict[str, Any]], columns: Optional[List] = None, - columns_width: Optional[int] = None, ) -> str: """Format data to output based on settings in ctx/Context.""" return raw_format_output( diff --git a/tests/fixtures/basic_entities_table_fixed_width.txt b/tests/fixtures/basic_entities_table_fixed_width.txt index d9e0a7b..7d2f072 100644 --- a/tests/fixtures/basic_entities_table_fixed_width.txt +++ b/tests/fixtures/basic_entities_table_fixed_width.txt @@ -1,4 +1,4 @@ ENTITY DESCRIPTION STATE CHANGED -sensor.one friendly lon... on 2018-12-02T1... -sensor.two off 2018-12-01T1... -sensor.three off 2018-12-03T1... +sensor.one friendly long … on 2018-12-02T10:… +sensor.two off 2018-12-01T12:… +sensor.three off 2018-12-03T12:…