From 66c9995020dcb9c052eb53d3d549503964166b0b Mon Sep 17 00:00:00 2001 From: Bahex Date: Sun, 5 Jan 2025 15:52:46 +0300 Subject: [PATCH] add `clip copy` and `clip paste` (#1009) # Description Add `clip copy` and `clip paste` for interacting with system clipboard, making use of OSC 52. I'm not sure how to write tests for these commands. > [!TIP] > No platform specific external binary is required. > [!WARNING] > - Not all terminal emulators will support this > - Terminal multiplexers may interfere with it, depending on their configuration. # Related - https://github.com/nushell/nushell/pull/11131 - https://github.com/nushell/nu_scripts/pull/674 --- stdlib-candidate/nupm.nuon | 2 +- stdlib-candidate/std-rfc/clip/mod.nu | 35 ++++++++++++++++++++++++++++ stdlib-candidate/std-rfc/mod.nu | 2 ++ 3 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 stdlib-candidate/std-rfc/clip/mod.nu create mode 100644 stdlib-candidate/std-rfc/mod.nu diff --git a/stdlib-candidate/nupm.nuon b/stdlib-candidate/nupm.nuon index a7847e6cb..edce6a091 100644 --- a/stdlib-candidate/nupm.nuon +++ b/stdlib-candidate/nupm.nuon @@ -3,6 +3,6 @@ description: "Official candidates for Nushell standard library" documentation: "https://github.com/nushell/nu_scripts/blob/main/stdlib-candidate/std-rfc/README.md" license: "https://github.com/nushell/nu_scripts/blob/main/LICENSE" - version: 0.4.1 + version: 0.4.2 type: "module" } diff --git a/stdlib-candidate/std-rfc/clip/mod.nu b/stdlib-candidate/std-rfc/clip/mod.nu new file mode 100644 index 000000000..170b8eb94 --- /dev/null +++ b/stdlib-candidate/std-rfc/clip/mod.nu @@ -0,0 +1,35 @@ +# Commands for interacting with the system clipboard +# +# > These commands require your terminal to support OSC 52 +# > Terminal multiplexers such as screen, tmux, zellij etc may interfere with this command + +# Copy input to system clipboard +# +# # Example +# ```nushell +# >_ "Hello" | clip copy +# ``` +export def copy []: [string -> nothing] { + print -n $'(ansi osc)52;c;($in | encode base64)(ansi st)' +} + +# Paste contenst of system clipboard +# +# # Example +# ```nushell +# >_ clip paste +# "Hello" +# ``` +export def paste []: [nothing -> string] { + try { + term query $'(ansi osc)52;c;?(ansi st)' -p $'(ansi osc)52;c;' -t (ansi st) + } catch { + error make -u { + msg: "Terminal did not responds to OSC 52 paste request." + help: $"Check if your terminal supports OSC 52." + } + } + | decode + | decode base64 + | decode +} diff --git a/stdlib-candidate/std-rfc/mod.nu b/stdlib-candidate/std-rfc/mod.nu new file mode 100644 index 000000000..5cbe4c364 --- /dev/null +++ b/stdlib-candidate/std-rfc/mod.nu @@ -0,0 +1,2 @@ +export module aggregate +export module clip