]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
ptyfwd: introduce pty_forward_set_window_title() helper function
authorYu Watanabe <watanabe.yu+github@gmail.com>
Thu, 5 Jun 2025 05:59:40 +0000 (14:59 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Thu, 5 Jun 2025 10:06:02 +0000 (19:06 +0900)
src/nspawn/nspawn.c
src/run/run.c
src/shared/ptyfwd.c
src/shared/ptyfwd.h
src/vmspawn/vmspawn.c

index f404e085a748153cc0793d0d57fbec7012337149..f21614aacac16e05112455a00ab0c513ddc91a78 100644 (file)
@@ -4611,28 +4611,6 @@ static int setup_notify_parent(sd_event *event, int fd, pid_t *inner_child_pid,
         return 0;
 }
 
-static void set_window_title(PTYForward *f) {
-        _cleanup_free_ char *hn = NULL, *dot = NULL;
-
-        assert(f);
-
-        if (!shall_set_terminal_title())
-                return;
-
-        (void) gethostname_strict(&hn);
-
-        if (emoji_enabled())
-                dot = strjoin(glyph(GLYPH_BLUE_CIRCLE), " ");
-
-        if (hn)
-                (void) pty_forward_set_titlef(f, "%sContainer %s on %s", strempty(dot), arg_machine, hn);
-        else
-                (void) pty_forward_set_titlef(f, "%sContainer %s", strempty(dot), arg_machine);
-
-        if (dot)
-                (void) pty_forward_set_title_prefix(f, dot);
-}
-
 static int ptyfwd_hotkey(PTYForward *f, char c, void *userdata) {
         pid_t pid = PTR_TO_PID(userdata);
         const char *word;
@@ -5643,7 +5621,8 @@ static int run_container(
                         } else if (!isempty(arg_background))
                                 (void) pty_forward_set_background_color(forward, arg_background);
 
-                        set_window_title(forward);
+                        (void) pty_forward_set_window_title(forward, GLYPH_BLUE_CIRCLE, /* hostname = */ NULL,
+                                                            STRV_MAKE("Container", arg_machine));
 
                         pty_forward_set_hotkey_handler(forward, ptyfwd_hotkey, PID_TO_PTR(*pid));
                         break;
index 6a74123c96ac6a20799e4d1f158dc6841b6f0354..5a9476f13f6995c162fdaa6cb267201f637d2ae6 100644 (file)
@@ -36,7 +36,6 @@
 #include "format-table.h"
 #include "format-util.h"
 #include "fs-util.h"
-#include "hostname-setup.h"
 #include "log.h"
 #include "main-func.h"
 #include "osc-context.h"
@@ -2085,32 +2084,6 @@ static int acquire_invocation_id(sd_bus *bus, const char *unit, sd_id128_t *ret)
         return r; /* Return true when we get a non-null invocation ID. */
 }
 
-static void set_window_title(PTYForward *f) {
-        _cleanup_free_ char *hn = NULL, *cl = NULL, *dot = NULL;
-
-        assert(f);
-
-        if (!shall_set_terminal_title())
-                return;
-
-        if (!arg_host)
-                (void) gethostname_strict(&hn);
-
-        cl = strv_join(arg_cmdline, " ");
-        if (!cl)
-                return (void) log_oom();
-
-        if (emoji_enabled())
-                dot = strjoin(glyph(privileged_execution() ? GLYPH_RED_CIRCLE : GLYPH_YELLOW_CIRCLE), " ");
-
-        if (arg_host || hn)
-                (void) pty_forward_set_titlef(f, "%s%s on %s", strempty(dot), cl, arg_host ?: hn);
-        else
-                (void) pty_forward_set_titlef(f, "%s%s", strempty(dot), cl);
-
-        (void) pty_forward_set_title_prefix(f, dot);
-}
-
 static int fchown_to_capsule(int fd, const char *capsule) {
         _cleanup_free_ char *p = NULL;
         int r;
@@ -2187,7 +2160,9 @@ static int run_context_setup_ptyfwd(RunContext *c) {
         if (!isempty(arg_background))
                 (void) pty_forward_set_background_color(c->forward, arg_background);
 
-        set_window_title(c->forward);
+        (void) pty_forward_set_window_title(c->forward,
+                                            privileged_execution() ? GLYPH_RED_CIRCLE : GLYPH_YELLOW_CIRCLE,
+                                            arg_host, arg_cmdline);
         return 0;
 }
 
index 53d3d31131afbf08411c70d356ca95e391ba2bcd..3eda8c08343b04e6d8238d138b5faf403ba05d7e 100644 (file)
@@ -15,6 +15,8 @@
 #include "errno-util.h"
 #include "extract-word.h"
 #include "fd-util.h"
+#include "glyph-util.h"
+#include "hostname-setup.h"
 #include "io-util.h"
 #include "log.h"
 #include "ptyfwd.h"
@@ -1254,3 +1256,45 @@ int pty_forward_set_title_prefix(PTYForward *f, const char *title_prefix) {
 
         return free_and_strdup(&f->title_prefix, title_prefix);
 }
+
+int pty_forward_set_window_title(
+                PTYForward *f,
+                Glyph circle,           /* e.g. GLYPH_GREEN_CIRCLE */
+                const char *hostname,   /* Can be NULL, and obtained by gethostname_strict() in that case. */
+                char * const *msg) {
+
+        _cleanup_free_ char *hn = NULL, *dot = NULL, *joined = NULL;
+        int r;
+
+        assert(f);
+
+        if (!shall_set_terminal_title())
+                return 0;
+
+        if (!hostname) {
+                (void) gethostname_strict(&hn);
+                hostname = hn;
+        }
+
+        if (circle >= 0 && emoji_enabled()) {
+                dot = strjoin(glyph(circle), " ");
+                if (!dot)
+                        return -ENOMEM;
+        }
+
+        joined = strv_join(msg, " ");
+        if (!joined)
+                return -ENOMEM;
+
+        r = pty_forward_set_titlef(f, "%s%s%s%s", strempty(dot), joined, hn ? " on " : "", strempty(hn));
+        if (r < 0)
+                return r;
+
+        if (dot) {
+                r = pty_forward_set_title_prefix(f, dot);
+                if (r < 0)
+                        return r;
+        }
+
+        return 0;
+}
index e7b5210cc7a7e6ccf43f90e74724495d5cbddba8..6f7ec0734c2698b52417daac9321cac3731788c6 100644 (file)
@@ -49,4 +49,10 @@ int pty_forward_set_title_prefix(PTYForward *f, const char *prefix);
 
 bool shall_set_terminal_title(void);
 
+int pty_forward_set_window_title(
+                PTYForward *f,
+                Glyph circle,           /* e.g. GLYPH_GREEN_CIRCLE */
+                const char *hostname,   /* Can be NULL, and obtained by gethostname_strict() in that case. */
+                char * const *msg);
+
 DEFINE_TRIVIAL_CLEANUP_FUNC(PTYForward*, pty_forward_free);
index 7e1bd6c14967803f5234bc47c7d46d5d43b8a546..0def23f346b2e89aa4d9444213ad4a7923bb5fe1 100644 (file)
@@ -1446,28 +1446,6 @@ static int merge_initrds(char **ret) {
         return 0;
 }
 
-static void set_window_title(PTYForward *f) {
-        _cleanup_free_ char *hn = NULL, *dot = NULL;
-
-        assert(f);
-
-        if (!shall_set_terminal_title())
-                return;
-
-        (void) gethostname_strict(&hn);
-
-        if (emoji_enabled())
-                dot = strjoin(glyph(GLYPH_GREEN_CIRCLE), " ");
-
-        if (hn)
-                (void) pty_forward_set_titlef(f, "%sVirtual Machine %s on %s", strempty(dot), arg_machine, hn);
-        else
-                (void) pty_forward_set_titlef(f, "%sVirtual Machine %s", strempty(dot), arg_machine);
-
-        if (dot)
-                (void) pty_forward_set_title_prefix(f, dot);
-}
-
 static int generate_ssh_keypair(const char *key_path, const char *key_type) {
         _cleanup_free_ char *ssh_keygen = NULL;
         _cleanup_strv_free_ char **cmdline = NULL;
@@ -2449,7 +2427,8 @@ static int run_virtual_machine(int kvm_device_fd, int vhost_device_fd) {
                 } else if (!isempty(arg_background))
                         (void) pty_forward_set_background_color(forward, arg_background);
 
-                set_window_title(forward);
+                (void) pty_forward_set_window_title(forward, GLYPH_GREEN_CIRCLE, /* hostname = */ NULL,
+                                                    STRV_MAKE("Virtual Machine", arg_machine));
         }
 
         r = sd_event_loop(event);