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,28 +448,58 @@ fn keyCallback(
_ = scancode; _ = scancode;
// Paste if (action == .press and mods.super) {
if (action == .press and mods.super and key == .v) { switch (key) {
const win = window.getUserPointer(Window) orelse return; // Copy
.c => {
const win = window.getUserPointer(Window) orelse return;
// Ignore this character for writing // Ignore this character for writing
win.ignore_char = true; win.ignore_char = true;
const data = glfw.getClipboardString() catch |err| { // If we have a selection, copy it.
log.warn("error reading clipboard: {}", .{err}); if (win.terminal.selection) |sel| {
return; 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);
if (data.len > 0) { glfw.setClipboardString(buf) catch |err| {
if (win.bracketed_paste) win.queueWrite("\x1B[200~") catch |err| log.err("error setting clipboard string err={}", .{err});
log.err("error queueing write in keyCallback err={}", .{err}); return;
win.queueWrite(data) catch |err| };
log.warn("error pasting clipboard: {}", .{err}); }
if (win.bracketed_paste) win.queueWrite("\x1B[201~") catch |err|
log.err("error queueing write in keyCallback err={}", .{err}); return;
},
// Paste
.v => {
const win = window.getUserPointer(Window) orelse return;
// Ignore this character for writing
win.ignore_char = true;
const data = glfw.getClipboardString() catch |err| {
log.warn("error reading clipboard: {}", .{err});
return;
};
if (data.len > 0) {
if (win.bracketed_paste) win.queueWrite("\x1B[200~") catch |err|
log.err("error queueing write in keyCallback err={}", .{err});
win.queueWrite(data) catch |err|
log.warn("error pasting clipboard: {}", .{err});
if (win.bracketed_paste) win.queueWrite("\x1B[201~") catch |err|
log.err("error queueing write in keyCallback err={}", .{err});
}
return;
},
else => {},
} }
return;
} }
//log.info("KEY {} {} {} {}", .{ key, scancode, mods, action }); //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 /// Returns the raw text associated with a selection. This will unwrap
/// soft-wrapped edges. The returned slice is owned by the caller. /// 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 // Get the slices for the string
const slices = self.selectionSlices(sel); const slices = self.selectionSlices(sel);
@ -424,7 +424,8 @@ pub fn selectionString(self: Screen, alloc: Allocator, sel: Selection) ![]const
break :chars count; 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; var i: usize = 0;
for (slices.top) |cell, idx| { 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..]); i += try std.unicode.utf8Encode(@intCast(u21, char), buf[i..]);
} }
// If we wrote less than what we allocated, try to shrink it. Otherwise // Add null termination
// return the buf as-is. buf[i] = 0;
return if (i < buf.len) try alloc.realloc(buf, i) else buf; return buf[0..i :0];
} }
/// Returns the slices that make up the selection, in order. There are at most /// Returns the slices that make up the selection, in order. There are at most