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:
2026-09-21 02:52:17 -04:00
parent 07613c7889
commit b2be858d8c
7 changed files with 79 additions and 48 deletions
@@ -13,7 +13,7 @@ These features couple the shell to specific external tools. Disabling
spwin Kitty or WezTerm spwin Kitty or WezTerm
tab Kitty, WezTerm, or Konsole tab Kitty, WezTerm, or Konsole
split Kitty or WezTerm split Kitty or WezTerm
hist fzf + wl-copy or win32yank.exe (WSL2) hist fzf + wl-copy, xclip, or win32yank.exe (WSL2)
logs fzf + ov; reads from ~/.terminal_history/ logs fzf + ov; reads from ~/.terminal_history/
upgrade paru or yay (Arch Linux only) upgrade paru or yay (Arch Linux only)
WakaTime hook wakatime CLI and a configured API key WakaTime hook wakatime CLI and a configured API key
+28
View File
@@ -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
+31
View File
@@ -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
View File
@@ -12,8 +12,11 @@
# #
# DESCRIPTION # DESCRIPTION
# Searches fish history interactively using fzf, inserts the selected command # Searches fish history interactively using fzf, inserts the selected command
# into the command line, and copies it to the clipboard via wl-copy (falls # into the command line, and copies it to the clipboard via wl-copy, xclip,
# back to win32yank on WSL2). # or win32yank (WSL2).
#
# DEPENDENCIES
# _fish_clipboard_copy
# #
# EXIT STATUS # EXIT STATUS
# 0 Command selected and inserted, or fzf was cancelled # 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 # 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} ' '') 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 | _fish_clipboard_copy 2>/dev/null
echo $command | wl-copy 2>/dev/null
else if type -q win32yank.exe
echo $command | win32yank.exe -i --crlf 2>/dev/null
end
commandline -r $command commandline -r $command
end end
end end
+4 -15
View File
@@ -11,6 +11,9 @@
# Outputs clipboard contents to stdout. Uses wl-paste on Wayland, xclip on # Outputs clipboard contents to stdout. Uses wl-paste on Wayland, xclip on
# X11, or win32yank on WSL2. Supports -h/--help for usage info. # X11, or win32yank on WSL2. Supports -h/--help for usage info.
# #
# DEPENDENCIES
# _fish_clipboard_paste
#
# ARGUMENTS # ARGUMENTS
# -h, --help Show usage help # -h, --help Show usage help
# args... Arguments forwarded to the clipboard tool # args... Arguments forwarded to the clipboard tool
@@ -41,19 +44,5 @@ function p --description 'Put from clipboard'
return 0 return 0
end end
# Determine the clipboard provider _fish_clipboard_paste $argv
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
end end
+4 -10
View File
@@ -11,6 +11,9 @@
# Outputs clipboard contents to stdout. Uses wl-paste on Wayland, xclip on # Outputs clipboard contents to stdout. Uses wl-paste on Wayland, xclip on
# X11, or win32yank on WSL2. # X11, or win32yank on WSL2.
# #
# DEPENDENCIES
# _fish_clipboard_paste
#
# ARGUMENTS # ARGUMENTS
# args... Arguments forwarded to the clipboard tool # args... Arguments forwarded to the clipboard tool
# #
@@ -24,14 +27,5 @@
# EXAMPLE # EXAMPLE
# paste > file.txt # paste > file.txt
function paste --description 'Paste from clipboard' function paste --description 'Paste from clipboard'
if type -q wl-paste _fish_clipboard_paste $argv
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 end
+5 -15
View File
@@ -11,6 +11,9 @@
# Copies text to the system clipboard using wl-copy (Wayland), xclip (X11), # Copies text to the system clipboard using wl-copy (Wayland), xclip (X11),
# or win32yank (WSL2). Reads from stdin when no arguments are given. # or win32yank (WSL2). Reads from stdin when no arguments are given.
# #
# DEPENDENCIES
# _fish_clipboard_copy
#
# ARGUMENTS # ARGUMENTS
# text Text to copy; reads from stdin if omitted # text Text to copy; reads from stdin if omitted
# #
@@ -36,26 +39,13 @@ function y --description 'Yank to clipboard'
return 0 return 0
end 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 # Handle input
if set -q argv[1] if set -q argv[1]
# If arguments are provided, echo them to the clipboard # If arguments are provided, echo them to the clipboard
echo $argv | eval $copy_cmd echo $argv | _fish_clipboard_copy
else else
# If no arguments, read from stdin (pipes/redirects) # If no arguments, read from stdin (pipes/redirects)
eval $copy_cmd _fish_clipboard_copy
end end
end end