diff --git a/crates/nu-std/src/lib.rs b/crates/nu-std/src/lib.rs index fb7c79f9c4..f60f8648a5 100644 --- a/crates/nu-std/src/lib.rs +++ b/crates/nu-std/src/lib.rs @@ -64,6 +64,7 @@ pub fn load_standard_library( "std/testing", include_str!("../std/testing/mod.nu"), ), + ("mod.nu", "std/clip", include_str!("../std/clip/mod.nu")), ]; for (filename, std_subdir_name, content) in std_submodules.drain(..) { diff --git a/crates/nu-std/std-rfc/clip/mod.nu b/crates/nu-std/std-rfc/clip/mod.nu index 44cc4cbaa1..bc33eaf407 100644 --- a/crates/nu-std/std-rfc/clip/mod.nu +++ b/crates/nu-std/std-rfc/clip/mod.nu @@ -1,48 +1,38 @@ +# The clip module has been added to std. This std-rfc module is deprecated and will be removed. +# # 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 +module std-clip { + export use std/clip * +} + +use std-clip + # Copy input to system clipboard +@deprecated "The clip module has been moved to std. Please use the std version instead of the std-rfc version." --since 0.105.0 --remove 0.107.0 @example "Copy a string to the clipboard" { "Hello" | clip copy } export def copy [ --ansi (-a) # Copy ansi formatting ]: any -> nothing { - let input = $in | collect - if not $ansi { - $env.config.use_ansi_coloring = false - } - let text = match ($input | describe -d | get type) { - $type if $type in [ table, record, list ] => { - $input | table -e - } - _ => {$input} - } - - print -n $'(ansi osc)52;c;($text | encode base64)(ansi st)' + std-clip copy --ansi=$ansi } # Paste contents of system clipboard +@deprecated "The clip module has been moved to std. Please use the std version instead of the std-rfc version." --since 0.105.0 --remove 0.107.0 @example "Paste a string from the clipboard" { clip paste } --result "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 + std-clip paste } # Add a prefix to each line of the content to be copied +@deprecated "The clip module has been moved to std. Please use the std version instead of the std-rfc version." --since 0.105.0 --remove 0.107.0 @example "Format output for Nushell doc" { [1 2 3] | clip prefix '# => ' } --result "# => ╭───┬───╮ @@ -55,12 +45,5 @@ export def paste []: [nothing -> string] { ls | clip prefix '# => ' | clip copy } export def prefix [prefix: string]: any -> string { - let input = $in | collect - match ($input | describe -d | get type) { - $type if $type in [ table, record, list ] => { - $input | table -e - } - _ => {$input} - } - | str replace -r --all '(?m)(.*)' $'($prefix)$1' + std-clip prefix $prefix } diff --git a/crates/nu-std/std/clip/mod.nu b/crates/nu-std/std/clip/mod.nu new file mode 100644 index 0000000000..ef2da0bfd4 --- /dev/null +++ b/crates/nu-std/std/clip/mod.nu @@ -0,0 +1,66 @@ +# 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 "Copy a string to the clipboard" { + "Hello" | clip copy +} +export def copy [ + --ansi (-a) # Copy ansi formatting +]: any -> nothing { + let input = $in | collect + if not $ansi { + $env.config.use_ansi_coloring = false + } + let text = match ($input | describe -d | get type) { + $type if $type in [ table, record, list ] => { + $input | table -e + } + _ => {$input} + } + + print -n $'(ansi osc)52;c;($text | encode base64)(ansi st)' +} + +# Paste contents of system clipboard +@example "Paste a string from the clipboard" { + clip paste +} --result "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 +} + +# Add a prefix to each line of the content to be copied +@example "Format output for Nushell doc" { + [1 2 3] | clip prefix '# => ' +} --result "# => ╭───┬───╮ +# => │ 0 │ 1 │ +# => │ 1 │ 2 │ +# => │ 2 │ 3 │ +# => ╰───┴───╯ +# => " +@example "Format output for Nushell doc and copy it" { + ls | clip prefix '# => ' | clip copy +} +export def prefix [prefix: string]: any -> string { + let input = $in | collect + match ($input | describe -d | get type) { + $type if $type in [ table, record, list ] => { + $input | table -e + } + _ => {$input} + } + | str replace -r --all '(?m)(.*)' $'($prefix)$1' +} diff --git a/crates/nu-std/testing.nu b/crates/nu-std/testing.nu index 9f9ee75843..624a927c67 100644 --- a/crates/nu-std/testing.nu +++ b/crates/nu-std/testing.nu @@ -37,11 +37,16 @@ def get-annotated [ source `($file)` scope commands | select name attributes - | where attributes != [] | to nuon ' | from nuon - | update attributes { get name | each {|x| $valid_annotations | get -i $x } | first } + | each {|e| + # filter commands with test attributes, and map attributes to annotation name + let test_attributes = $e.attributes.name | each {|x| $valid_annotations | get -i $x } + if ($test_attributes | is-not-empty) { + $e | update attributes $test_attributes.0 + } + } | rename function_name annotation }