From 88d055452b81dab01449a75d35d4177ebf59633e Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Thu, 11 Jul 2024 19:02:33 -0700 Subject: [PATCH] terminal: tmux enter/exit --- src/terminal/dcs.zig | 39 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/src/terminal/dcs.zig b/src/terminal/dcs.zig index 06e4d9437..883be90e1 100644 --- a/src/terminal/dcs.zig +++ b/src/terminal/dcs.zig @@ -48,6 +48,21 @@ pub const Handler = struct { fn tryHook(alloc: Allocator, dcs: DCS) !?Hook { return switch (dcs.intermediates.len) { + 0 => switch (dcs.final) { + // Tmux control mode + 'p' => tmux: { + // Tmux control mode must start with ESC P 1000 p + if (dcs.params.len != 1 or dcs.params[0] != 1000) break :tmux null; + + break :tmux .{ + .state = .{ .tmux = {} }, + .command = .{ .tmux = .{ .enter = {} } }, + }; + }, + + else => null, + }, + 1 => switch (dcs.intermediates[0]) { '+' => switch (dcs.final) { // XTGETTCAP @@ -128,7 +143,7 @@ pub const Handler = struct { .ignore, => null, - .tmux => null, + .tmux => .{ .tmux = .{ .exit = {} } }, .xtgettcap => |list| .{ .xtgettcap = .{ .data = list } }, @@ -340,3 +355,25 @@ test "DECRQSS invalid command" { _ = h.put('q'); try testing.expect(h.unhook() == null); } + +test "tmux enter and implicit exit" { + const testing = std.testing; + const alloc = testing.allocator; + + var h: Handler = .{}; + defer h.deinit(); + + { + var cmd = h.hook(alloc, .{ .params = &.{1000}, .final = 'p' }).?; + defer cmd.deinit(); + try testing.expect(cmd == .tmux); + try testing.expect(cmd.tmux == .enter); + } + + { + var cmd = h.unhook().?; + defer cmd.deinit(); + try testing.expect(cmd == .tmux); + try testing.expect(cmd.tmux == .exit); + } +}