From 11cb6824cd2384b4195d969cc89f29c4672ee51c Mon Sep 17 00:00:00 2001 From: Tim Culverhouse Date: Fri, 29 Sep 2023 09:49:09 -0500 Subject: [PATCH] input: don't ESC prefix non-ascii characters MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit User input withe Alt modifier is typically ESC prefixed. Escape prefixing a non-ascii character can cause bugs in some applications. For example in bash, emitting an Alt+ф allows the user to backspace one character into the prompt. This can be repeated multiple times. When a character is outside the ASCII range (exclusive of 0x7F, this is handled as a control sequence), print the character as is, with no prefix. --- src/input/KeyEncoder.zig | 38 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 36 insertions(+), 2 deletions(-) diff --git a/src/input/KeyEncoder.zig b/src/input/KeyEncoder.zig index 85b39baca..bc931fed0 100644 --- a/src/input/KeyEncoder.zig +++ b/src/input/KeyEncoder.zig @@ -279,8 +279,12 @@ fn legacy( // If we have alt-pressed and alt-esc-prefix is enabled, then // we need to prefix the utf8 sequence with an esc. - if (binding_mods.alt and self.alt_esc_prefix) { - return try std.fmt.bufPrint(buf, "\x1B{s}", .{utf8}); + if (binding_mods.alt and + self.alt_esc_prefix and + utf8.len == 1 and + utf8[0] < 0x7F) + { + return try std.fmt.bufPrint(buf, "\x1B{u}", .{utf8[0]}); } return try copyToBuf(buf, utf8); @@ -999,6 +1003,36 @@ test "legacy: ctrl+alt+c" { try testing.expectEqualStrings("\x1b\x03", actual); } +test "legacy: alt+c" { + var buf: [128]u8 = undefined; + var enc: KeyEncoder = .{ + .event = .{ + .key = .c, + .utf8 = "c", + .mods = .{ .alt = true }, + }, + .alt_esc_prefix = true, + }; + + const actual = try enc.legacy(&buf); + try testing.expectEqualStrings("\x1Bc", actual); +} + +test "legacy: alt+ф" { + var buf: [128]u8 = undefined; + var enc: KeyEncoder = .{ + .event = .{ + .key = .f, + .utf8 = "ф", + .mods = .{ .alt = true }, + }, + .alt_esc_prefix = true, + }; + + const actual = try enc.legacy(&buf); + try testing.expectEqualStrings("ф", actual); +} + test "legacy: ctrl+c" { var buf: [128]u8 = undefined; var enc: KeyEncoder = .{