Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Additional clip copy options #1028

Merged
merged 2 commits into from
Jan 31, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 38 additions & 2 deletions stdlib-candidate/std-rfc/clip/mod.nu
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,29 @@
# ```nushell
# >_ "Hello" | clip copy
# ```
export def copy []: [string -> nothing] {
print -n $'(ansi osc)52;c;($in | encode base64)(ansi st)'
export def copy [
--ansi (-a) # Copy ansi formatting
]: any -> nothing {
let input = $in | collect
let text = match ($input | describe -d | get type) {
$type if $type in [ table, record, list ] => {
$input | table -e
}
_ => {$input}
}

let do_strip_ansi = match $ansi {
true => {{||}}
false => {{|| ansi strip }}
}

let output = (
$text
| do $do_strip_ansi
| encode base64
)

print -n $'(ansi osc)52;c;($output)(ansi st)'
}

# Paste contenst of system clipboard
Expand All @@ -33,3 +54,18 @@ export def paste []: [nothing -> string] {
| decode base64
| decode
}

# Add a prefix to each line of the content to be copied
#
# # Example: Format output for Nushell doc
# 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'
}