Skip to content

Commit

Permalink
Improving --column-width to 0: auto, -1: disable
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jm66 authored and maxandersen committed Nov 17, 2019
1 parent 5cd64f1 commit b6b5dd2
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 13 deletions.
9 changes: 3 additions & 6 deletions homeassistant_cli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
4 changes: 3 additions & 1 deletion homeassistant_cli/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
13 changes: 10 additions & 3 deletions homeassistant_cli/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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 = [
[
Expand Down Expand Up @@ -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(
Expand Down
6 changes: 3 additions & 3 deletions tests/fixtures/basic_entities_table_fixed_width.txt
Original file line number Diff line number Diff line change
@@ -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:…

0 comments on commit b6b5dd2

Please sign in to comment.