diff --git a/changelog.md b/changelog.md index fb622df9..bfd618b7 100644 --- a/changelog.md +++ b/changelog.md @@ -5,6 +5,7 @@ Features --------- * Allow shorter timeout lengths after pressing Esc, for vi-mode. * Let tab and control-space behaviors be configurable. +* Add short hostname prompt format string. 1.60.0 (2026/03/05) diff --git a/mycli/main.py b/mycli/main.py index 873b62ef..08c2d9b9 100755 --- a/mycli/main.py +++ b/mycli/main.py @@ -1574,9 +1574,13 @@ def get_prompt(self, string: str, _render_counter: int) -> str: prompt_host = sqlexecute.host else: prompt_host = "localhost" + short_prompt_host, _, _ = prompt_host.partition('.') + if re.match(r'^[\d\.]+$', short_prompt_host): + short_prompt_host = prompt_host now = datetime.now() string = string.replace("\\u", sqlexecute.user or "(none)") string = string.replace("\\h", prompt_host or "(none)") + string = string.replace("\\H", short_prompt_host or "(none)") string = string.replace("\\d", sqlexecute.dbname or "(none)") string = string.replace("\\t", sqlexecute.server_info.species.name) string = string.replace("\\n", "\n") diff --git a/mycli/myclirc b/mycli/myclirc index 1d741811..698794ad 100644 --- a/mycli/myclirc +++ b/mycli/myclirc @@ -108,6 +108,7 @@ wider_completion_menu = False # * \P - AM/PM # * \d - selected database/schema # * \h - hostname of the server +# * \H - shortened hostname of the server # * \p - connection port # * \j - connection socket basename # * \J - full connection socket path diff --git a/test/myclirc b/test/myclirc index 734f07ef..3a758d97 100644 --- a/test/myclirc +++ b/test/myclirc @@ -106,6 +106,7 @@ wider_completion_menu = False # * \P - AM/PM # * \d - selected database/schema # * \h - hostname of the server +# * \H - shortened hostname of the server # * \p - connection port # * \j - connection socket basename # * \J - full connection socket path diff --git a/test/test_main.py b/test/test_main.py index 25c95e6e..fb486492 100644 --- a/test/test_main.py +++ b/test/test_main.py @@ -355,6 +355,21 @@ def test_prompt_socket_overrides_port(executor): assert prompt == "MySQL root@localhost:mysqld.sock mysql> " +@dbtest +def test_prompt_socket_short_host(executor): + mycli = MyCli() + mycli.prompt_format = "\\t \\u@\\H:\\k \\d> " + mycli.sqlexecute = SQLExecute + mycli.sqlexecute.server_info = ServerInfo.from_version_string("8.0.44-0ubuntu0.24.04.1") + mycli.sqlexecute.host = 'localhost.localdomain' + mycli.sqlexecute.socket = None + mycli.sqlexecute.user = "root" + mycli.sqlexecute.dbname = "mysql" + mycli.sqlexecute.port = "3306" + prompt = mycli.get_prompt(mycli.prompt_format, 0) + assert prompt == "MySQL root@localhost:3306 mysql> " + + @dbtest def test_enable_show_warnings(executor): mycli = MyCli()