refactor(clipboard): share provider detection across y/p/paste/hist
Extracts _fish_clipboard_copy and _fish_clipboard_paste so the wl-copy/xclip/win32yank fallback chain lives in one place instead of four near-duplicates. hist now goes through the same chain, so it also gets the xclip (X11) fallback it was missing before, alongside win32yank on WSL2.
This commit is contained in:
@@ -0,0 +1,28 @@
|
||||
# Copyright (C) 2026 Rootiest
|
||||
# SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
|
||||
# SYNOPSIS
|
||||
# _fish_clipboard_copy
|
||||
#
|
||||
# DESCRIPTION
|
||||
# Copies stdin to the system clipboard. Tries wl-copy (Wayland), then
|
||||
# xclip (X11), then win32yank.exe (WSL2).
|
||||
#
|
||||
# EXIT STATUS
|
||||
# 0 Text copied to clipboard
|
||||
# 1 No clipboard provider found
|
||||
#
|
||||
# EXAMPLE
|
||||
# echo "hello" | _fish_clipboard_copy
|
||||
function _fish_clipboard_copy --description 'Copy stdin to the system clipboard'
|
||||
if type -q wl-copy
|
||||
wl-copy
|
||||
else if type -q xclip
|
||||
xclip -selection clipboard
|
||||
else if type -q win32yank.exe
|
||||
win32yank.exe -i --crlf
|
||||
else
|
||||
echo "Error: No clipboard provider (wl-copy, xclip, or win32yank) found." >&2
|
||||
return 1
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,31 @@
|
||||
# Copyright (C) 2026 Rootiest
|
||||
# SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
|
||||
# SYNOPSIS
|
||||
# _fish_clipboard_paste [args...]
|
||||
#
|
||||
# DESCRIPTION
|
||||
# Prints the system clipboard contents to stdout. Tries wl-paste
|
||||
# (Wayland), then xclip (X11), then win32yank.exe (WSL2).
|
||||
#
|
||||
# ARGUMENTS
|
||||
# args... Arguments forwarded to the clipboard tool
|
||||
#
|
||||
# EXIT STATUS
|
||||
# 0 Clipboard contents read successfully
|
||||
# 1 No clipboard provider found
|
||||
#
|
||||
# EXAMPLE
|
||||
# _fish_clipboard_paste
|
||||
function _fish_clipboard_paste --description 'Print the system clipboard contents'
|
||||
if type -q wl-paste
|
||||
wl-paste $argv
|
||||
else if type -q xclip
|
||||
xclip -selection clipboard -o $argv
|
||||
else if type -q win32yank.exe
|
||||
win32yank.exe -o --lf $argv
|
||||
else
|
||||
echo "Error: No clipboard provider (wl-paste, xclip, or win32yank) found." >&2
|
||||
return 1
|
||||
end
|
||||
end
|
||||
+6
-7
@@ -12,8 +12,11 @@
|
||||
#
|
||||
# DESCRIPTION
|
||||
# Searches fish history interactively using fzf, inserts the selected command
|
||||
# into the command line, and copies it to the clipboard via wl-copy (falls
|
||||
# back to win32yank on WSL2).
|
||||
# into the command line, and copies it to the clipboard via wl-copy, xclip,
|
||||
# or win32yank (WSL2).
|
||||
#
|
||||
# DEPENDENCIES
|
||||
# _fish_clipboard_copy
|
||||
#
|
||||
# EXIT STATUS
|
||||
# 0 Command selected and inserted, or fzf was cancelled
|
||||
@@ -37,11 +40,7 @@ function hist --description 'Search fish history and put it in the prompt'
|
||||
# Strip the timestamp for the final output
|
||||
set -l command (echo $selected | string replace -r '^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2} ' '')
|
||||
|
||||
if type -q wl-copy
|
||||
echo $command | wl-copy 2>/dev/null
|
||||
else if type -q win32yank.exe
|
||||
echo $command | win32yank.exe -i --crlf 2>/dev/null
|
||||
end
|
||||
echo $command | _fish_clipboard_copy 2>/dev/null
|
||||
commandline -r $command
|
||||
end
|
||||
end
|
||||
|
||||
+4
-15
@@ -11,6 +11,9 @@
|
||||
# Outputs clipboard contents to stdout. Uses wl-paste on Wayland, xclip on
|
||||
# X11, or win32yank on WSL2. Supports -h/--help for usage info.
|
||||
#
|
||||
# DEPENDENCIES
|
||||
# _fish_clipboard_paste
|
||||
#
|
||||
# ARGUMENTS
|
||||
# -h, --help Show usage help
|
||||
# args... Arguments forwarded to the clipboard tool
|
||||
@@ -41,19 +44,5 @@ function p --description 'Put from clipboard'
|
||||
return 0
|
||||
end
|
||||
|
||||
# Determine the clipboard provider
|
||||
set -l paste_cmd
|
||||
if type -q wl-paste
|
||||
set paste_cmd wl-paste
|
||||
else if type -q xclip
|
||||
set paste_cmd xclip -selection clipboard -o
|
||||
else if type -q win32yank.exe
|
||||
set paste_cmd win32yank.exe -o --lf
|
||||
else
|
||||
echo "Error: No clipboard provider (wl-paste, xclip, or win32yank) found." >&2
|
||||
return 1
|
||||
end
|
||||
|
||||
# Execute the paste command with any provided arguments
|
||||
$paste_cmd $argv
|
||||
_fish_clipboard_paste $argv
|
||||
end
|
||||
|
||||
+4
-10
@@ -11,6 +11,9 @@
|
||||
# Outputs clipboard contents to stdout. Uses wl-paste on Wayland, xclip on
|
||||
# X11, or win32yank on WSL2.
|
||||
#
|
||||
# DEPENDENCIES
|
||||
# _fish_clipboard_paste
|
||||
#
|
||||
# ARGUMENTS
|
||||
# args... Arguments forwarded to the clipboard tool
|
||||
#
|
||||
@@ -24,14 +27,5 @@
|
||||
# EXAMPLE
|
||||
# paste > file.txt
|
||||
function paste --description 'Paste from clipboard'
|
||||
if type -q wl-paste
|
||||
wl-paste $argv
|
||||
else if type -q xclip
|
||||
xclip -selection clipboard -o $argv
|
||||
else if type -q win32yank.exe
|
||||
win32yank.exe -o --lf $argv
|
||||
else
|
||||
echo "Error: No clipboard provider (wl-paste, xclip, or win32yank) found." >&2
|
||||
return 1
|
||||
end
|
||||
_fish_clipboard_paste $argv
|
||||
end
|
||||
|
||||
+5
-15
@@ -11,6 +11,9 @@
|
||||
# Copies text to the system clipboard using wl-copy (Wayland), xclip (X11),
|
||||
# or win32yank (WSL2). Reads from stdin when no arguments are given.
|
||||
#
|
||||
# DEPENDENCIES
|
||||
# _fish_clipboard_copy
|
||||
#
|
||||
# ARGUMENTS
|
||||
# text Text to copy; reads from stdin if omitted
|
||||
#
|
||||
@@ -36,26 +39,13 @@ function y --description 'Yank to clipboard'
|
||||
return 0
|
||||
end
|
||||
|
||||
# Determine the clipboard provider
|
||||
set -l copy_cmd
|
||||
if type -q wl-copy
|
||||
set copy_cmd wl-copy
|
||||
else if type -q xclip
|
||||
set copy_cmd xclip -selection clipboard
|
||||
else if type -q win32yank.exe
|
||||
set copy_cmd win32yank.exe -i --crlf
|
||||
else
|
||||
echo "Error: No clipboard provider (wl-copy, xclip, or win32yank) found." >&2
|
||||
return 1
|
||||
end
|
||||
|
||||
# Handle input
|
||||
if set -q argv[1]
|
||||
# If arguments are provided, echo them to the clipboard
|
||||
echo $argv | eval $copy_cmd
|
||||
echo $argv | _fish_clipboard_copy
|
||||
else
|
||||
# If no arguments, read from stdin (pipes/redirects)
|
||||
eval $copy_cmd
|
||||
_fish_clipboard_copy
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
Reference in New Issue
Block a user