mirror of
https://github.com/fish-shell/fish-shell.git
synced 2024-11-29 05:03:46 +08:00
29778ee845
This makes these tools usable in a pipe. You can run ```fish some-long-command | fish_clipboard_copy ``` to copy some command's output to your clipboard, and ```fish fish_clipboard_paste | some-other-command ``` To feed your clipboard to some command.
67 lines
2.5 KiB
Fish
67 lines
2.5 KiB
Fish
function fish_clipboard_paste
|
|
set -l data
|
|
if type -q pbpaste
|
|
set data (pbpaste 2>/dev/null | string collect -N)
|
|
else if set -q WAYLAND_DISPLAY; and type -q wl-paste
|
|
set data (wl-paste -n 2>/dev/null | string collect -N)
|
|
else if set -q DISPLAY; and type -q xsel
|
|
set data (xsel --clipboard | string collect -N)
|
|
else if set -q DISPLAY; and type -q xclip
|
|
set data (xclip -selection clipboard -o 2>/dev/null | string collect -N)
|
|
else if type -q powershell.exe
|
|
set data (powershell.exe Get-Clipboard | string trim -r -c \r | string collect -N)
|
|
end
|
|
|
|
# Issue 6254: Handle zero-length clipboard content
|
|
if not string length -q -- "$data"
|
|
return 1
|
|
end
|
|
|
|
if not isatty stdout
|
|
# If we're redirected, just write the data *as-is*.
|
|
printf %s $data
|
|
return
|
|
end
|
|
|
|
# Also split on \r, otherwise it looks confusing
|
|
set data (string split \r -- $data | string split \n)
|
|
|
|
# If the current token has an unmatched single-quote,
|
|
# escape all single-quotes (and backslashes) in the paste,
|
|
# in order to turn it into a single literal token.
|
|
#
|
|
# This eases pasting non-code (e.g. markdown or git commitishes).
|
|
set -l quote_state (__fish_tokenizer_state -- (commandline -ct | string collect))
|
|
if contains -- $quote_state single single-escaped
|
|
if status test-feature regex-easyesc
|
|
set data (string replace -ra "(['\\\])" '\\\\$1' -- $data)
|
|
else
|
|
set data (string replace -ra "(['\\\])" '\\\\\\\$1' -- $data)
|
|
end
|
|
else if not contains -- $quote_state double double-escaped
|
|
and set -q data[2]
|
|
# Leading whitespace in subsequent lines is unneded, since fish
|
|
# already indents. Also gets rid of tabs (issue #5274).
|
|
set -l tmp
|
|
for line in $data
|
|
switch $quote_state
|
|
case normal
|
|
set -a tmp (string trim -l -- $line)
|
|
case single single-escaped double double-escaped escaped
|
|
set -a tmp $line
|
|
end
|
|
set quote_state (__fish_tokenizer_state -i $quote_state -- $line)
|
|
end
|
|
set data $data[1] $tmp[2..]
|
|
end
|
|
if not string length -q -- (commandline -c)
|
|
# If we're at the beginning of the first line, trim whitespace from the start,
|
|
# so we don't trigger ignoring history.
|
|
set data[1] (string trim -l -- $data[1])
|
|
end
|
|
|
|
if test -n "$data"
|
|
commandline -i -- $data
|
|
end
|
|
end
|