summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorERYoung11 <78834571+ERYoung11@users.noreply.github.com>2023-10-06 22:56:48 +0000
committerGitHub <noreply@github.com>2023-10-06 15:56:48 -0700
commit43360b5d1bcc027b9282e4ee688ed9bf50a88e97 (patch)
tree040708aeddc02b9bd992c2d0f17835c894eba8e4
parenta615333686f5ac4de04aed68c2ce5d4cf7dee991 (diff)
Added \echo & \qecho for Issue #1335 (#1371)
* Added \echo & \qecho for Issue #1335 * black + changelog updates * trying to re-kick build process
-rw-r--r--changelog.rst2
-rw-r--r--pgcli/main.py21
-rw-r--r--tests/test_main.py16
3 files changed, 38 insertions, 1 deletions
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
@@ -297,6 +297,22 @@ def test_i_works(tmpdir, executor):
@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)