Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 50 additions & 2 deletions pgcli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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 <name>)."""
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",
Expand Down Expand Up @@ -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:
Expand All @@ -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:
Expand Down Expand Up @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion pgcli/packages/sqlcompletion.py
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
5 changes: 5 additions & 0 deletions pgcli/pgclirc
Original file line number Diff line number Diff line change
Expand Up @@ -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 <name>).
# 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

Expand Down
Loading