Commit 883b02d

David van Munster  ·  2026-08-13 10:13:16 -0400 EDT
parent a959c5a
fix: write/reset osc 10/11/12 on attach/detach
2 files changed,  +26, -2
+3, -2
......@@ -1312,12 +1312,14 @@ fn attach(gpa: std.mem.Allocator, io: std.Io, daemon: *Daemon) !void {
13121312 var orig_termios: cross.c.termios = undefined;
13131313 const stdin_is_tty = cross.c.tcgetattr(lib_posix.STDIN_FILENO, &orig_termios) == 0;
13141314
1315+ // RIS, OSC 10/11/12
1316+ const restore_seq = "\x1bc\x1b]110\x1b\\\x1b]111\x1b\\\x1b]112\x1b\\";
1317+
13151318 defer {
13161319 if (stdin_is_tty) {
13171320 _ = cross.c.tcsetattr(lib_posix.STDIN_FILENO, cross.c.TCSAFLUSH, &orig_termios);
13181321 }
13191322 // Reset terminal modes on detach
1320- const restore_seq = "\x1bc";
13211323 _ = lib_posix.write(lib_posix.STDOUT_FILENO, restore_seq) catch {};
13221324 }
13231325
......@@ -1350,7 +1352,6 @@ fn attach(gpa: std.mem.Allocator, io: std.Io, daemon: *Daemon) !void {
13501352 .switch_session => {
13511353 if (looper.session_name) |session_name| {
13521354 // Reset terminal modes when switching sessions
1353- const restore_seq = "\x1bc";
13541355 _ = lib_posix.write(lib_posix.STDOUT_FILENO, restore_seq) catch {};
13551356
13561357 const target_path = socket.getSocketPath(
+23, -0
......@@ -746,6 +746,26 @@ fn writePwd(writer: *std.Io.Writer, term: *const ghostty_vt.Terminal) void {
746746 };
747747 }
748748
749+fn writeDynamicColor(
750+ writer: *std.Io.Writer,
751+ kind: ghostty_vt.color.Dynamic,
752+ dynamic: ghostty_vt.color.DynamicRGB,
753+) void {
754+ const c = dynamic.override orelse return;
755+ writer.print("\x1b]{d};rgb:{x:0>2}/{x:0>2}/{x:0>2}\x1b\\", .{
756+ @intFromEnum(kind), c.r, c.g, c.b,
757+ }) catch |err| {
758+ std.log.warn("failed to format dynamic color err={s}", .{@errorName(err)});
759+ };
760+}
761+
762+fn writeColorOverrides(writer: *std.Io.Writer, term: *const ghostty_vt.Terminal) void {
763+ const colors = &term.colors;
764+ writeDynamicColor(writer, .foreground, colors.foreground);
765+ writeDynamicColor(writer, .background, colors.background);
766+ writeDynamicColor(writer, .cursor, colors.cursor);
767+}
768+
749769 pub fn serializeTerminalState(alloc: std.mem.Allocator, term: *ghostty_vt.Terminal) ?[]const u8 {
750770 var builder: std.Io.Writer.Allocating = .init(alloc);
751771 defer builder.deinit();
......@@ -760,6 +780,9 @@ pub fn serializeTerminalState(alloc: std.mem.Allocator, term: *ghostty_vt.Termin
760780 term.modes.set(.synchronized_output, false);
761781 }
762782
783+ // If state contains color override, restore it.
784+ writeColorOverrides(&builder.writer, term);
785+
763786 const pages = &term.screens.active.pages;
764787 const screen_top = pages.getTopLeft(.screen);
765788 const active_top = pages.getTopLeft(.active);