Skip to content

Commit

Permalink
add clip copy and clip paste (#1009)
Browse files Browse the repository at this point in the history
# 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
- nushell/nushell#11131
- #674
  • Loading branch information
Bahex authored Jan 5, 2025
1 parent 2dadab7 commit 66c9995
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
2 changes: 1 addition & 1 deletion stdlib-candidate/nupm.nuon
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
35 changes: 35 additions & 0 deletions stdlib-candidate/std-rfc/clip/mod.nu
Original file line number Diff line number Diff line change
@@ -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
}
2 changes: 2 additions & 0 deletions stdlib-candidate/std-rfc/mod.nu
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export module aggregate
export module clip

0 comments on commit 66c9995

Please sign in to comment.