diff --git a/pgcli/main.py b/pgcli/main.py index 0b4b64f59..de09cc1af 100644 --- a/pgcli/main.py +++ b/pgcli/main.py @@ -208,6 +208,7 @@ def __init__( self.output_file = None self.pgspecial = PGSpecial() + self.hide_named_query_text = "hide_named_query_text" in c["main"] and c["main"].as_bool("hide_named_query_text") self.explain_mode = False self.multi_line = c["main"].as_bool("multi_line") self.multiline_mode = c["main"].get("multi_line_mode", "psql") @@ -307,7 +308,28 @@ def __init__( def quit(self): raise PgCliQuitError + def toggle_named_query_quiet(self): + """Toggle hiding of named query text""" + self.hide_named_query_text = not self.hide_named_query_text + status = "ON" if self.hide_named_query_text else "OFF" + message = f"Named query quiet mode: {status}" + return [(None, None, None, message)] + + def _is_named_query_execution(self, text): + """Check if the command is a named query execution (\n ).""" + text = text.strip() + return text.startswith("\\n ") and not text.startswith("\\ns ") and not text.startswith("\\nd ") + def register_special_commands(self): + self.pgspecial.register( + self.toggle_named_query_quiet, + "\\nq", + "\\nq", + "Toggle named query quiet mode (hide query text)", + arg_type=NO_QUERY, + case_sensitive=True, + ) + self.pgspecial.register( self.change_db, "\\c", @@ -828,7 +850,14 @@ def execute_command(self, text, handle_closed_connection=True): if self.output_file and not text.startswith(("\\o ", "\\log-file", "\\? ", "\\echo ")): try: with open(self.output_file, "a", encoding="utf-8") as f: - click.echo(text, file=f) + should_hide = ( + self.hide_named_query_text + and query.is_special + and query.successful + and self._is_named_query_execution(text) + ) + if not should_hide: + click.echo(text, file=f) click.echo("\n".join(output), file=f) click.echo("", file=f) # extra newline except OSError as e: @@ -842,7 +871,14 @@ def execute_command(self, text, handle_closed_connection=True): try: with open(self.log_file, "a", encoding="utf-8") as f: click.echo(dt.datetime.now().isoformat(), file=f) # timestamp log - click.echo(text, file=f) + should_hide = ( + self.hide_named_query_text + and query.is_special + and query.successful + and self._is_named_query_execution(text) + ) + if not should_hide: + click.echo(text, file=f) click.echo("\n".join(output), file=f) click.echo("", file=f) # extra newline except OSError as e: @@ -1136,6 +1172,18 @@ def _evaluate_command(self, text): style_output=self.style_output, max_field_width=self.max_field_width, ) + + # Hide query text for named queries in quiet mode + if ( + self.hide_named_query_text + and is_special + and success + and self._is_named_query_execution(text) + and title + and title.startswith("> ") + ): + title = None + execution = time() - start formatted = format_output(title, cur, headers, status, settings, self.explain_mode) diff --git a/pgcli/packages/sqlcompletion.py b/pgcli/packages/sqlcompletion.py index 9db4e31ed..20194ab14 100644 --- a/pgcli/packages/sqlcompletion.py +++ b/pgcli/packages/sqlcompletion.py @@ -270,7 +270,7 @@ def suggest_special(text): return (Schema(), Function(schema=None, usage="special")) return (Schema(), rel_type(schema=None)) - if cmd in ["\\n", "\\ns", "\\nd"]: + if cmd in ["\\n", "\\ns", "\\nd", "\\nq"]: return (NamedQuery(),) return (Keyword(), Special()) diff --git a/pgcli/pgclirc b/pgcli/pgclirc index 63ccdaf30..cac738937 100644 --- a/pgcli/pgclirc +++ b/pgcli/pgclirc @@ -116,6 +116,11 @@ search_path_filter = False # Timing of sql statements and table rendering. timing = True +# Hide the query text when executing named queries (\n ). +# Only the query results will be displayed. +# Can be toggled at runtime with \nq command. +hide_named_query_text = False + # Show/hide the informational toolbar with function keymap at the footer. show_bottom_toolbar = True