add write_selection_file

Based on the work by @gigamaax
This commit is contained in:
Mitchell Hashimoto
2024-07-19 20:20:49 -07:00
parent 10198d88dc
commit 55657465a7
2 changed files with 28 additions and 11 deletions

View File

@ -3329,6 +3329,11 @@ pub fn performBindingAction(self: *Surface, action: input.Binding.Action) !bool
v, v,
), ),
.write_selection_file => |v| try self.writeScreenFile(
.selection,
v,
),
.new_window => try self.app.newWindow(self.rt_app, .{ .parent = self }), .new_window => try self.app.newWindow(self.rt_app, .{ .parent = self }),
.new_tab => { .new_tab => {
@ -3508,6 +3513,7 @@ fn closingAction(action: input.Binding.Action) bool {
/// The portion of the screen to write for writeScreenFile. /// The portion of the screen to write for writeScreenFile.
const WriteScreenLoc = enum { const WriteScreenLoc = enum {
history, history,
selection,
}; };
fn writeScreenFile( fn writeScreenFile(
@ -3534,30 +3540,35 @@ fn writeScreenFile(
// the file and write the empty file to the pty so that this // the file and write the empty file to the pty so that this
// command always works on the primary screen. // command always works on the primary screen.
const pages = &self.io.terminal.screen.pages; const pages = &self.io.terminal.screen.pages;
const tl: terminal.Pin, const br: ?terminal.Pin = switch (loc) { const sel_: ?terminal.Selection = switch (loc) {
.history => history: { .history => history: {
// We do not support this for alternate screens // We do not support this for alternate screens
// because they don't have scrollback anyways. // because they don't have scrollback anyways.
if (self.io.terminal.active_screen == .alternate) { if (self.io.terminal.active_screen == .alternate) {
tmp_dir.deinit(); break :history null;
return;
} }
break :history .{ break :history terminal.Selection.init(
pages.getTopLeft(.history), pages.getTopLeft(.history),
pages.getBottomRight(.history), pages.getBottomRight(.history) orelse
}; break :history null,
false,
);
}, },
.selection => self.io.terminal.screen.selection,
}; };
const sel = sel_ orelse {
// If we have no selection we have no data so we do nothing.
tmp_dir.deinit();
return;
};
try self.io.terminal.screen.dumpString( try self.io.terminal.screen.dumpString(
buf_writer.writer(), buf_writer.writer(),
.{ .{
.tl = tl, .tl = sel.start(),
.br = br orelse { .br = sel.end(),
tmp_dir.deinit();
return;
},
.unwrap = true, .unwrap = true,
}, },
); );

View File

@ -211,6 +211,12 @@ pub const Action = union(enum) {
/// ///
write_scrollback_file: WriteScreenAction, write_scrollback_file: WriteScreenAction,
/// Same as write_scrollback_file but writes the selected text.
/// If there is no selected text this does nothing (it doesn't
/// even create an empty file). See write_scrollback_file for
/// available values.
write_selection_file: WriteScreenAction,
/// Open a new window. /// Open a new window.
new_window: void, new_window: void,