From 9ad27924a62307c583625bf4994d317a4303363d Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Wed, 20 Sep 2023 14:33:55 -0700 Subject: [PATCH] input: correct xterm encoding for modified F1-F4 Fixes #499 --- src/input/KeyEncoder.zig | 46 +++++++++++++++++++++++++++++++++++++ src/input/function_keys.zig | 8 +++---- 2 files changed, 50 insertions(+), 4 deletions(-) diff --git a/src/input/KeyEncoder.zig b/src/input/KeyEncoder.zig index 51b80197a..4be5adf31 100644 --- a/src/input/KeyEncoder.zig +++ b/src/input/KeyEncoder.zig @@ -1106,6 +1106,52 @@ test "legacy: keypad enter" { try testing.expectEqualStrings("\r", actual); } +test "legacy: f1" { + var buf: [128]u8 = undefined; + var enc: KeyEncoder = .{ + .event = .{ + .key = .f1, + .mods = .{ .ctrl = true }, + .consumed_mods = .{}, + }, + }; + + // F1 + { + enc.event.key = .f1; + const actual = try enc.legacy(&buf); + try testing.expectEqualStrings("\x1b[1;5P", actual); + } + + // F2 + { + enc.event.key = .f2; + const actual = try enc.legacy(&buf); + try testing.expectEqualStrings("\x1b[1;5Q", actual); + } + + // F3 + { + enc.event.key = .f3; + const actual = try enc.legacy(&buf); + try testing.expectEqualStrings("\x1b[1;5R", actual); + } + + // F4 + { + enc.event.key = .f4; + const actual = try enc.legacy(&buf); + try testing.expectEqualStrings("\x1b[1;5S", actual); + } + + // F5 uses new encoding + { + enc.event.key = .f5; + const actual = try enc.legacy(&buf); + try testing.expectEqualStrings("\x1b[15;5~", actual); + } +} + test "ctrlseq: normal ctrl c" { const seq = ctrlSeq(.c, .{ .ctrl = true }); try testing.expectEqual(@as(u8, 0x03), seq.?); diff --git a/src/input/function_keys.zig b/src/input/function_keys.zig index 9c6c486d3..9721130d1 100644 --- a/src/input/function_keys.zig +++ b/src/input/function_keys.zig @@ -87,10 +87,10 @@ pub const keys = keys: { result.set(.page_down, pcStyle("\x1b[6;{}~") ++ .{.{ .sequence = "\x1B[6~" }}); // Function Keys. todo: f13-f35 but we need to add to input.Key - result.set(.f1, pcStyle("\x1b[11;{}~") ++ .{.{ .sequence = "\x1BOP" }}); - result.set(.f2, pcStyle("\x1b[12;{}~") ++ .{.{ .sequence = "\x1BOQ" }}); - result.set(.f3, pcStyle("\x1b[13;{}~") ++ .{.{ .sequence = "\x1BOR" }}); - result.set(.f4, pcStyle("\x1b[14;{}~") ++ .{.{ .sequence = "\x1BOS" }}); + result.set(.f1, pcStyle("\x1b[1;{}P") ++ .{.{ .sequence = "\x1BOP" }}); + result.set(.f2, pcStyle("\x1b[1;{}Q") ++ .{.{ .sequence = "\x1BOQ" }}); + result.set(.f3, pcStyle("\x1b[1;{}R") ++ .{.{ .sequence = "\x1BOR" }}); + result.set(.f4, pcStyle("\x1b[1;{}S") ++ .{.{ .sequence = "\x1BOS" }}); result.set(.f5, pcStyle("\x1b[15;{}~") ++ .{.{ .sequence = "\x1B[15~" }}); result.set(.f6, pcStyle("\x1b[17;{}~") ++ .{.{ .sequence = "\x1B[17~" }}); result.set(.f7, pcStyle("\x1b[18;{}~") ++ .{.{ .sequence = "\x1B[18~" }});