add "text" action

This commit is contained in:
Guillaume Wenzek
2023-11-24 17:21:57 +01:00
parent 9ee1c939fd
commit 223accb4c8
2 changed files with 43 additions and 2 deletions

View File

@ -2254,6 +2254,39 @@ fn showMouse(self: *Surface) void {
self.rt_surface.setMouseVisibility(true);
}
pub fn parseStringLiteral(out: []u8, bytes: []const u8) []u8 {
var offset: usize = 0;
var index: usize = 0;
while (true) {
if (index >= bytes.len or offset >= out.len) break;
const b = bytes[index];
switch (b) {
'\\' => {
const escape_char_index = index + 1;
const result = std.zig.string_literal.parseEscapeSequence(bytes, &index);
switch (result) {
.success => |codepoint| {
if (bytes[escape_char_index] == 'u') {
const len = std.unicode.utf8Encode(codepoint, out[offset..]) catch break;
offset += len;
} else {
out[offset] = @as(u8, @intCast(codepoint));
offset += 1;
}
},
.failure => break,
}
},
else => {
out[offset] = b;
offset += 1;
index += 1;
},
}
}
return out[0..offset];
}
/// Perform a binding action. A binding is a keybinding. This function
/// must be called from the GUI thread.
///
@ -2271,12 +2304,17 @@ pub fn performBindingAction(self: *Surface, action: input.Binding.Action) !bool
.reload_config => try self.app.reloadConfig(self.rt_app),
.csi, .esc => |data| {
.csi, .esc, .text => |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.
var buf: [128]u8 = undefined;
const full_data = try std.fmt.bufPrint(&buf, "\x1b{s}{s}", .{ if (action == .csi) "[" else "", data });
const full_data = switch (action) {
.csi => try std.fmt.bufPrint(&buf, "\x1b[{s}", .{data}),
.esc => try std.fmt.bufPrint(&buf, "\x1b{s}", .{data}),
.text => parseStringLiteral(&buf, data),
else => unreachable,
};
_ = self.io_thread.mailbox.push(try termio.Message.writeReq(
self.alloc,
full_data,

View File

@ -134,6 +134,9 @@ pub const Action = union(enum) {
/// Send an ESC sequence.
esc: []const u8,
// Send the given text. Uses Zig string literal syntax.
text: []const u8,
/// Send data to the pty depending on whether cursor key mode is
/// enabled ("application") or disabled ("normal").
cursor_key: CursorKey,