Skip to content

Commit f2156b3

Browse files
authored
Fix short host (\h) display in prompt when using an IP address (#1441)
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.
1 parent 89979a9 commit f2156b3

3 files changed

Lines changed: 19 additions & 0 deletions

File tree

changelog.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
Upcoming
2+
========
3+
4+
Bug fixes:
5+
----------
6+
7+
* Fix display of "short host" in prompt (with `\h`) for IPv4 addresses ([issue 964](https://github.com/dbcli/pgcli/issues/964)).
8+
9+
110
==================
211
4.0.1 (2023-10-30)
312
==================

pgcli/pgexecute.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import ipaddress
12
import logging
23
import traceback
34
from collections import namedtuple
@@ -273,6 +274,11 @@ def connect(
273274

274275
@property
275276
def short_host(self):
277+
try:
278+
ipaddress.ip_address(self.host)
279+
return self.host
280+
except ValueError:
281+
pass
276282
if "," in self.host:
277283
host, _, _ = self.host.partition(",")
278284
else:

tests/test_pgexecute.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -721,6 +721,10 @@ def test_short_host(executor):
721721
executor, "host", "localhost1.example.org,localhost2.example.org"
722722
):
723723
assert executor.short_host == "localhost1"
724+
with patch.object(executor, "host", "ec2-11-222-333-444.compute-1.amazonaws.com"):
725+
assert executor.short_host == "ec2-11-222-333-444"
726+
with patch.object(executor, "host", "1.2.3.4"):
727+
assert executor.short_host == "1.2.3.4"
724728

725729

726730
class VirtualCursor:

0 commit comments

Comments
 (0)