#!/bin/zsh setopt no_unset warn_create_global typeset -gA clipboards typeset -g map=/run/display-container-map #set -x get-clip() { local sel out name=$1 local -a env=( env ) [[ -f /run/Xauthority.${DISPLAY#:} ]] && \ env+=( XAUTHORITY=/run/Xauthority.${DISPLAY#:} ) for sel in CLIPBOARD PRIMARY; do out=$( $env xclip -o -sel $sel ) && \ [[ -n $out ]] && clipboards[${sel:l}@$name]=$out done } scan() { local sock local -a found for sock in /tmp/.X11-unix/X*; do found=( $map/${${sock:t}#X}=*(N) ) DISPLAY=:${sock##*/X} get-clip ${${found[1]:-${sock:t}}##*=} done } #typeset -f -t scan print-clip() { local name content for name content in "${(kv@)clipboards}"; do printf '>> %s <<\n%s\n\n' $name $content done } print-clip-0() { local name content for name content in "${(kv@)clipboards}"; do printf '%s\0' $name $content done } main() { scan if (($#)) && [[ $1 == -0 ]]; then print-clip-0 else print-clip fi } main "$@"