ptyfwd: introduce pty_forward_set_window_title() helper function

This commit is contained in:
Yu Watanabe
2025-06-05 14:59:40 +09:00
parent 1c814f3500
commit 23f9ff4724
5 changed files with 57 additions and 74 deletions

View 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;

View 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;
}

View 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;
}

View 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);

View 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);