input: don't ESC prefix non-ascii characters

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.
This commit is contained in:
Tim Culverhouse
2023-09-29 09:49:09 -05:00
committed by Mitchell Hashimoto
parent 3ef67fe025
commit 11cb6824cd

View File

@ -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 = .{