nnn

mirror of https://github.com/jarun/nnn
Log | Files | Refs | README | LICENSE

x2sel (1547B)


      1 #!/usr/bin/env sh
      2 
      3 # Description: Copy system clipboard newline-separated file list to selection
      4 #
      5 # Dependencies:
      6 #   - tr
      7 #   - xclip/xsel (Linux)
      8 #   - pbpaste (macOS)
      9 #   - termux-clipboard-get (Termux)
     10 #   - powershell (WSL)
     11 #   - cygwin's /dev/clipboard (Cygwin)
     12 #   - wl-paste (Wayland)
     13 #   - clipboard (Haiku)
     14 #
     15 # Note:
     16 #   - Limitation: breaks if a filename has newline in it
     17 #
     18 # Shell: POSIX compliant
     19 # Author: Léo Villeveygoux, after Arun Prakash Jana's .cbcp
     20 
     21 IFS="$(printf '%b_' '\n')"; IFS="${IFS%_}" # protect trailing \n
     22 
     23 selection=${NNN_SEL:-${XDG_CONFIG_HOME:-$HOME/.config}/nnn/.selection}
     24 
     25 getclip () {
     26 	if [ "$XDG_SESSION_TYPE" = "wayland" ]; then
     27 		# Wayland
     28 		wl-paste
     29 	elif type powershell.exe >/dev/null 2>&1; then
     30 		# WSL
     31 		powershell.exe -noprofile -command Get-Clipboard | tr -d '\r'
     32 	elif type xsel >/dev/null 2>&1; then
     33 		# Linux
     34 		xsel -bo
     35 	elif type xclip >/dev/null 2>&1; then
     36 		# Linux
     37 		xclip -sel clip -o
     38 	elif type pbpaste >/dev/null 2>&1; then
     39 		# macOS
     40 		pbpaste
     41 	elif type termux-clipboard-get >/dev/null 2>&1; then
     42 		# Termux
     43 		termux-clipboard-get
     44 	elif [ -r /dev/clipboard ] ; then
     45 		# Cygwin
     46 		cat /dev/clipboard
     47 	elif type clipboard >/dev/null 2>&1; then
     48 		# Haiku
     49 		clipboard --print
     50 	fi
     51 }
     52 
     53 CLIPBOARD=$(getclip)
     54 
     55 if [ -z "$CLIPBOARD" ]; then
     56 	echo "No content in clipboard"
     57 	exit 1
     58 fi
     59 
     60 # Check if clipboard actually contains a file list
     61 for file in $CLIPBOARD ; do
     62 	if [ ! -e "$file" ] ; then
     63 		echo "'$file' is not a file"
     64 		exit 1;
     65 	fi
     66 done
     67 
     68 printf "%s" "$CLIPBOARD" | tr '\n' '\0' > "$selection"