2019-12-12 14:21:08 +00:00
|
|
|
#!/usr/bin/env sh
|
|
|
|
|
|
|
|
# Description: Copy selection to system clipboard as newline-separated entries
|
2020-05-06 05:29:57 +00:00
|
|
|
# Dependencies:
|
|
|
|
# - tr
|
|
|
|
# - xclip/xsel (Linux)
|
|
|
|
# - pbcopy (macOS)
|
|
|
|
# - termux-clipboard-set (Termux)
|
|
|
|
# - clip.exe (WSL)
|
|
|
|
# - clip (Cygwin)
|
|
|
|
# - wl-copy (Wayland)
|
2020-08-09 21:44:11 +00:00
|
|
|
# - clipboard (Haiku)
|
2019-12-12 14:21:08 +00:00
|
|
|
#
|
2020-05-06 05:29:57 +00:00
|
|
|
# Limitation: breaks if a filename has newline in it
|
2019-12-12 14:21:08 +00:00
|
|
|
#
|
|
|
|
# Note: For a space-separated list:
|
|
|
|
# xargs -0 < "$SELECTION"
|
|
|
|
#
|
|
|
|
# Shell: POSIX compliant
|
|
|
|
# Author: Arun Prakash Jana
|
|
|
|
|
|
|
|
IFS="$(printf '%b_' '\n')"; IFS="${IFS%_}" # protect trailing \n
|
|
|
|
|
2020-04-24 04:42:24 +00:00
|
|
|
selection=${NNN_SEL:-${XDG_CONFIG_HOME:-$HOME/.config}/nnn/.selection}
|
2022-04-29 22:50:47 +00:00
|
|
|
[ -s "$selection" ] || { echo "plugin .cbcp error: empty selection" >&2 ; exit 1; }
|
2019-12-12 14:21:08 +00:00
|
|
|
|
2022-11-26 03:40:59 +00:00
|
|
|
if [ "$XDG_SESSION_TYPE" = "wayland" ]; then
|
|
|
|
# Wayland
|
|
|
|
tr '\0' '\n' < "$selection" | wl-copy
|
|
|
|
elif type xsel >/dev/null 2>&1; then
|
2019-12-12 14:21:08 +00:00
|
|
|
# Linux
|
2020-04-24 04:42:24 +00:00
|
|
|
tr '\0' '\n' < "$selection" | xsel -bi
|
2021-05-14 12:03:28 +00:00
|
|
|
elif type xclip >/dev/null 2>&1; then
|
2019-12-12 14:21:08 +00:00
|
|
|
# Linux
|
2020-04-24 04:42:24 +00:00
|
|
|
tr '\0' '\n' < "$selection" | xclip -sel clip
|
2021-05-14 12:03:28 +00:00
|
|
|
elif type pbcopy >/dev/null 2>&1; then
|
2019-12-12 14:21:08 +00:00
|
|
|
# macOS
|
2020-04-24 04:42:24 +00:00
|
|
|
tr '\0' '\n' < "$selection" | pbcopy
|
2021-05-14 12:03:28 +00:00
|
|
|
elif type termux-clipboard-set >/dev/null 2>&1; then
|
2019-12-12 14:21:08 +00:00
|
|
|
# Termux
|
2020-04-24 04:42:24 +00:00
|
|
|
tr '\0' '\n' < "$selection" | termux-clipboard-set
|
2021-05-14 12:03:28 +00:00
|
|
|
elif type clip.exe >/dev/null 2>&1; then
|
2019-12-12 14:21:08 +00:00
|
|
|
# WSL
|
2020-04-24 04:42:24 +00:00
|
|
|
tr '\0' '\n' < "$selection" | clip.exe
|
2021-05-14 12:03:28 +00:00
|
|
|
elif type clip >/dev/null 2>&1; then
|
2019-12-12 14:21:08 +00:00
|
|
|
# Cygwin
|
2020-04-24 04:42:24 +00:00
|
|
|
tr '\0' '\n' < "$selection" | clip
|
2021-05-14 12:03:28 +00:00
|
|
|
elif type clipboard >/dev/null 2>&1; then
|
2020-08-09 21:44:11 +00:00
|
|
|
# Haiku
|
|
|
|
tr '\0' '\n' < "$selection" | clipboard --stdin
|
2019-12-12 14:21:08 +00:00
|
|
|
fi
|