Skip to content

Commit

Permalink
Added \echo & \qecho for Issue dbcli#1335 (dbcli#1371)
Browse files Browse the repository at this point in the history
* Added \echo & \qecho for Issue dbcli#1335

* black + changelog updates

* trying to re-kick build process
  • Loading branch information
ERYoung11 committed Oct 6, 2023
1 parent a615333 commit 43360b5
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
2 changes: 2 additions & 0 deletions changelog.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
========
Upcoming
========

Expand All @@ -17,6 +18,7 @@ Features:
it will now not restart.
* Config option to always run with a single connection.
* Add comment explaining default LESS environment variable behavior and change example pager setting.
* Added \echo & \qecho special commands. ([issue 1335](https://github.com/dbcli/pgcli/issues/1335)).

Bug fixes:
----------
Expand Down
21 changes: 20 additions & 1 deletion pgcli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,23 @@ def register_special_commands(self):
"Change the table format used to output results",
)

self.pgspecial.register(
self.echo,
"\\echo",
"\\echo [string]",
"Echo a string to stdout",
)

self.pgspecial.register(
self.echo,
"\\qecho",
"\\qecho [string]",
"Echo a string to the query output channel.",
)

def echo(self, pattern, **_):
return [(None, None, None, pattern)]

def change_table_format(self, pattern, **_):
try:
if pattern not in TabularOutputFormatter().supported_formats:
Expand Down Expand Up @@ -756,7 +773,9 @@ def execute_command(self, text, handle_closed_connection=True):
click.secho(str(e), err=True, fg="red")
else:
try:
if self.output_file and not text.startswith(("\\o ", "\\? ")):
if self.output_file and not text.startswith(
("\\o ", "\\? ", "\\echo ")
):
try:
with open(self.output_file, "a", encoding="utf-8") as f:
click.echo(text, file=f)
Expand Down
16 changes: 16 additions & 0 deletions tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,22 @@ def test_i_works(tmpdir, executor):
run(executor, statement, pgspecial=cli.pgspecial)


@dbtest
def test_echo_works(executor):
cli = PGCli(pgexecute=executor)
statement = r"\echo asdf"
result = run(executor, statement, pgspecial=cli.pgspecial)
assert result == ["asdf"]


@dbtest
def test_qecho_works(executor):
cli = PGCli(pgexecute=executor)
statement = r"\qecho asdf"
result = run(executor, statement, pgspecial=cli.pgspecial)
assert result == ["asdf"]


@dbtest
def test_watch_works(executor):
cli = PGCli(pgexecute=executor)
Expand Down

0 comments on commit 43360b5

Please sign in to comment.