Skip to content

Commit

Permalink
Add support for using pspg as the pager. (#1173)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
pmav99 authored Apr 23, 2020
1 parent 3071de1 commit bcb0c8b
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ Contributors:
* Yoni Nakache(lazydba247)
* Gantsev Denis
* Stephano Paraskeva
* Panos Mavrogiorgos (pmav99)

Creator:
--------
Expand Down
1 change: 1 addition & 0 deletions changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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:
----------
Expand Down
14 changes: 13 additions & 1 deletion pgcli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import humanize
import datetime as dt
import itertools
import platform
from time import time, sleep
from codecs import open

Expand Down Expand Up @@ -1074,6 +1075,8 @@ def is_too_tall(self, lines):
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":

This comment has been minimized.

Copy link
@tobwen

tobwen May 2, 2020

This freaks out if the key doesn't exist. Please change it to:

elif "PAGER" in os.environ and "pspg" in os.environ["PAGER"] and self.table_format == "csv":

This comment has been minimized.

Copy link
@j-bennet

j-bennet May 7, 2020

Contributor

Good catch: #1178.

click.echo_via_pager(text, color)
elif self.pgspecial.pager_config == PAGER_LONG_OUTPUT:
lines = text.split("\n")

Expand Down Expand Up @@ -1426,6 +1429,14 @@ def format_arrays(data, headers, **_):
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)

Expand Down Expand Up @@ -1464,7 +1475,8 @@ def format_arrays(data, headers, **_):

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
Expand Down

0 comments on commit bcb0c8b

Please sign in to comment.