]> git.ipfire.org Git - thirdparty/mkosi.git/commitdiff
log: set terminal window title in complete_step while mkosi runs
authorJörg Behrmann <behrmann@physik.fu-berlin.de>
Thu, 2 Oct 2025 08:01:36 +0000 (10:01 +0200)
committerDaan De Meyer <daan.j.demeyer@gmail.com>
Thu, 2 Oct 2025 13:14:06 +0000 (15:14 +0200)
mkosi/__main__.py
mkosi/log.py

index 23dc534071018b3244cbc3149365bdb0b875691f..57c082b69ec67ac683babf069e8db22d473c98b9 100644 (file)
@@ -10,7 +10,7 @@ from typing import Optional
 import mkosi.resources
 from mkosi import run_verb
 from mkosi.config import parse_config
-from mkosi.log import log_setup
+from mkosi.log import log_setup, stash_terminal_title
 from mkosi.run import find_binary, run, uncaught_exception_handler
 from mkosi.util import resource_path
 
@@ -34,7 +34,7 @@ def main() -> None:
 
     log_setup()
 
-    with resource_path(mkosi.resources) as resources:
+    with resource_path(mkosi.resources) as resources, stash_terminal_title():
         args, tools, images = parse_config(sys.argv[1:], resources=resources)
 
         if args.debug:
index 55a3e850f894f803a898c6d434d84d72a475c7c5..8c4471e1941f5742a82c545f8d37c14dab73a6ca 100644 (file)
@@ -8,7 +8,7 @@ import sys
 from collections.abc import Iterator
 from typing import Any, NoReturn, Optional
 
-from mkosi.sandbox import Style
+from mkosi.sandbox import Style, terminal_is_dumb
 
 # This global should be initialized after parsing arguments
 ARG_DEBUG = contextvars.ContextVar("debug", default=False)
@@ -32,8 +32,12 @@ def log_step(text: str) -> None:
         # De-emphasize this step here, so the user can tell more
         # easily which step generated the exception. The exception
         # or error will only be printed after we finish cleanup.
+        if not terminal_is_dumb():
+            print(f"\033]0;mkosi: {text}", file=sys.stderr)
         logging.info(f"{prefix}({text})")
     else:
+        if not terminal_is_dumb():
+            print(f"\033]0;mkosi: {text}", file=sys.stderr)
         logging.info(f"{prefix}{Style.bold}{text}{Style.reset}")
 
 
@@ -85,3 +89,17 @@ def log_setup(default_log_level: str = "info") -> None:
     logging.getLogger().setLevel(
         logging.getLevelName(os.getenv("SYSTEMD_LOG_LEVEL", default_log_level).upper())
     )
+
+
+@contextlib.contextmanager
+def stash_terminal_title() -> Iterator[None]:
+    try:
+        # push terminal window title to stack
+        if not terminal_is_dumb():
+            print("\033[22t", file=sys.stderr)
+
+        yield
+    finally:
+        # pop terminal window title from stack to reset
+        if not terminal_is_dumb():
+            print("\033[23t", file=sys.stderr)