From 81d16c9282964d389b673be99230727cf2909a15 Mon Sep 17 00:00:00 2001 From: godtechwak Date: Thu, 8 Jan 2026 01:40:58 +0900 Subject: [PATCH 1/3] Add named query quiet mode to hide query text during execution --- pgcli/main.py | 59 +++++++++++++++++++++++++++++++++++++++++++++++++-- pgcli/pgclirc | 5 +++++ 2 files changed, 62 insertions(+), 2 deletions(-) diff --git a/pgcli/main.py b/pgcli/main.py index 0b4b64f5..31f54161 100644 --- a/pgcli/main.py +++ b/pgcli/main.py @@ -208,6 +208,10 @@ 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 +311,32 @@ 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 +857,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 +878,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 +1179,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/pgclirc b/pgcli/pgclirc index 63ccdaf3..cac73893 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 From a6122c49ca153c4de9a4db2b71e9536c870014dd Mon Sep 17 00:00:00 2001 From: godtechwak Date: Thu, 8 Jan 2026 01:55:05 +0900 Subject: [PATCH 2/3] Add \nq to named query command autocomplete --- pgcli/packages/sqlcompletion.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pgcli/packages/sqlcompletion.py b/pgcli/packages/sqlcompletion.py index 9db4e31e..20194ab1 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()) From f2d6bd85325db13cc9b4bb0643737b5b7a2e8f72 Mon Sep 17 00:00:00 2001 From: godtechwak Date: Mon, 12 Jan 2026 13:37:58 +0900 Subject: [PATCH 3/3] fix:Fix code formatting to comply with ruff style guide --- pgcli/main.py | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/pgcli/main.py b/pgcli/main.py index 31f54161..de09cc1a 100644 --- a/pgcli/main.py +++ b/pgcli/main.py @@ -208,10 +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.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") @@ -321,11 +318,7 @@ def toggle_named_query_quiet(self): 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 ") - ) + return text.startswith("\\n ") and not text.startswith("\\ns ") and not text.startswith("\\nd ") def register_special_commands(self): self.pgspecial.register(