Good morning, Attached please find a patch...
Programs such as Claude Code report progress with OSC 9;4, which Windows Terminal, iTerm2 and ghostty draw in the tab or taskbar. Inside screen the bar sticks when you switch windows. This patch keeps each window's progress and sends only the focused window's, the way screen already handles xterm titles. A new integration test runs under make check beside the detach test. It attaches a client with script and covers switching with one, two and no bars, malformed input, detach and reattach. It also handles the FreeBSD and macOS form of script, which lacks -c, but I have tried that form only with a stand-in. I am not 100% certain that this is the right testing approach or system target. - Rhys
From 836f9517b9110970c231d03ad0fb174956c68272 Mon Sep 17 00:00:00 2001 From: Rhys Ulerich <[email protected]> Date: Thu, 19 Nov 2026 22:42:56 +0000 Subject: [PATCH] Track OSC 9;4 progress per window Screen ignores OSC 9;4, so programs wrap it in DCS to reach the outer terminal. A DCS from a background window goes nowhere and nothing replays it, so the outer progress bar sticks. Store each window's progress and send it to displays where that window has focus. On a switch, redraw or reattach, send the new window's state, or 9;4;0 when a bar is showing and the window has none. On detach, send 9;4;0. Accept only "9;4;" followed by STATE 0-4 and an optional PERCENT 0-100. Drop plain OSC 9 and anything malformed, keeping the stored state. Test through a client attached with script. --- Also at https://github.com/RhysU/screen/pull/1 src/Makefile.in | 4 +- src/ansi.c | 32 ++++ src/display.c | 19 +++ src/display.h | 3 + src/doc/screen.1 | 21 +++ src/doc/screen.texinfo | 8 + src/process.c | 8 + src/process.h | 1 + src/tests/integration-progress.c | 251 +++++++++++++++++++++++++++++++ src/window.h | 1 + 10 files changed, 347 insertions(+), 1 deletion(-) create mode 100644 src/tests/integration-progress.c diff --git a/src/Makefile.in b/src/Makefile.in index 694f40a..00b44e0 100644 --- a/src/Makefile.in +++ b/src/Makefile.in @@ -59,7 +59,7 @@ OFILES=$(CFILES:c=o) TESTCFILES := $(wildcard tests/test-*.c) TESTBIN := $(TESTCFILES:.c=) -TESTSTANDALONE := tests/test-detach-cli +TESTSTANDALONE := tests/test-detach-cli tests/test-progress-cli all: screen @@ -83,6 +83,8 @@ tests/test-%: tests/test-%.c %.o tests/mallocmock.o tests/macros.h tests/signatu $(CC) $(CPPFLAGS) $(CFLAGS) $< -o $@ $*.o tests/mallocmock.o tests/test-detach-cli: tests/integration-detach.c tests/macros.h screen $(CC) $(CPPFLAGS) $(CFLAGS) $< -o $@ +tests/test-progress-cli: tests/integration-progress.c tests/macros.h screen + $(CC) $(CPPFLAGS) $(CFLAGS) $< -o $@ install_bin: screen installdirs -if [ -f $(DESTDIR)$(bindir)/$(SCREEN) ] && [ ! -f $(DESTDIR)$(bindir)/$(SCREEN).old ]; \ diff --git a/src/ansi.c b/src/ansi.c index 1b5e6bc..114a89e 100644 --- a/src/ansi.c +++ b/src/ansi.c @@ -1221,6 +1221,30 @@ static void StringChar(Window *win, int c) *(win->w_stringp)++ = c; } +/* + * Store OSC 9;4 STATE[;PERCENT] as "STATE;PERCENT", or as empty when + * STATE is 0. Returns false and stores nothing on bad input. + */ +static bool StoreProgress(Window *win, char *p) +{ + int state, percent = 0; + + if (*p < '0' || *p > '4') + return false; + state = *p++ - '0'; + if (*p == ';') + for (p++; *p >= '0' && *p <= '9'; p++) + if ((percent = 10 * percent + (*p - '0')) > 100) + return false; + if (*p) + return false; + if (state) + snprintf(win->w_progress, ARRAY_SIZE(win->w_progress), "%d;%d", state, percent); + else + win->w_progress[0] = '\0'; + return true; +} + /* * Do string processing. Returns -1 if output should be suspended * until status is gone. @@ -1240,6 +1264,14 @@ static int StringEnd(Window *win) *win->w_stringp = '\0'; switch (win->w_StringType) { case OSC: /* special xterm compatibility hack */ + if (strncmp(win->w_string, "9;4;", 4) == 0) { /* ConEmu progress */ + if (!StoreProgress(win, win->w_string + 4)) + break; + for (display = displays; display; display = display->d_next) + if (D_forecv->c_layer->l_bottom == &win->w_layer) + SetProgress(win->w_progress, t); + break; + } if (win->w_string[0] == ';' || (p = strchr(win->w_string, ';')) == NULL) break; typ = atoi(win->w_string); diff --git a/src/display.c b/src/display.c index a3fc962..2c25c60 100644 --- a/src/display.c +++ b/src/display.c @@ -364,6 +364,7 @@ void FinitTerm(void) AddCStr(D_CCE); if (D_hstatus) ShowHStatus(NULL); + SetProgress(NULL, "\a"); ClearAllXtermOSC(); D_x = D_y = -1; GotoPos(0, D_height - 1); @@ -955,6 +956,7 @@ void Redisplay(int cur_only) SetFlow(FLOW_ON); ClearAll(); + RefreshProgress(); RefreshXtermOSC(); if (cur_only > 0 && D_fore) RefreshArea(0, D_fore->w_y, D_width - 1, D_fore->w_y, 1); @@ -2141,6 +2143,23 @@ void ChangeScrollRegion(int newtop, int newbot) D_y = D_x = -1; /* Just in case... */ } +/* + * Send OSC 9;4 progress. Empty s clears a shown bar. + */ +void SetProgress(char *s, char *t) +{ + if (!D_CXT) + return; + if (!s) + s = ""; + if (!D_progress && !*s) + return; + D_progress = *s != '\0'; + AddStr("\033]9;4;"); + AddStr(*s ? s : "0"); + AddStr(t); +} + #define WT_FLAG "2" /* change to "0" to set both title and icon */ void SetXtermOSC(int i, char *s, char *t) diff --git a/src/display.h b/src/display.h index 96a76c1..e637bde 100644 --- a/src/display.h +++ b/src/display.h @@ -124,6 +124,7 @@ struct Display { does not */ int d_bracketed; /* bracketed paste mode */ int d_cursorstyle; /* cursor style */ + bool d_progress; /* progress bar shown */ int d_xtermosc[5]; /* osc used */ struct mchar d_lpchar; /* missing char */ struct timeval d_status_time; /* time of status display */ @@ -235,6 +236,7 @@ struct Display { #define D_mouse_parse DISPLAY(d_mouse_parse) #define D_extmouse DISPLAY(d_extmouse) #define D_mousetrack DISPLAY(d_mousetrack) +#define D_progress DISPLAY(d_progress) #define D_xtermosc DISPLAY(d_xtermosc) #define D_lpchar DISPLAY(d_lpchar) #define D_status DISPLAY(d_status) @@ -371,6 +373,7 @@ void Flush (int); void freetty (void); void Resize_obuf (void); void NukePending (void); +void SetProgress (char *, char *); void ClearAllXtermOSC (void); void SetXtermOSC (int, char *, char *); void ResetIdle (void); diff --git a/src/doc/screen.1 b/src/doc/screen.1 index fb33851..4780d0d 100644 --- a/src/doc/screen.1 +++ b/src/doc/screen.1 @@ -4648,6 +4648,27 @@ Application Program Command (Hardstatus) .BR "ESC ] 0 ; string ^G" " (A)" Operating System Command (Hardstatus, xterm title hack) .TP 27 +.BR "ESC ] 9 ; 4 ; \fPPs\fB ^G" " (A)" +Progress indicator, kept per window. Sent to the host terminal +while the window has focus. Bad input is dropped. +.RS +.TP 27 +Ps = \fB0\fP +Clear +.TP 27 +Ps = \fB1\fP ; Pn +Normal, Pn percent +.TP 27 +Ps = \fB2\fP ; Pn +Error, Pn percent +.TP 27 +Ps = \fB3\fP +Indeterminate +.TP 27 +Ps = \fB4\fP ; Pn +Warning, Pn percent +.RE +.TP 27 .BR "ESC ] 83 ; cmd ^G" " (A)" Execute screen command. This only works if multi-user support is compiled into screen. The pseudo-user \*Q:window:\*U is used to diff --git a/src/doc/screen.texinfo b/src/doc/screen.texinfo index 58075c2..8ab8083 100644 --- a/src/doc/screen.texinfo +++ b/src/doc/screen.texinfo @@ -3355,6 +3355,14 @@ ESC P (A) Device Control String ESC _ (A) Application Program Command (Hardstatus) ESC ] 0 ; string ^G (A) Operating System Command (Hardstatus, xterm title hack) +ESC ] 9 ; 4 ; Ps ^G (A) Progress indicator, kept per window. Sent + to the host terminal while the window has + focus. Bad input is dropped. + Ps = 0 (A) Clear + 1 ; Pn (A) Normal, Pn percent + 2 ; Pn (A) Error, Pn percent + 3 (A) Indeterminate + 4 ; Pn (A) Warning, Pn percent ESC ] 83 ; cmd ^G (A) Execute screen command. This only works if multi-user support is compiled into screen. The pseudo-user ":window:" is used to check diff --git a/src/process.c b/src/process.c index 278a064..ed80055 100644 --- a/src/process.c +++ b/src/process.c @@ -7205,6 +7205,7 @@ void SetForeCanvas(Display *d, Canvas *cv) Activate(-1); } else { RefreshHStatus(); + RefreshProgress(); RefreshXtermOSC(); flayer = D_forecv->c_layer; CV_CALL(D_forecv, LayRestore(); @@ -7215,6 +7216,13 @@ void SetForeCanvas(Display *d, Canvas *cv) display = odisplay; } +void RefreshProgress(void) +{ + Window *p = Layer2Window(D_forecv->c_layer); + + SetProgress(p ? p->w_progress : NULL, "\a"); +} + void RefreshXtermOSC(void) { Window *p = Layer2Window(D_forecv->c_layer); diff --git a/src/process.h b/src/process.h index ed1c712..c6d25ee 100644 --- a/src/process.h +++ b/src/process.h @@ -24,6 +24,7 @@ char *AddOtherUsers (char *, int, Window *); int WindowByNoN (char *); Window *FindNiceWindow (Window *, char *); int CompileKeys (char *, int, unsigned char *); +void RefreshProgress (void); void RefreshXtermOSC (void); uint64_t ParseAttrColor (char *, int); void ApplyAttrColor (uint64_t, struct mchar *); diff --git a/src/tests/integration-progress.c b/src/tests/integration-progress.c new file mode 100644 index 0000000..008f30f --- /dev/null +++ b/src/tests/integration-progress.c @@ -0,0 +1,251 @@ +#define _GNU_SOURCE +#define _XOPEN_SOURCE 600 + +#include <poll.h> +#include <signal.h> +#include <stdbool.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <sys/types.h> +#include <sys/wait.h> +#include <unistd.h> + +#include "macros.h" + +#define WAIT_MS 5000 +#define QUIET_MS 700 + +#define PROGRESS "\033]9;4;" + +static char session[64]; +static bool script_c; /* script takes -c, as on Linux, NetBSD and OpenBSD */ +static int idle[2]; /* script's stdin, held open so input never ends */ +static char seen[65536]; +static size_t seen_len; + +/* Read the client's output until needle appears or ms pass. */ +static bool read_until(int fd, const char *needle, int ms) +{ + struct pollfd pfd = { .fd = fd, .events = POLLIN }; + + while (poll(&pfd, 1, ms) > 0) { + ssize_t n = read(fd, seen + seen_len, sizeof(seen) - 1 - seen_len); + + if (n <= 0) + break; + seen_len += n; + seen[seen_len] = '\0'; + if (needle && strstr(seen, needle)) + return true; + } + return false; +} + +static void forget(void) +{ + seen_len = 0; + seen[0] = '\0'; +} + +static void expect(int fd, const char *needle) +{ + ASSERT(read_until(fd, needle, WAIT_MS)); + forget(); +} + +static void expect_quiet(int fd, const char *needle) +{ + read_until(fd, NULL, QUIET_MS); + ASSERT(strstr(seen, needle) == NULL); + forget(); +} + +/* Run ./screen -S session followed by args. */ +static void screen_x(char *const args[]) +{ + char *argv[16] = { "./screen", "-S", session }; + int status; + pid_t pid; + int i; + + for (i = 0; args[i]; i++) + argv[i + 3] = args[i]; + argv[i + 3] = NULL; + pid = fork(); + ASSERT(pid >= 0); + if (pid == 0) { + execv(argv[0], argv); + _exit(127); + } + ASSERT(waitpid(pid, &status, 0) == pid); + ASSERT(WIFEXITED(status) && WEXITSTATUS(status) == 0); +} + +static void stuff(const char *win, const char *line) +{ + screen_x((char *[]){ "-p", (char *)win, "-X", "stuff", (char *)line, NULL }); +} + +/* Select win and expect needle as the first progress sent. */ +static void select_win(int fd, const char *win, const char *needle) +{ + screen_x((char *[]){ "-X", "select", (char *)win, NULL }); + ASSERT(read_until(fd, needle, WAIT_MS)); + ASSERT(strstr(seen, PROGRESS) == strstr(seen, needle)); + forget(); +} + +/* Quit the session when an ASSERT aborts. */ +static void quit_session(int sig) +{ + if (fork() == 0) { + execl("./screen", "./screen", "-S", session, "-X", "quit", (char *)NULL); + _exit(127); + } + signal(sig, SIG_DFL); + raise(sig); +} + +/* + * Run cmd under script and return a pipe carrying the client's output. + * FreeBSD and macOS take the command after the file instead of -c. + */ +static pid_t spawn_client(int *out, const char *cmd) +{ + int fds[2]; + pid_t pid; + + ASSERT(pipe(fds) == 0); + forget(); + pid = fork(); + ASSERT(pid >= 0); + if (pid == 0) { + dup2(idle[0], 0); + dup2(fds[1], 1); + if (script_c) + execlp("script", "script", "-c", cmd, "/dev/null", (char *)NULL); + else + execlp("script", "script", "/dev/null", "/bin/sh", "-c", cmd, (char *)NULL); + _exit(127); + } + close(fds[1]); + *out = fds[0]; + return pid; +} + +int main(void) +{ + char cmd[256]; + int out; + int status; + pid_t pid; + + if (system("command -v script >/dev/null 2>&1") != 0) { + fprintf(stderr, "SKIP: script not available\n"); + return 77; + } + script_c = system("script -c true /dev/null </dev/null >/dev/null 2>&1") == 0; + ASSERT(setenv("TERM", "xterm", 1) == 0); + ASSERT(setenv("PS1", "$ ", 1) == 0); + ASSERT(setenv("SHELL", "/bin/sh", 1) == 0); + snprintf(session, sizeof(session), "progress-%ld", (long)getpid()); + ASSERT(pipe(idle) == 0); + signal(SIGABRT, quit_session); + snprintf(cmd, sizeof(cmd), "./screen -c /dev/null -S %s /bin/sh", session); + pid = spawn_client(&out, cmd); + expect(out, "$ "); + + /* Front window passes progress through. */ + stuff("0", "printf '\\033]9;4;1;50\\007'\n"); + expect(out, PROGRESS "1;50\a"); + + /* A new window without progress clears the bar. */ + screen_x((char *[]){ "-X", "screen", NULL }); + expect(out, PROGRESS "0\a"); + + /* Background progress waits for a switch back. */ + stuff("0", "printf '\\033]9;4;2;30\\007'\n"); + expect_quiet(out, PROGRESS); + screen_x((char *[]){ "-X", "select", "0", NULL }); + expect(out, PROGRESS "2;30\a"); + + /* Plain OSC 9 and bad input are dropped and the stored state kept. */ + stuff("0", "printf '" + "\\033]9;hello\\007" + "\\033]9;4;5;10\\007" + "\\033]9;4;-1;5\\007" + "\\033]9;4;;5\\007" + "\\033]9;4; 1;5\\007" + "\\033]09;4;1;5\\007" + "\\033]9x;4;1;5\\007" + "\\033]9;4;04;5\\007" + "\\033]9;4;1; 5\\007" + "\\033]9;4;1;+5\\007" + "\\033]9;4;1;-5\\007" + "\\033]9;4;1;101\\007" + "\\033]9;4;99999999999999999999;1\\007" + "\\033]9;4;1;-99999999999999999999\\007" + "\\033]9;4;1;5\\033\\033]2;x\\007" + "'\n"); + expect_quiet(out, "\033]9"); + screen_x((char *[]){ "-X", "select", "1", NULL }); + expect(out, PROGRESS "0\a"); + screen_x((char *[]){ "-X", "select", "0", NULL }); + expect(out, PROGRESS "2;30\a"); + + /* State 0 clears a shown bar. */ + stuff("0", "printf '\\033]9;4;0;0\\007'\n"); + expect(out, PROGRESS "0\a"); + + /* Percent takes leading zeros as in ParseNum(). */ + stuff("0", "printf '\\033]9;4;4;007\\007'\n"); + expect(out, PROGRESS "4;7\a"); + + /* One bar follows its window there and back, twice. */ + select_win(out, "1", PROGRESS "0\a"); + select_win(out, "0", PROGRESS "4;7\a"); + select_win(out, "1", PROGRESS "0\a"); + select_win(out, "0", PROGRESS "4;7\a"); + + /* Two bars swap with no clear between. */ + stuff("1", "printf '\\033]9;4;2;30\\007'\n"); + expect_quiet(out, PROGRESS); + select_win(out, "1", PROGRESS "2;30\a"); + select_win(out, "0", PROGRESS "4;7\a"); + select_win(out, "1", PROGRESS "2;30\a"); + select_win(out, "0", PROGRESS "4;7\a"); + + /* No bars send nothing. */ + stuff("0", "printf '\\033]9;4;0\\007'\n"); + expect(out, PROGRESS "0\a"); + stuff("1", "printf '\\033]9;4;0\\007'\n"); + expect_quiet(out, PROGRESS); + screen_x((char *[]){ "-X", "select", "1", NULL }); + screen_x((char *[]){ "-X", "select", "0", NULL }); + screen_x((char *[]){ "-X", "select", "1", NULL }); + screen_x((char *[]){ "-X", "select", "0", NULL }); + expect_quiet(out, PROGRESS); + + /* ST terminates as sent. */ + stuff("0", "printf '\\033]9;4;1;7\\033\\\\'\n"); + expect(out, PROGRESS "1;7\033\\"); + + /* Detach clears the bar. */ + screen_x((char *[]){ "-X", "detach", NULL }); + expect(out, PROGRESS "0\a"); + + ASSERT(waitpid(pid, &status, 0) == pid); + + /* Reattach sends progress set while detached. */ + stuff("0", "printf '\\033]9;4;3\\007'\n"); + poll(NULL, 0, QUIET_MS); + snprintf(cmd, sizeof(cmd), "./screen -r %s", session); + close(out); + pid = spawn_client(&out, cmd); + expect(out, PROGRESS "3;0\a"); + screen_x((char *[]){ "-X", "detach", NULL }); + ASSERT(waitpid(pid, &status, 0) == pid); + screen_x((char *[]){ "-X", "quit", NULL }); + return 0; +} diff --git a/src/window.h b/src/window.h index e1c1a68..063b0aa 100644 --- a/src/window.h +++ b/src/window.h @@ -222,6 +222,7 @@ struct Window { int w_silencewait; /* wait for silencewait secs */ int w_silence; /* silence status (Lloyd Zusman) */ char w_norefresh; /* dont redisplay when switching to that win */ + char w_progress[16]; /* OSC 9;4 progress state */ char w_xtermosc[5][2560]; /* special xterm/rxvt escapes */ int w_mouse; /* mouse mode 0,9,1000 */ int w_extmouse; /* extended mouse mode 0,1006 */ -- 2.43.0
