Skip to content

Commit

Permalink
Update copy.sh for better WSL support
Browse files Browse the repository at this point in the history
  • Loading branch information
lindhe committed Mar 21, 2024
1 parent 7895d3c commit 5e55fb4
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion terminal/copy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,29 @@ fail() {
exit "${2:-1}"
}

wsl_copy() {
# This function is superior to Windows' copy.exe, since it does not mess up
# text encoding by asssuming BOM.
# https://github.com/microsoft/WSL/issues/10095#issuecomment-1602833117
INPUT="$(cat; echo x)"
declare -r INPUT="${INPUT%x}" # https://stackoverflow.com/a/32365596/893211
# Note that the string terminator '@ must be on a separate line and must not
# have leading whitespace:
# https://devblogs.microsoft.com/scripting/powershell-for-programmers-here-strings-there-strings-everywhere-some-string-strings/
powershell.exe -c "Set-Clipboard @'
${INPUT}
'@
"
}

if [[ "$(uname -r)" =~ .*microsoft.* ]]; then
declare -r IS_WSL=true
else
declare -r IS_WSL=false
fi

if [[ "${IS_WSL}" == "true" ]]; then
declare -r CLIPBOARD_CMD='/mnt/c/Windows/System32/clip.exe'
declare -r CLIPBOARD_CMD=wsl_copy
else
missing_dependencies=false
declare -r dependencies=(
Expand Down

0 comments on commit 5e55fb4

Please sign in to comment.