From d700c443da0e32d85ac69624a556298651473e4e Mon Sep 17 00:00:00 2001 From: azthec Date: Fri, 18 Oct 2024 19:15:54 +0200 Subject: [PATCH 1/5] set column min width --- src/snowflake/cli/api/cli_global_context.py | 5 +++++ src/snowflake/cli/api/commands/decorators.py | 7 +++++++ src/snowflake/cli/api/commands/flags.py | 9 +++++++++ tests/api/test_global_cli_context.py | 1 + 4 files changed, 22 insertions(+) diff --git a/src/snowflake/cli/api/cli_global_context.py b/src/snowflake/cli/api/cli_global_context.py index 9aae191ec7..8190dfe1ed 100644 --- a/src/snowflake/cli/api/cli_global_context.py +++ b/src/snowflake/cli/api/cli_global_context.py @@ -42,6 +42,7 @@ class _CliGlobalContextManager: ) output_format: OutputFormat = OutputFormat.TABLE + table_min_col_width: int | None = None silent: bool = False verbose: bool = False experimental: bool = False @@ -163,6 +164,10 @@ def metrics(self): def output_format(self) -> OutputFormat: return self._manager.output_format + @property + def table_min_col_width(self) -> int | None: + return self._manager.table_min_col_width + @property def verbose(self) -> bool: return self._manager.verbose diff --git a/src/snowflake/cli/api/commands/decorators.py b/src/snowflake/cli/api/commands/decorators.py index f06af68183..4278b54be1 100644 --- a/src/snowflake/cli/api/commands/decorators.py +++ b/src/snowflake/cli/api/commands/decorators.py @@ -33,6 +33,7 @@ MasterTokenOption, MfaPasscodeOption, OutputFormatOption, + TableMinColWidthOption, PasswordOption, PortOption, PrivateKeyPathOption, @@ -335,6 +336,12 @@ def _evaluate_param_type( annotation=OutputFormat, default=OutputFormatOption, ), + inspect.Parameter( + "table_min_col_width", + inspect.Parameter.KEYWORD_ONLY, + annotation=Optional[int], + default=TableMinColWidthOption, + ), inspect.Parameter( "verbose", inspect.Parameter.KEYWORD_ONLY, diff --git a/src/snowflake/cli/api/commands/flags.py b/src/snowflake/cli/api/commands/flags.py index 698a2b0a21..dc87350bb6 100644 --- a/src/snowflake/cli/api/commands/flags.py +++ b/src/snowflake/cli/api/commands/flags.py @@ -327,6 +327,15 @@ def _diag_log_path_callback(path: str): rich_help_panel=_CLI_BEHAVIOUR, ) +TableMinColWidthOption = typer.Option( + None, + "--table-min-col-width", + help="Sets the minimum column width when printing in the table output format.", + callback=_context_callback("table_min_col_width"), + show_default=False, + rich_help_panel=_CLI_BEHAVIOUR, +) + SilentOption = typer.Option( False, "--silent", diff --git a/tests/api/test_global_cli_context.py b/tests/api/test_global_cli_context.py index 1bc1298c4f..dfec005c92 100644 --- a/tests/api/test_global_cli_context.py +++ b/tests/api/test_global_cli_context.py @@ -34,6 +34,7 @@ def test_reset_global_context_mgr(): mgr.experimental = True mgr.silent = True mgr.output_format = OutputFormat.JSON + mgr.table_min_col_width = None mgr.connection_context.database = "blahblah" mgr.connection_context.password = "****" mgr.override_project_definition = "project definition" From b7bde8744cdeb7c1ae880652c8a674396325ce5b Mon Sep 17 00:00:00 2001 From: azthec Date: Fri, 18 Oct 2024 19:17:30 +0200 Subject: [PATCH 2/5] add printing --- src/snowflake/cli/_app/printing.py | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/snowflake/cli/_app/printing.py b/src/snowflake/cli/_app/printing.py index 1dc22bd951..768bd35ef7 100644 --- a/src/snowflake/cli/_app/printing.py +++ b/src/snowflake/cli/_app/printing.py @@ -75,7 +75,7 @@ def _get_table(): return Table(show_header=True, box=box.ASCII) -def _print_multiple_table_results(obj: CollectionResult): +def _print_multiple_table_results(obj: CollectionResult, table_format_min_width: int | None = None): items = obj.result try: first_item = next(items) @@ -84,7 +84,7 @@ def _print_multiple_table_results(obj: CollectionResult): return table = _get_table() for column in first_item.keys(): - table.add_column(column, overflow="fold") + table.add_column(column, overflow="fold", min_width=table_format_min_width) with Live(table, refresh_per_second=4): table.add_row(*[str(i) for i in first_item.values()]) for item in items: @@ -137,7 +137,7 @@ def write(self, text: str): print("\n]") -def print_unstructured(obj: CommandResult | None): +def print_unstructured(obj: CommandResult | None, table_format_min_width: int | None = None): """Handles outputs like table, plain text and other unstructured types.""" if not obj: rich_print("Done", flush=True) @@ -147,17 +147,17 @@ def print_unstructured(obj: CommandResult | None): rich_print(sanitize_for_terminal(obj.message), flush=True) else: if isinstance(obj, ObjectResult): - _print_single_table(obj) + _print_single_table(obj, table_format_min_width) elif isinstance(obj, CollectionResult): - _print_multiple_table_results(obj) + _print_multiple_table_results(obj, table_format_min_width) else: raise TypeError(f"No print strategy for type: {type(obj)}") -def _print_single_table(obj): +def _print_single_table(obj, table_format_min_width: int | None = None): table = _get_table() - table.add_column("key", overflow="fold") - table.add_column("value", overflow="fold") + table.add_column("key", no_wrap=True, overflow="fold", width=table_format_min_width) + table.add_column("value", no_wrap=True, overflow="fold", width=table_format_min_width) for key, value in obj.result.items(): table.add_row( sanitize_for_terminal(str(key)), sanitize_for_terminal(str(value)) @@ -165,7 +165,7 @@ def _print_single_table(obj): rich_print(table, flush=True) -def print_result(cmd_result: CommandResult, output_format: OutputFormat | None = None): +def print_result(cmd_result: CommandResult, output_format: OutputFormat | None = None, table_format_min_width: int | None = None): output_format = output_format or _get_format_type() if is_structured_format(output_format): print_structured(cmd_result) @@ -176,6 +176,6 @@ def print_result(cmd_result: CommandResult, output_format: OutputFormat | None = isinstance(cmd_result, (MessageResult, ObjectResult, CollectionResult)) or cmd_result is None ): - print_unstructured(cmd_result) + print_unstructured(cmd_result, table_format_min_width) else: raise ValueError(f"Unexpected type {type(cmd_result)}") From ce9e37a04a883a8f5bb6a98469ebce62ea92b984 Mon Sep 17 00:00:00 2001 From: azthec Date: Fri, 18 Oct 2024 19:53:18 +0200 Subject: [PATCH 3/5] Add unwrapped table output support for pagers and text files. --- src/snowflake/cli/_app/printing.py | 23 +++++++++++--------- src/snowflake/cli/api/cli_global_context.py | 6 ++--- src/snowflake/cli/api/commands/decorators.py | 8 +++---- src/snowflake/cli/api/commands/flags.py | 12 +++++----- 4 files changed, 26 insertions(+), 23 deletions(-) diff --git a/src/snowflake/cli/_app/printing.py b/src/snowflake/cli/_app/printing.py index 768bd35ef7..77b18dadb3 100644 --- a/src/snowflake/cli/_app/printing.py +++ b/src/snowflake/cli/_app/printing.py @@ -75,7 +75,7 @@ def _get_table(): return Table(show_header=True, box=box.ASCII) -def _print_multiple_table_results(obj: CollectionResult, table_format_min_width: int | None = None): +def _print_multiple_table_results(obj: CollectionResult): items = obj.result try: first_item = next(items) @@ -84,7 +84,7 @@ def _print_multiple_table_results(obj: CollectionResult, table_format_min_width: return table = _get_table() for column in first_item.keys(): - table.add_column(column, overflow="fold", min_width=table_format_min_width) + table.add_column(column, overflow="fold") with Live(table, refresh_per_second=4): table.add_row(*[str(i) for i in first_item.values()]) for item in items: @@ -137,7 +137,7 @@ def write(self, text: str): print("\n]") -def print_unstructured(obj: CommandResult | None, table_format_min_width: int | None = None): +def print_unstructured(obj: CommandResult | None): """Handles outputs like table, plain text and other unstructured types.""" if not obj: rich_print("Done", flush=True) @@ -146,18 +146,21 @@ def print_unstructured(obj: CommandResult | None, table_format_min_width: int | elif isinstance(obj, MessageResult): rich_print(sanitize_for_terminal(obj.message), flush=True) else: + full_width_table = get_cli_context().full_width_table + if full_width_table: + get_console().width = sys.maxsize if isinstance(obj, ObjectResult): - _print_single_table(obj, table_format_min_width) + _print_single_table(obj) elif isinstance(obj, CollectionResult): - _print_multiple_table_results(obj, table_format_min_width) + _print_multiple_table_results(obj) else: raise TypeError(f"No print strategy for type: {type(obj)}") -def _print_single_table(obj, table_format_min_width: int | None = None): +def _print_single_table(obj): table = _get_table() - table.add_column("key", no_wrap=True, overflow="fold", width=table_format_min_width) - table.add_column("value", no_wrap=True, overflow="fold", width=table_format_min_width) + table.add_column("key") + table.add_column("value") for key, value in obj.result.items(): table.add_row( sanitize_for_terminal(str(key)), sanitize_for_terminal(str(value)) @@ -165,7 +168,7 @@ def _print_single_table(obj, table_format_min_width: int | None = None): rich_print(table, flush=True) -def print_result(cmd_result: CommandResult, output_format: OutputFormat | None = None, table_format_min_width: int | None = None): +def print_result(cmd_result: CommandResult, output_format: OutputFormat | None = None): output_format = output_format or _get_format_type() if is_structured_format(output_format): print_structured(cmd_result) @@ -176,6 +179,6 @@ def print_result(cmd_result: CommandResult, output_format: OutputFormat | None = isinstance(cmd_result, (MessageResult, ObjectResult, CollectionResult)) or cmd_result is None ): - print_unstructured(cmd_result, table_format_min_width) + print_unstructured(cmd_result) else: raise ValueError(f"Unexpected type {type(cmd_result)}") diff --git a/src/snowflake/cli/api/cli_global_context.py b/src/snowflake/cli/api/cli_global_context.py index 8190dfe1ed..1e9a658c6c 100644 --- a/src/snowflake/cli/api/cli_global_context.py +++ b/src/snowflake/cli/api/cli_global_context.py @@ -42,7 +42,7 @@ class _CliGlobalContextManager: ) output_format: OutputFormat = OutputFormat.TABLE - table_min_col_width: int | None = None + full_width_table: bool = False silent: bool = False verbose: bool = False experimental: bool = False @@ -165,8 +165,8 @@ def output_format(self) -> OutputFormat: return self._manager.output_format @property - def table_min_col_width(self) -> int | None: - return self._manager.table_min_col_width + def full_width_table(self) -> bool: + return self._manager.full_width_table @property def verbose(self) -> bool: diff --git a/src/snowflake/cli/api/commands/decorators.py b/src/snowflake/cli/api/commands/decorators.py index 4278b54be1..ee24ba2e81 100644 --- a/src/snowflake/cli/api/commands/decorators.py +++ b/src/snowflake/cli/api/commands/decorators.py @@ -33,7 +33,7 @@ MasterTokenOption, MfaPasscodeOption, OutputFormatOption, - TableMinColWidthOption, + FullWidthTableOption, PasswordOption, PortOption, PrivateKeyPathOption, @@ -337,10 +337,10 @@ def _evaluate_param_type( default=OutputFormatOption, ), inspect.Parameter( - "table_min_col_width", + "full_width_table", inspect.Parameter.KEYWORD_ONLY, - annotation=Optional[int], - default=TableMinColWidthOption, + annotation=Optional[bool], + default=FullWidthTableOption, ), inspect.Parameter( "verbose", diff --git a/src/snowflake/cli/api/commands/flags.py b/src/snowflake/cli/api/commands/flags.py index dc87350bb6..2c91337942 100644 --- a/src/snowflake/cli/api/commands/flags.py +++ b/src/snowflake/cli/api/commands/flags.py @@ -327,12 +327,12 @@ def _diag_log_path_callback(path: str): rich_help_panel=_CLI_BEHAVIOUR, ) -TableMinColWidthOption = typer.Option( - None, - "--table-min-col-width", - help="Sets the minimum column width when printing in the table output format.", - callback=_context_callback("table_min_col_width"), - show_default=False, +FullWidthTableOption = typer.Option( + False, + "--full-width-table", + help="Table is printed without consideration for screen size, for use with text output and pagers.", + callback=_context_callback("full_width_table"), + is_flag=True, rich_help_panel=_CLI_BEHAVIOUR, ) From c277d518669646039532e1c2f93a8a40c0941d8d Mon Sep 17 00:00:00 2001 From: azthec Date: Fri, 18 Oct 2024 19:57:03 +0200 Subject: [PATCH 4/5] formatting --- src/snowflake/cli/api/commands/decorators.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/snowflake/cli/api/commands/decorators.py b/src/snowflake/cli/api/commands/decorators.py index ee24ba2e81..021fbe215b 100644 --- a/src/snowflake/cli/api/commands/decorators.py +++ b/src/snowflake/cli/api/commands/decorators.py @@ -29,11 +29,11 @@ DiagAllowlistPathOption, DiagLogPathOption, EnableDiagOption, + FullWidthTableOption, HostOption, MasterTokenOption, MfaPasscodeOption, OutputFormatOption, - FullWidthTableOption, PasswordOption, PortOption, PrivateKeyPathOption, From 274fd698d4f74be3029cdcab28aee4724ec48180 Mon Sep 17 00:00:00 2001 From: azthec Date: Fri, 18 Oct 2024 20:08:32 +0200 Subject: [PATCH 5/5] minor fixes --- src/snowflake/cli/_app/printing.py | 4 ++-- tests/api/test_global_cli_context.py | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/snowflake/cli/_app/printing.py b/src/snowflake/cli/_app/printing.py index 77b18dadb3..e8936e7d70 100644 --- a/src/snowflake/cli/_app/printing.py +++ b/src/snowflake/cli/_app/printing.py @@ -159,8 +159,8 @@ def print_unstructured(obj: CommandResult | None): def _print_single_table(obj): table = _get_table() - table.add_column("key") - table.add_column("value") + table.add_column("key", overflow="fold") + table.add_column("value", overflow="fold") for key, value in obj.result.items(): table.add_row( sanitize_for_terminal(str(key)), sanitize_for_terminal(str(value)) diff --git a/tests/api/test_global_cli_context.py b/tests/api/test_global_cli_context.py index dfec005c92..f7d7a9f619 100644 --- a/tests/api/test_global_cli_context.py +++ b/tests/api/test_global_cli_context.py @@ -34,7 +34,7 @@ def test_reset_global_context_mgr(): mgr.experimental = True mgr.silent = True mgr.output_format = OutputFormat.JSON - mgr.table_min_col_width = None + mgr.full_width_table = False mgr.connection_context.database = "blahblah" mgr.connection_context.password = "****" mgr.override_project_definition = "project definition"