lua: add window:copy_to_clipboard method

refs: https://github.com/wez/wezterm/issues/2223
This commit is contained in:
Wez Furlong committed 2022-07-05 21:59:00 -07:00
1 parent df1b832fce
commit 741cd37bd4
4 files changed
+44 -1

No files matched your search

+1
View File
@@ -245,6 +245,7 @@ pub enum ClipboardCopyDestination {
PrimarySelection,
ClipboardAndPrimarySelection,
}
impl_lua_conversion_dynamic!(ClipboardCopyDestination);
impl Default for ClipboardCopyDestination {
fn default() -> Self {
+1
View File
@@ -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)
@@ -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")
```
+12 -1
View File
@@ -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<ClipboardCopyDestination>)| {
let clipboard = clipboard.unwrap_or_default();
this.window
.notify(TermWindowNotif::Apply(Box::new(move |term_window| {
term_window.copy_to_clipboard(clipboard, text);
})));
Ok(())
},
);
}
}