input: allocate for text bindings

This commit is contained in:
Mitchell Hashimoto
2023-11-24 10:36:43 -08:00
parent 0e2970bdeb
commit 6fc0d2d4a8
2 changed files with 33 additions and 9 deletions

View File

@ -2271,7 +2271,7 @@ pub fn performBindingAction(self: *Surface, action: input.Binding.Action) !bool
.reload_config => try self.app.reloadConfig(self.rt_app),
.csi, .esc, .text => |data| {
.csi, .esc => |data| {
// We need to send the CSI/ESC sequence as a single write request.
// If you split it across two then the shell can interpret it
// as two literals.
@ -2279,10 +2279,6 @@ pub fn performBindingAction(self: *Surface, action: input.Binding.Action) !bool
const full_data = switch (action) {
.csi => try std.fmt.bufPrint(&buf, "\x1b[{s}", .{data}),
.esc => try std.fmt.bufPrint(&buf, "\x1b{s}", .{data}),
.text => configpkg.string.parse(&buf, data) catch |err| {
log.warn("error parsing text binding text={s} err={}", .{ data, err });
return true;
},
else => unreachable,
};
_ = self.io_thread.mailbox.push(try termio.Message.writeReq(
@ -2301,6 +2297,34 @@ pub fn performBindingAction(self: *Surface, action: input.Binding.Action) !bool
}
},
.text => |data| {
// For text we always allocate just because its easier to
// handle all cases that way.
var buf = try self.alloc.alloc(u8, data.len);
defer self.alloc.free(buf);
const text = configpkg.string.parse(buf, data) catch |err| {
log.warn(
"error parsing text binding text={s} err={}",
.{ data, err },
);
return true;
};
_ = self.io_thread.mailbox.push(try termio.Message.writeReq(
self.alloc,
text,
), .{ .forever = {} });
try self.io_thread.wakeup.notify();
// Text triggers a scroll.
{
self.renderer_state.mutex.lock();
defer self.renderer_state.mutex.unlock();
self.scrollToBottom() catch |err| {
log.warn("error scrolling to bottom err={}", .{err});
};
}
},
.cursor_key => |ck| {
// We send a different sequence depending on if we're
// in cursor keys mode. We're in "normal" mode if cursor

View File

@ -134,10 +134,10 @@ pub const Action = union(enum) {
/// Send an ESC sequence.
esc: []const u8,
// Send the given text. Uses Zig string literal syntax. The maximum
// length of the string is 128 bytes. This is currently not validated.
// If the text is invalid (i.e. contains an invalid escape sequence),
// the error will currently only show up in logs.
// Send the given text. Uses Zig string literal syntax. This is
// currently not validated. If the text is invalid (i.e. contains
// an invalid escape sequence), the error will currently only show
// up in logs.
text: []const u8,
/// Send data to the pty depending on whether cursor key mode is