Skip to content

Commit bab7845

Browse files
authored
Additional clip copy options (#1028)
Addition to `clip` module (#1009): * Passes complex values through `table -e` to render properly * By default, strips ansi codes, unless `--ansi (-a)` switch is used * Adds a `clip prefex` command which can accept a prefix string like `# => ` to be added to the beginning of each line.
1 parent 10b2262 commit bab7845

File tree

1 file changed

+38
-2
lines changed
  • stdlib-candidate/std-rfc/clip

1 file changed

+38
-2
lines changed

stdlib-candidate/std-rfc/clip/mod.nu

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,29 @@
99
# ```nushell
1010
# >_ "Hello" | clip copy
1111
# ```
12-
export def copy []: [string -> nothing] {
13-
print -n $'(ansi osc)52;c;($in | encode base64)(ansi st)'
12+
export def copy [
13+
--ansi (-a) # Copy ansi formatting
14+
]: any -> nothing {
15+
let input = $in | collect
16+
let text = match ($input | describe -d | get type) {
17+
$type if $type in [ table, record, list ] => {
18+
$input | table -e
19+
}
20+
_ => {$input}
21+
}
22+
23+
let do_strip_ansi = match $ansi {
24+
true => {{||}}
25+
false => {{|| ansi strip }}
26+
}
27+
28+
let output = (
29+
$text
30+
| do $do_strip_ansi
31+
| encode base64
32+
)
33+
34+
print -n $'(ansi osc)52;c;($output)(ansi st)'
1435
}
1536

1637
# Paste contenst of system clipboard
@@ -33,3 +54,18 @@ export def paste []: [nothing -> string] {
3354
| decode base64
3455
| decode
3556
}
57+
58+
# Add a prefix to each line of the content to be copied
59+
#
60+
# # Example: Format output for Nushell doc
61+
# ls | clip prefix '# => ' | clip copy
62+
export def prefix [prefix: string]: any -> string {
63+
let input = $in | collect
64+
match ($input | describe -d | get type) {
65+
$type if $type in [ table, record, list ] => {
66+
$input | table -e
67+
}
68+
_ => {$input}
69+
}
70+
| str replace -r --all '(?m)(.*)' $'($prefix)$1'
71+
}

0 commit comments

Comments
 (0)