input: encoding should always write to the buf

This commit is contained in:
Mitchell Hashimoto
2023-08-16 10:36:22 -07:00
parent aadb78394b
commit c254a8b09e
2 changed files with 13 additions and 5 deletions

View File

@ -11,7 +11,7 @@ const testing = std.testing;
const key = @import("key.zig");
const function_keys = @import("function_keys.zig");
event: key.Event,
event: key.KeyEvent,
/// The state of various modes of a terminal that impact encoding.
alt_esc_prefix: bool = false,
@ -46,7 +46,7 @@ pub fn legacy(
self.cursor_key_application,
self.keypad_key_application,
self.modify_other_keys_state_2,
)) |sequence| return sequence;
)) |sequence| return copyToBuf(buf, sequence);
// If we match a control sequence, we output that directly.
if (ctrlSeq(self.event.key, binding_mods)) |char| {
@ -154,7 +154,15 @@ pub fn legacy(
return try std.fmt.bufPrint(buf, "\x1B{s}", .{utf8});
}
return utf8;
return try copyToBuf(buf, utf8);
}
/// A helper to memcpy a src value to a buffer and return the result.
fn copyToBuf(buf: []u8, src: []const u8) ![]const u8 {
if (src.len > buf.len) return error.OutOfMemory;
const result = buf[0..src.len];
@memcpy(result, src);
return result;
}
/// Determines whether the key should be encoded in the xterm

View File

@ -9,7 +9,7 @@ const Allocator = std.mem.Allocator;
/// as GLFW. In this case, the apprt should provide as much information
/// as it can and it should be expected that the terminal behavior
/// will not be totally correct.
pub const Event = struct {
pub const KeyEvent = struct {
/// The action: press, release, etc.
action: Action = .press,
@ -43,7 +43,7 @@ pub const Event = struct {
/// Returns the effective modifiers for this event. The effective
/// modifiers are the mods that should be considered for keybindings.
pub fn effectiveMods(self: Event) Mods {
pub fn effectiveMods(self: KeyEvent) Mods {
if (self.utf8.len == 0) return self.mods;
return self.mods.unset(self.consumed_mods);
}