Skip to content

Commit 623633a

Browse files
authored
Add named query quiet mode to hide query text during execution (#1551)
* Add named query quiet mode to hide query text during execution * Add \nq to named query command autocomplete * fix:Fix code formatting to comply with ruff style guide
1 parent dc0932b commit 623633a

File tree

3 files changed

+56
-3
lines changed

3 files changed

+56
-3
lines changed

pgcli/main.py

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,7 @@ def __init__(
208208
self.output_file = None
209209
self.pgspecial = PGSpecial()
210210

211+
self.hide_named_query_text = "hide_named_query_text" in c["main"] and c["main"].as_bool("hide_named_query_text")
211212
self.explain_mode = False
212213
self.multi_line = c["main"].as_bool("multi_line")
213214
self.multiline_mode = c["main"].get("multi_line_mode", "psql")
@@ -307,7 +308,28 @@ def __init__(
307308
def quit(self):
308309
raise PgCliQuitError
309310

311+
def toggle_named_query_quiet(self):
312+
"""Toggle hiding of named query text"""
313+
self.hide_named_query_text = not self.hide_named_query_text
314+
status = "ON" if self.hide_named_query_text else "OFF"
315+
message = f"Named query quiet mode: {status}"
316+
return [(None, None, None, message)]
317+
318+
def _is_named_query_execution(self, text):
319+
"""Check if the command is a named query execution (\n <name>)."""
320+
text = text.strip()
321+
return text.startswith("\\n ") and not text.startswith("\\ns ") and not text.startswith("\\nd ")
322+
310323
def register_special_commands(self):
324+
self.pgspecial.register(
325+
self.toggle_named_query_quiet,
326+
"\\nq",
327+
"\\nq",
328+
"Toggle named query quiet mode (hide query text)",
329+
arg_type=NO_QUERY,
330+
case_sensitive=True,
331+
)
332+
311333
self.pgspecial.register(
312334
self.change_db,
313335
"\\c",
@@ -828,7 +850,14 @@ def execute_command(self, text, handle_closed_connection=True):
828850
if self.output_file and not text.startswith(("\\o ", "\\log-file", "\\? ", "\\echo ")):
829851
try:
830852
with open(self.output_file, "a", encoding="utf-8") as f:
831-
click.echo(text, file=f)
853+
should_hide = (
854+
self.hide_named_query_text
855+
and query.is_special
856+
and query.successful
857+
and self._is_named_query_execution(text)
858+
)
859+
if not should_hide:
860+
click.echo(text, file=f)
832861
click.echo("\n".join(output), file=f)
833862
click.echo("", file=f) # extra newline
834863
except OSError as e:
@@ -842,7 +871,14 @@ def execute_command(self, text, handle_closed_connection=True):
842871
try:
843872
with open(self.log_file, "a", encoding="utf-8") as f:
844873
click.echo(dt.datetime.now().isoformat(), file=f) # timestamp log
845-
click.echo(text, file=f)
874+
should_hide = (
875+
self.hide_named_query_text
876+
and query.is_special
877+
and query.successful
878+
and self._is_named_query_execution(text)
879+
)
880+
if not should_hide:
881+
click.echo(text, file=f)
846882
click.echo("\n".join(output), file=f)
847883
click.echo("", file=f) # extra newline
848884
except OSError as e:
@@ -1136,6 +1172,18 @@ def _evaluate_command(self, text):
11361172
style_output=self.style_output,
11371173
max_field_width=self.max_field_width,
11381174
)
1175+
1176+
# Hide query text for named queries in quiet mode
1177+
if (
1178+
self.hide_named_query_text
1179+
and is_special
1180+
and success
1181+
and self._is_named_query_execution(text)
1182+
and title
1183+
and title.startswith("> ")
1184+
):
1185+
title = None
1186+
11391187
execution = time() - start
11401188
formatted = format_output(title, cur, headers, status, settings, self.explain_mode)
11411189

pgcli/packages/sqlcompletion.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ def suggest_special(text):
270270
return (Schema(), Function(schema=None, usage="special"))
271271
return (Schema(), rel_type(schema=None))
272272

273-
if cmd in ["\\n", "\\ns", "\\nd"]:
273+
if cmd in ["\\n", "\\ns", "\\nd", "\\nq"]:
274274
return (NamedQuery(),)
275275

276276
return (Keyword(), Special())

pgcli/pgclirc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,11 @@ search_path_filter = False
116116
# Timing of sql statements and table rendering.
117117
timing = True
118118

119+
# Hide the query text when executing named queries (\n <name>).
120+
# Only the query results will be displayed.
121+
# Can be toggled at runtime with \nq command.
122+
hide_named_query_text = False
123+
119124
# Show/hide the informational toolbar with function keymap at the footer.
120125
show_bottom_toolbar = True
121126

0 commit comments

Comments
 (0)