Skip to content

Commit

Permalink
Fix short host (\h) display in prompt when using an IP address (#1441)
Browse files Browse the repository at this point in the history
When connecting to an IPv4 address (`pgcli -h 127.0.0.1`), trying to
use the "short host" in the prompt (with `\h`) would only display the
first octet (127). Now it shows the full IP.

Fixes #964.
  • Loading branch information
dbaty authored Nov 18, 2023
1 parent 89979a9 commit f2156b3
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 0 deletions.
9 changes: 9 additions & 0 deletions changelog.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
Upcoming
========

Bug fixes:
----------

* Fix display of "short host" in prompt (with `\h`) for IPv4 addresses ([issue 964](https://github.com/dbcli/pgcli/issues/964)).


==================
4.0.1 (2023-10-30)
==================
Expand Down
6 changes: 6 additions & 0 deletions pgcli/pgexecute.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import ipaddress
import logging
import traceback
from collections import namedtuple
Expand Down Expand Up @@ -273,6 +274,11 @@ def connect(

@property
def short_host(self):
try:
ipaddress.ip_address(self.host)
return self.host
except ValueError:
pass
if "," in self.host:
host, _, _ = self.host.partition(",")
else:
Expand Down
4 changes: 4 additions & 0 deletions tests/test_pgexecute.py
Original file line number Diff line number Diff line change
Expand Up @@ -721,6 +721,10 @@ def test_short_host(executor):
executor, "host", "localhost1.example.org,localhost2.example.org"
):
assert executor.short_host == "localhost1"
with patch.object(executor, "host", "ec2-11-222-333-444.compute-1.amazonaws.com"):
assert executor.short_host == "ec2-11-222-333-444"
with patch.object(executor, "host", "1.2.3.4"):
assert executor.short_host == "1.2.3.4"


class VirtualCursor:
Expand Down

0 comments on commit f2156b3

Please sign in to comment.