From 55657465a772fee4691a2471375ae7cee15976e8 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Fri, 19 Jul 2024 20:20:49 -0700 Subject: [PATCH] add write_selection_file Based on the work by @gigamaax --- src/Surface.zig | 33 ++++++++++++++++++++++----------- src/input/Binding.zig | 6 ++++++ 2 files changed, 28 insertions(+), 11 deletions(-) diff --git a/src/Surface.zig b/src/Surface.zig index b4fcc2a10..373c18613 100644 --- a/src/Surface.zig +++ b/src/Surface.zig @@ -3329,6 +3329,11 @@ pub fn performBindingAction(self: *Surface, action: input.Binding.Action) !bool v, ), + .write_selection_file => |v| try self.writeScreenFile( + .selection, + v, + ), + .new_window => try self.app.newWindow(self.rt_app, .{ .parent = self }), .new_tab => { @@ -3508,6 +3513,7 @@ fn closingAction(action: input.Binding.Action) bool { /// The portion of the screen to write for writeScreenFile. const WriteScreenLoc = enum { history, + selection, }; fn writeScreenFile( @@ -3534,30 +3540,35 @@ fn writeScreenFile( // the file and write the empty file to the pty so that this // command always works on the primary screen. 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: { // We do not support this for alternate screens // because they don't have scrollback anyways. if (self.io.terminal.active_screen == .alternate) { - tmp_dir.deinit(); - return; + break :history null; } - break :history .{ + break :history terminal.Selection.init( 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( buf_writer.writer(), .{ - .tl = tl, - .br = br orelse { - tmp_dir.deinit(); - return; - }, + .tl = sel.start(), + .br = sel.end(), .unwrap = true, }, ); diff --git a/src/input/Binding.zig b/src/input/Binding.zig index 02b9f79bf..37f478185 100644 --- a/src/input/Binding.zig +++ b/src/input/Binding.zig @@ -211,6 +211,12 @@ pub const Action = union(enum) { /// 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. new_window: void,