From 741cd37bd444ae2d4aa7fae4118beb8266ea3397 Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Mon, 4 Jul 2022 22:07:47 -0700 Subject: [PATCH] lua: add window:copy_to_clipboard method refs: https://github.com/wez/wezterm/issues/2223 --- config/src/keyassignment.rs | 1 + docs/changelog.md | 1 + docs/config/lua/window/copy_to_clipboard.md | 30 +++++++++++++++++++++ wezterm-gui/src/scripting/guiwin.rs | 13 ++++++++- 4 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 docs/config/lua/window/copy_to_clipboard.md diff --git a/config/src/keyassignment.rs b/config/src/keyassignment.rs index 29cfe27ab..770fc62d1 100644 --- a/config/src/keyassignment.rs +++ b/config/src/keyassignment.rs @@ -245,6 +245,7 @@ pub enum ClipboardCopyDestination { PrimarySelection, ClipboardAndPrimarySelection, } +impl_lua_conversion_dynamic!(ClipboardCopyDestination); impl Default for ClipboardCopyDestination { fn default() -> Self { diff --git a/docs/changelog.md b/docs/changelog.md index be0c605fa..fd0cd272a 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -13,6 +13,7 @@ As features stabilize some brief notes about them will accumulate here. #### New * [ActivateKeyTable](config/lua/keyassignment/ActivateKeyTable.md) now supports `until_unknown=true` to implicitly pop the table when a key not defined by that table is pressed. [#2178](https://github.com/wez/wezterm/issues/2178) +* [window:copy_to_clipboard](config/lua/window/copy_to_clipboard.md) method for putting arbitrary text into the clipboard/selection. #### Fixed * [ActivateKeyTable](config/lua/keyassignment/ActivateKeyTable.md)'s `replace_current` field was not actually optional. Made it optional. [#2179](https://github.com/wez/wezterm/issues/2179) diff --git a/docs/config/lua/window/copy_to_clipboard.md b/docs/config/lua/window/copy_to_clipboard.md new file mode 100644 index 000000000..4f7171afa --- /dev/null +++ b/docs/config/lua/window/copy_to_clipboard.md @@ -0,0 +1,30 @@ +# window:copy_to_clipboard(text \[,clipboard\]) + +*Since: nightly builds only* + +Puts `text` into the specified `clipboard`. + +Clipboard can be one of: + +* `"Clipboard"` - the system clipboard +* `"PrimarySelection"` - the primary selection buffer (applicable to X11 and some Wayland systems only) +* `"ClipboardAndPrimarySelection"` - both the system clipboard and the primary selection. This is the default if you don't specify the clipboard. + +Note that updating the clipboard is asynchronous; this method will return +immediately while the clipboard is updated a few moments later in another +thread. If you need to ensure that the published text is visible to other +applications before you trigger some other action in your lua code then you may +need to add a short sleep to allow for that to complete. + +```lua +window:set_clipboard("put this text in the clipboard and primary selection!") +``` + +```lua +window:set_clipboard("put me in the clipboard only", "Clipboard") +``` + +```lua +window:set_clipboard("put me in the primary selection", "PrimarySelection") +``` + diff --git a/wezterm-gui/src/scripting/guiwin.rs b/wezterm-gui/src/scripting/guiwin.rs index cf162cd53..4a2014614 100644 --- a/wezterm-gui/src/scripting/guiwin.rs +++ b/wezterm-gui/src/scripting/guiwin.rs @@ -3,7 +3,7 @@ use super::luaerr; use super::pane::PaneObject; use crate::termwindow::TermWindowNotif; use crate::TermWindow; -use config::keyassignment::KeyAssignment; +use config::keyassignment::{ClipboardCopyDestination, KeyAssignment}; use luahelper::*; use mlua::{UserData, UserDataMethods}; use mux::window::WindowId as MuxWindowId; @@ -188,5 +188,16 @@ impl UserData for GuiWin { .map_err(luaerr)?; Ok(mux.active_workspace().to_string()) }); + methods.add_method( + "copy_to_clipboard", + |_, this, (text, clipboard): (String, Option)| { + let clipboard = clipboard.unwrap_or_default(); + this.window + .notify(TermWindowNotif::Apply(Box::new(move |term_window| { + term_window.copy_to_clipboard(clipboard, text); + }))); + Ok(()) + }, + ); } }