input: 0x7F is a control character, use helper

Fixes #556

One check for control chars was missing 0x7F. Since we do this three
times, extract it to a helper and call that.
This commit is contained in:
Mitchell Hashimoto
2023-09-27 07:55:02 -07:00
parent 18b42ce6d5
commit 35a4427f54

View File

@ -115,7 +115,7 @@ fn kitty(
// the real world issue is usually control characters. // the real world issue is usually control characters.
const view = try std.unicode.Utf8View.init(self.event.utf8); const view = try std.unicode.Utf8View.init(self.event.utf8);
var it = view.iterator(); var it = view.iterator();
while (it.nextCodepoint()) |cp| if (cp < 0x20) break :plain_text; while (it.nextCodepoint()) |cp| if (isControl(cp)) break :plain_text;
return try copyToBuf(buf, self.event.utf8); return try copyToBuf(buf, self.event.utf8);
} }
@ -147,7 +147,7 @@ fn kitty(
var it = view.iterator(); var it = view.iterator();
const cp = it.nextCodepoint() orelse break :alternates; const cp = it.nextCodepoint() orelse break :alternates;
if (it.nextCodepoint() != null) break :alternates; if (it.nextCodepoint() != null) break :alternates;
if (cp != seq.key and cp >= 0x20 and cp != 0x7F) { if (cp != seq.key and !isControl(cp)) {
seq.alternates = &.{cp}; seq.alternates = &.{cp};
} }
} }
@ -415,6 +415,11 @@ fn ctrlSeq(keyval: key.Key, mods: key.Mods) ?u8 {
}; };
} }
/// Returns true if this is an ASCII control character, matches libc implementation.
fn isControl(cp: u21) bool {
return cp < 0x20 or cp == 0x7F;
}
/// This is the bitmask for fixterm CSI u modifiers. /// This is the bitmask for fixterm CSI u modifiers.
const CsiUMods = packed struct(u3) { const CsiUMods = packed struct(u3) {
shift: bool = false, shift: bool = false,
@ -592,7 +597,7 @@ const KittySequence = struct {
var count: usize = 0; var count: usize = 0;
while (it.nextCodepoint()) |cp| { while (it.nextCodepoint()) |cp| {
// If the codepoint is non-printable ASCII character, skip. // If the codepoint is non-printable ASCII character, skip.
if (cp < 0x20 or cp == 0x7F) continue; if (isControl(cp)) continue;
// We need to add our ";". We need to add two if we didn't emit // We need to add our ";". We need to add two if we didn't emit
// the modifier section. We only do this initially. // the modifier section. We only do this initially.
@ -814,6 +819,18 @@ test "kitty: enter, backspace, tab" {
} }
} }
test "kitty: delete" {
var buf: [128]u8 = undefined;
{
var enc: KeyEncoder = .{
.event = .{ .key = .delete, .mods = .{}, .utf8 = "\x7F" },
.kitty_flags = .{ .disambiguate = true },
};
const actual = try enc.kitty(&buf);
try testing.expectEqualStrings("\x1b[3~", actual);
}
}
test "kitty: composing with no modifier" { test "kitty: composing with no modifier" {
var buf: [128]u8 = undefined; var buf: [128]u8 = undefined;
var enc: KeyEncoder = .{ var enc: KeyEncoder = .{