From 25d2a804cff8f7bd9c8d91d6535b7d4c67f35b75 Mon Sep 17 00:00:00 2001 From: Roland Walker Date: Sat, 21 Feb 2026 07:57:46 -0500 Subject: [PATCH] add prompt format strings for server uptime * "\y" shows uptime in seconds * "\Y" shows uptime in words This requires a trip to the server for every update, which is noted in the commentary. There is also a bug, not new to this commit, that the prompt is updated on every keypress rather than on every return. --- changelog.md | 1 + mycli/main.py | 15 ++++++++++++++- mycli/myclirc | 2 ++ mycli/packages/special/utils.py | 16 ++++++++++++++++ test/myclirc | 2 ++ 5 files changed, 35 insertions(+), 1 deletion(-) diff --git a/changelog.md b/changelog.md index c861375b..9048900a 100644 --- a/changelog.md +++ b/changelog.md @@ -13,6 +13,7 @@ Features * Accept `character_set` as a DSN query parameter. * Don't attempt SSL for local socket connections when in "auto" SSL mode. * Add prompt format string for SSL/TLS version of the connection. +* Add prompt format strings for displaying uptime. Bug Fixes diff --git a/mycli/main.py b/mycli/main.py index f1d87ebc..3a549ba7 100755 --- a/mycli/main.py +++ b/mycli/main.py @@ -66,7 +66,7 @@ from mycli.packages.prompt_utils import confirm, confirm_destructive_query from mycli.packages.special.favoritequeries import FavoriteQueries from mycli.packages.special.main import ArgType -from mycli.packages.special.utils import get_ssl_version +from mycli.packages.special.utils import format_uptime, get_ssl_version, get_uptime from mycli.packages.sqlresult import SQLResult from mycli.packages.tabular_output import sql_format from mycli.packages.toolkit.history import FileHistoryWithTimestamp @@ -1454,6 +1454,7 @@ def get_completions(self, text: str, cursor_position: int) -> Iterable[Completio with self._completer_lock: return self.completer.get_completions(Document(text=text, cursor_position=cursor_position), None) + # todo: time/uptime update on every character typed, instead of after every return def get_prompt(self, string: str) -> str: sqlexecute = self.sqlexecute assert sqlexecute is not None @@ -1483,6 +1484,18 @@ def get_prompt(self, string: str) -> str: string = string.replace("\\k", os.path.basename(sqlexecute.socket or str(sqlexecute.port))) string = string.replace("\\K", sqlexecute.socket or str(sqlexecute.port)) string = string.replace("\\A", self.dsn_alias or "(none)") + # jump through hoops for the test environment, and for efficiency + if hasattr(sqlexecute, 'conn') and sqlexecute.conn is not None: + if '\\y' in string: + with sqlexecute.conn.cursor() as cur: + string = string.replace('\\y', str(get_uptime(cur)) or '(none)') + if '\\Y' in string: + with sqlexecute.conn.cursor() as cur: + string = string.replace('\\Y', format_uptime(str(get_uptime(cur))) or '(none)') + else: + string = string.replace('\\y', '(none)') + string = string.replace('\\Y', '(none)') + string = string.replace("\\_", " ") # jump through hoops for the test environment and for efficiency if hasattr(sqlexecute, 'conn') and sqlexecute.conn is not None: diff --git a/mycli/myclirc b/mycli/myclirc index 618478fd..7e73021b 100644 --- a/mycli/myclirc +++ b/mycli/myclirc @@ -116,6 +116,8 @@ wider_completion_menu = False # * \T - connection SSL/TLS version # * \t - database vendor (Percona, MySQL, MariaDB, TiDB) # * \u - username +# * \y - uptime in seconds (requires frequent trips to the server) +# * \Y - uptime in words (requires frequent trips to the server) # * \A - DSN alias # * \n - a newline # * \_ - a space diff --git a/mycli/packages/special/utils.py b/mycli/packages/special/utils.py index c5b7cd6e..eedec11e 100644 --- a/mycli/packages/special/utils.py +++ b/mycli/packages/special/utils.py @@ -56,6 +56,22 @@ def format_uptime(uptime_in_seconds: str) -> str: return uptime +def get_uptime(cur: Cursor) -> int: + query = 'SHOW STATUS LIKE "Uptime"' + logger.debug(query) + + uptime = 0 + + try: + cur.execute(query) + if one := cur.fetchone(): + uptime = int(one[1] or 0) + except pymysql.err.OperationalError: + pass + + return uptime + + def get_ssl_version(cur: Cursor) -> str | None: cache_key = (id(cur.connection), cur.connection.thread_id()) diff --git a/test/myclirc b/test/myclirc index 1280d118..de62bf38 100644 --- a/test/myclirc +++ b/test/myclirc @@ -113,6 +113,8 @@ wider_completion_menu = False # * \K - full connection socket path OR the port # * \T - connection SSL/TLS version # * \t - database vendor (Percona, MySQL, MariaDB, TiDB) +# * \y - uptime in seconds (requires frequent trips to the server) +# * \Y - uptime in words (requires frequent trips to the server) # * \u - username # * \A - DSN alias # * \n - a newline