From 43360b5d1bcc027b9282e4ee688ed9bf50a88e97 Mon Sep 17 00:00:00 2001 From: ERYoung11 <78834571+ERYoung11@users.noreply.github.com> Date: Fri, 6 Oct 2023 22:56:48 +0000 Subject: Added \echo & \qecho for Issue #1335 (#1371) * Added \echo & \qecho for Issue #1335 * black + changelog updates * trying to re-kick build process --- changelog.rst | 2 ++ pgcli/main.py | 21 ++++++++++++++++++++- tests/test_main.py | 16 ++++++++++++++++ 3 files changed, 38 insertions(+), 1 deletion(-) diff --git a/changelog.rst b/changelog.rst index db9a39a9..8584bfec 100644 --- a/changelog.rst +++ b/changelog.rst @@ -1,3 +1,4 @@ +======== Upcoming ======== @@ -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: ---------- diff --git a/pgcli/main.py b/pgcli/main.py index a9d53f68..8459178b 100644 --- a/pgcli/main.py +++ b/pgcli/main.py @@ -363,6 +363,23 @@ class PGCli: "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: @@ -756,7 +773,9 @@ class PGCli: 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) diff --git a/tests/test_main.py b/tests/test_main.py index 9635fc04..cbf20a6a 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -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) -- cgit v1.2.3