Skip to content

Commit

Permalink
Allows setting maximum column width --columns-width
Browse files Browse the repository at this point in the history
- If specified --columns-width or HASS_COL_WIDTH env var
  truncates column values
- Uses default const.COLUMNS_WIDTH_STR
- Fixes home-assistant-ecosystem#253
  • Loading branch information
jm66 authored and maxandersen committed Nov 17, 2019
1 parent 1528c0a commit 5cd64f1
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 2 deletions.
13 changes: 13 additions & 0 deletions homeassistant_cli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,17 @@ def _default_token() -> Optional[str]:
' Example: ENTITY=entity_id, NAME=attributes.friendly_name'
),
)
@click.option(
'--columns-width',
default=None,
type=click.INT,
envvar='HASS_COL_WIDTH',
show_envvar=True,
help=(
'Columns custom width. '
'If specified truncates column values (default: auto)'
),
)
@click.option(
'--no-headers',
default=False,
Expand Down Expand Up @@ -220,6 +231,7 @@ def cli(
showexceptions: bool,
cert: str,
columns: str,
columns_width: int,
no_headers: bool,
table_format: str,
sort_by: Optional[str],
Expand All @@ -236,6 +248,7 @@ def cli(
ctx.showexceptions = showexceptions
ctx.cert = cert
ctx.columns = to_tuples(columns)
ctx.columns_width = columns_width
ctx.no_headers = no_headers
ctx.table_format = table_format
ctx.sort_by = sort_by # type: ignore
Expand Down
3 changes: 2 additions & 1 deletion homeassistant_cli/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def _locate_ha() -> Optional[str]:
listener = _ZeroconfListener()
zeroconf.ServiceBrowser(_zeroconf, "_home-assistant._tcp.local.", listener)
try:
import time
import time # pylint: disable=import-outside-toplevel

retries = 0
while not listener.services and retries < 5:
Expand Down Expand Up @@ -120,6 +120,7 @@ def __init__(self) -> None:
self.session = None # type: Optional[Session]
self.cert = None # type: Optional[str]
self.columns = None # type: Optional[List[Tuple[str, str]]]
self.columns_width = None # type: Optional[int]
self.no_headers = False
self.table_format = 'plain'
self.sort_by = None
Expand Down
1 change: 1 addition & 0 deletions homeassistant_cli/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@
('CHANGED', 'last_changed'),
]
COLUMNS_SERVICES = [('DOMAIN', 'domain'), ("SERVICE", "domain.services[*]")]
COLUMNS_WIDTH_STR = "..."
18 changes: 17 additions & 1 deletion homeassistant_cli/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,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,
no_headers: bool = False,
table_format: str = 'plain',
sort_by: Optional[str] = None,
Expand Down Expand Up @@ -99,7 +100,20 @@ def raw_format_output(
val = [match.value for match in fmtpair[1].find(item)]
row.append(", ".join(map(str, val)))
result.append(row)

# Truncates data
if columns_width:
max_str = columns_width - len(const.COLUMNS_WIDTH_STR)
result = [
[
(
c
if len(c) < columns_width
else c[:max_str] + const.COLUMNS_WIDTH_STR
)
for c in row
]
for row in result
]
res = tabulate(
result, headers=headers, tablefmt=table_format
) # type: str
Expand Down Expand Up @@ -130,13 +144,15 @@ 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(
ctx.output,
data,
ctx.yaml(),
columns,
ctx.columns_width,
ctx.no_headers,
ctx.table_format,
ctx.sort_by,
Expand Down
4 changes: 4 additions & 0 deletions tests/fixtures/basic_entities_table_fixed_width.txt
Original file line number Diff line number Diff line change
@@ -0,0 +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...
11 changes: 11 additions & 0 deletions tests/test_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,17 @@ def test_state_list_table_columns_sortby(
)


def test_state_list_table_columns_width(
basic_entities_text, basic_entities_table_fixed_width_text
) -> None:
"""Test table columns."""
output_formats(
["--output=table", "--columns-width=15", "state", "list"],
basic_entities_text,
basic_entities_table_fixed_width_text,
)


def test_state_list_no_header(
basic_entities_text, basic_entities_table_no_header_text
) -> None:
Expand Down

0 comments on commit 5cd64f1

Please sign in to comment.