Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for \clip command #1264

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ Bug fixes:
* Fix comments being lost in config when saving a named query. (#1240)
* Fix IPython magic for ipython-sql >= 0.4.0
* Fix pager not being used when output format is set to csv. (#1238)
* Add \clip command to copy to clipboard
* Fix ANSI escape codes in first line make the cli choose expanded output incorrectly

3.1.0
Expand Down
20 changes: 20 additions & 0 deletions pgcli/pgexecute.py
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,26 @@ def run(
self.reset_expanded = True
sql = sql[:-2].strip()

# stub implementation
# copying the output is useful for profiling queries with EXPLAIN
if sql.startswith(r"\clip"):
sql = sql.strip(r"\clip")
cur = self.conn.cursor()
cur.execute(sql)
out_lines = cur.fetchall()
output = "\n".join([tup[0] for tup in out_lines])

# cross-platform clipboard copy
_logger.debug("copying to clipboard")
import pyperclip

pyperclip.copy(output)

# still print output
# since it is likely a explain analyze query, we may like to use an
# existing Pygments lexer for Postgres explain output
print(output)

# First try to run each query as special
_logger.debug("Trying a pgspecial command. sql: %r", sql)
try:
Expand Down