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 committed Oct 16, 2019
1 parent fa918dd commit 822a2db
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 1 deletion.
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_name, 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
1 change: 1 addition & 0 deletions homeassistant_cli/config.py
Original file line number Diff line number Diff line change
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

0 comments on commit 822a2db

Please sign in to comment.