This commit is contained in:
Mitchell Hashimoto
2022-08-05 11:37:02 -07:00
parent abde2b9e18
commit d4c4330d4d
2 changed files with 54 additions and 23 deletions

View File

@ -448,8 +448,34 @@ fn keyCallback(
_ = scancode;
if (action == .press and mods.super) {
switch (key) {
// Copy
.c => {
const win = window.getUserPointer(Window) orelse return;
// Ignore this character for writing
win.ignore_char = true;
// If we have a selection, copy it.
if (win.terminal.selection) |sel| {
var buf = win.terminal.screen.selectionString(win.alloc, sel) catch |err| {
log.err("error reading selection string err={}", .{err});
return;
};
defer win.alloc.free(buf);
glfw.setClipboardString(buf) catch |err| {
log.err("error setting clipboard string err={}", .{err});
return;
};
}
return;
},
// Paste
if (action == .press and mods.super and key == .v) {
.v => {
const win = window.getUserPointer(Window) orelse return;
// Ignore this character for writing
@ -470,6 +496,10 @@ fn keyCallback(
}
return;
},
else => {},
}
}
//log.info("KEY {} {} {} {}", .{ key, scancode, mods, action });

View File

@ -402,7 +402,7 @@ pub fn resize(self: *Screen, alloc: Allocator, rows: usize, cols: usize) !void {
/// Returns the raw text associated with a selection. This will unwrap
/// soft-wrapped edges. The returned slice is owned by the caller.
pub fn selectionString(self: Screen, alloc: Allocator, sel: Selection) ![]const u8 {
pub fn selectionString(self: Screen, alloc: Allocator, sel: Selection) ![:0]const u8 {
// Get the slices for the string
const slices = self.selectionSlices(sel);
@ -424,7 +424,8 @@ pub fn selectionString(self: Screen, alloc: Allocator, sel: Selection) ![]const
break :chars count;
};
const buf = try alloc.alloc(u8, chars + newlines);
const buf = try alloc.alloc(u8, chars + newlines + 1);
errdefer alloc.free(buf);
var i: usize = 0;
for (slices.top) |cell, idx| {
@ -465,9 +466,9 @@ pub fn selectionString(self: Screen, alloc: Allocator, sel: Selection) ![]const
i += try std.unicode.utf8Encode(@intCast(u21, char), buf[i..]);
}
// If we wrote less than what we allocated, try to shrink it. Otherwise
// return the buf as-is.
return if (i < buf.len) try alloc.realloc(buf, i) else buf;
// Add null termination
buf[i] = 0;
return buf[0..i :0];
}
/// Returns the slices that make up the selection, in order. There are at most