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:…