From bcb0c8bce776a5eba9f5425c57c2ec8385f10e94 Mon Sep 17 00:00:00 2001 From: pmav99 Date: Thu, 23 Apr 2020 20:17:40 +0300 Subject: Add support for using pspg as the pager. (#1173) * Stop printing "status" when table_format is "csv" * Use the "unix" dialect on *nix for CSV output. * Use a pager when `pspg` has been configured with CSV "table_format". Fix #1102 --- AUTHORS | 1 + changelog.rst | 1 + pgcli/main.py | 14 +++++++++++++- 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/AUTHORS b/AUTHORS index 4c2ab3f0..8812c7ad 100644 --- a/AUTHORS +++ b/AUTHORS @@ -107,6 +107,7 @@ Contributors: * Yoni Nakache(lazydba247) * Gantsev Denis * Stephano Paraskeva + * Panos Mavrogiorgos (pmav99) Creator: -------- diff --git a/changelog.rst b/changelog.rst index 3c8d8c20..3db7fec7 100644 --- a/changelog.rst +++ b/changelog.rst @@ -5,6 +5,7 @@ Features: --------- * Make the output more compact by removing the empty newline. (Thanks: `laixintao`_) +* Add support for using [pspg](https://github.com/okbob/pspg) as a pager (#1102) Bug fixes: ---------- diff --git a/pgcli/main.py b/pgcli/main.py index a2128619..cc3d58c4 100644 --- a/pgcli/main.py +++ b/pgcli/main.py @@ -18,6 +18,7 @@ import functools import humanize import datetime as dt import itertools +import platform from time import time, sleep from codecs import open @@ -1074,6 +1075,8 @@ class PGCli(object): def echo_via_pager(self, text, color=None): if self.pgspecial.pager_config == PAGER_OFF or self.watch_command: click.echo(text, color=color) + elif "pspg" in os.environ["PAGER"] and self.table_format == "csv": + click.echo_via_pager(text, color) elif self.pgspecial.pager_config == PAGER_LONG_OUTPUT: lines = text.split("\n") @@ -1426,6 +1429,14 @@ def format_output(title, cur, headers, status, settings): if not settings.floatfmt: output_kwargs["preprocessors"] = (align_decimals,) + if table_format == "csv": + # The default CSV dialect is "excel" which is not handling newline values correctly + # Nevertheless, we want to keep on using "excel" on Windows since it uses '\r\n' + # as the line terminator + # https://github.com/dbcli/pgcli/issues/1102 + dialect = "excel" if platform.system() == "Windows" else "unix" + output_kwargs["dialect"] = dialect + if title: # Only print the title if it's not None. output.append(title) @@ -1464,7 +1475,8 @@ def format_output(title, cur, headers, status, settings): output = itertools.chain(output, formatted) - if status: # Only print the status if it's not None. + # Only print the status if it's not None and we are not producing CSV + if status and table_format != "csv": output = itertools.chain(output, [status]) return output -- cgit v1.2.3