Skip to content

Commit bc5bd46

Browse files
authored
Merge pull request #1680 from dbcli/RW/short-host-prompt-format-string
Add short hostname prompt format string: `\H`
2 parents 4671cd0 + 53ca36f commit bc5bd46

File tree

5 files changed

+22
-0
lines changed

5 files changed

+22
-0
lines changed

changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ Features
55
---------
66
* Allow shorter timeout lengths after pressing Esc, for vi-mode.
77
* Let tab and control-space behaviors be configurable.
8+
* Add short hostname prompt format string.
89

910

1011
1.60.0 (2026/03/05)

mycli/main.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1574,9 +1574,13 @@ def get_prompt(self, string: str, _render_counter: int) -> str:
15741574
prompt_host = sqlexecute.host
15751575
else:
15761576
prompt_host = "localhost"
1577+
short_prompt_host, _, _ = prompt_host.partition('.')
1578+
if re.match(r'^[\d\.]+$', short_prompt_host):
1579+
short_prompt_host = prompt_host
15771580
now = datetime.now()
15781581
string = string.replace("\\u", sqlexecute.user or "(none)")
15791582
string = string.replace("\\h", prompt_host or "(none)")
1583+
string = string.replace("\\H", short_prompt_host or "(none)")
15801584
string = string.replace("\\d", sqlexecute.dbname or "(none)")
15811585
string = string.replace("\\t", sqlexecute.server_info.species.name)
15821586
string = string.replace("\\n", "\n")

mycli/myclirc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ wider_completion_menu = False
108108
# * \P - AM/PM
109109
# * \d - selected database/schema
110110
# * \h - hostname of the server
111+
# * \H - shortened hostname of the server
111112
# * \p - connection port
112113
# * \j - connection socket basename
113114
# * \J - full connection socket path

test/myclirc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ wider_completion_menu = False
106106
# * \P - AM/PM
107107
# * \d - selected database/schema
108108
# * \h - hostname of the server
109+
# * \H - shortened hostname of the server
109110
# * \p - connection port
110111
# * \j - connection socket basename
111112
# * \J - full connection socket path

test/test_main.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,21 @@ def test_prompt_socket_overrides_port(executor):
355355
assert prompt == "MySQL root@localhost:mysqld.sock mysql> "
356356

357357

358+
@dbtest
359+
def test_prompt_socket_short_host(executor):
360+
mycli = MyCli()
361+
mycli.prompt_format = "\\t \\u@\\H:\\k \\d> "
362+
mycli.sqlexecute = SQLExecute
363+
mycli.sqlexecute.server_info = ServerInfo.from_version_string("8.0.44-0ubuntu0.24.04.1")
364+
mycli.sqlexecute.host = 'localhost.localdomain'
365+
mycli.sqlexecute.socket = None
366+
mycli.sqlexecute.user = "root"
367+
mycli.sqlexecute.dbname = "mysql"
368+
mycli.sqlexecute.port = "3306"
369+
prompt = mycli.get_prompt(mycli.prompt_format, 0)
370+
assert prompt == "MySQL root@localhost:3306 mysql> "
371+
372+
358373
@dbtest
359374
def test_enable_show_warnings(executor):
360375
mycli = MyCli()

0 commit comments

Comments
 (0)