From 283c94f874d3e2734c7d81bfe3095c9549a277c0 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Wed, 31 May 2023 15:38:55 -0700 Subject: [PATCH] terminal: parse OSC 133 A redraw like kitty --- src/terminal/osc.zig | 51 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/src/terminal/osc.zig b/src/terminal/osc.zig index 1c6e796c3..3063bed40 100644 --- a/src/terminal/osc.zig +++ b/src/terminal/osc.zig @@ -26,6 +26,7 @@ pub const Command = union(enum) { /// not all shells will send the prompt end code. prompt_start: struct { aid: ?[]const u8 = null, + redraw: bool = true, }, /// End of prompt and start of user input, terminated by a OSC "133;C" @@ -345,6 +346,29 @@ pub const Parser = struct { .prompt_start => |*v| v.aid = value, else => {}, } + } else if (mem.eql(u8, self.temp_state.key, "redraw")) { + // Kitty supports a "redraw" option for prompt_start. I can't find + // this documented anywhere but can see in the code that this is used + // by shell environments to tell the terminal that the shell will NOT + // redraw the prompt so we should attempt to resize it. + switch (self.command) { + .prompt_start => |*v| { + const valid = if (value.len == 1) valid: { + switch (value[0]) { + '0' => v.redraw = false, + '1' => v.redraw = true, + else => break :valid false, + } + + break :valid true; + } else false; + + if (!valid) { + log.info("OSC 133 A invalid redraw value: {s}", .{value}); + } + }, + else => {}, + } } else log.info("unknown semantic prompts option: {s}", .{self.temp_state.key}); } @@ -416,6 +440,7 @@ test "OSC: prompt_start" { const cmd = p.end().?; try testing.expect(cmd == .prompt_start); try testing.expect(cmd.prompt_start.aid == null); + try testing.expect(cmd.prompt_start.redraw); } test "OSC: prompt_start with single option" { @@ -431,6 +456,32 @@ test "OSC: prompt_start with single option" { try testing.expectEqualStrings("14", cmd.prompt_start.aid.?); } +test "OSC: prompt_start with redraw disabled" { + const testing = std.testing; + + var p: Parser = .{}; + + const input = "133;A;redraw=0"; + for (input) |ch| p.next(ch); + + const cmd = p.end().?; + try testing.expect(cmd == .prompt_start); + try testing.expect(!cmd.prompt_start.redraw); +} + +test "OSC: prompt_start with redraw invalid value" { + const testing = std.testing; + + var p: Parser = .{}; + + const input = "133;A;redraw=42"; + for (input) |ch| p.next(ch); + + const cmd = p.end().?; + try testing.expect(cmd == .prompt_start); + try testing.expect(cmd.prompt_start.redraw); +} + test "OSC: end_of_command no exit code" { const testing = std.testing;