test-clipmenu (2054B)
1 #!/usr/bin/env bash 2 3 set -x 4 set -e 5 set -o pipefail 6 7 : "${CM_DIR="${XDG_RUNTIME_DIR-"${TMPDIR-/tmp}"}"}" 8 9 major_version=6 10 dir=$CM_DIR/clipmenu.$major_version.$USER 11 cache_file=$dir/line_cache 12 13 if [[ $0 == /* ]]; then 14 location=${0%/*} 15 else 16 location=$PWD/${0#./} 17 location=${location%/*} 18 fi 19 20 cat - "$location/../clipmenu" > /tmp/clipmenu << 'EOF' 21 #!/usr/bin/env bash 22 23 shopt -s expand_aliases 24 25 shim() { 26 printf '%s args:' "$1" >&2 27 printf ' %q' "${@:2}" >&2 28 printf '\n' >&2 29 30 i=0 31 32 while IFS= read -r line; do 33 let i++ 34 printf '%s line %d stdin: %s\n' "$1" "$i" "$line" >&2 35 done 36 37 if [[ -v SHIM_STDOUT ]]; then 38 printf '%s\n' "$SHIM_STDOUT" 39 fi 40 } 41 42 # Cannot be an alias due to expansion order with $CM_LAUNCHER 43 dmenu() { 44 SHIM_STDOUT="Selected text. (2 lines)" shim dmenu "$@" 45 } 46 47 rofi() { 48 SHIM_STDOUT="Selected text. (2 lines)" shim rofi "$@" 49 } 50 51 alias xsel='shim xsel' 52 alias xclip='shim xclip' 53 EOF 54 55 chmod a+x /tmp/clipmenu 56 57 rm -rf "$dir" 58 mkdir -p "$dir" 59 60 cat > "$cache_file" << 'EOF' 61 1234 Selected text. (2 lines) 62 1235 Selected text 2. (2 lines) 63 EOF 64 65 cat > "$dir/$(cksum <<< 'Selected text. (2 lines)')" << 'EOF' 66 Selected text. 67 Yes, it's selected text. 68 EOF 69 70 ### TESTS ### 71 72 temp=$(mktemp) 73 74 trap 'cat "$temp"' EXIT 75 76 /tmp/clipmenu --foo bar > "$temp" 2>&1 77 78 # Arguments are transparently passed to dmenu 79 grep -Fxq 'dmenu args: -l 8 --foo bar' "$temp" 80 81 # Output from cache file should get to dmenu, reversed 82 grep -Fxq 'dmenu line 1 stdin: Selected text 2. (2 lines)' "$temp" 83 grep -Fxq 'dmenu line 2 stdin: Selected text. (2 lines)' "$temp" 84 85 # xsel should copy both to clipboard *and* primary 86 grep -Fxq 'xsel args: --logfile /dev/null -i --clipboard' "$temp" 87 grep -Fxq 'xsel args: --logfile /dev/null -i --primary' "$temp" 88 89 grep -Fxq 'xsel line 1 stdin: Selected text.' "$temp" 90 grep -Fxq "xsel line 2 stdin: Yes, it's selected text." "$temp" 91 92 CM_LAUNCHER=rofi /tmp/clipmenu --foo bar > "$temp" 2>&1 93 94 # We have a special case to add -dmenu for rofi 95 grep -Fxq 'rofi args: -l 8 -dmenu --foo bar' "$temp"