test/fakefzf (1912 bytes)
1 #!/bin/sh 2 3 # Scripted stand-in for fzf, installed as "fzf" on the test PATH. Consumes one 4 # line of $SBM_TEST_ANSWERS per call, like test/fakemenu. What it prints 5 # depends on the options it was given, as with the real thing: 6 # 7 # --expect answer key:text the typed text, the key ("enter" 8 # is the empty key), then the first 9 # row containing text, if any 10 # --multi answer text:words the typed text, then the choices 11 # that start with each word 12 # --print-query alone answer text the typed text, then the first row 13 # containing it, if any 14 # none of these answer text the first row containing text 15 # 16 # An empty answer is ESC: no output, exit 130. 17 18 [ -z "$SBM_TEST_LOG" ] || printf 'fzf %s\n' "$*" >> "$SBM_TEST_LOG" 19 20 mode=pick 21 for arg in "$@"; do 22 case $arg in 23 --print-query) [ "$mode" = pick ] && mode=query ;; 24 --multi) [ "$mode" = expect ] || mode=multi ;; 25 --expect=*) mode=expect ;; 26 esac 27 done 28 29 choices=$(cat) 30 answer=$(sed -n 1p "$SBM_TEST_ANSWERS") 31 sed 1d "$SBM_TEST_ANSWERS" > "$SBM_TEST_ANSWERS.next" 32 mv "$SBM_TEST_ANSWERS.next" "$SBM_TEST_ANSWERS" 33 34 [ -n "$answer" ] || exit 130 35 36 row () { 37 [ -n "$1" ] || return 0 38 printf '%s\n' "$choices" | grep -F -- "$1" | head -n 1 39 } 40 41 case $mode in 42 expect) 43 key=${answer%%:*} 44 [ "$key" = enter ] && key= 45 printf '%s\n%s\n' "${answer#*:}" "$key" 46 row "${answer#*:}" 47 ;; 48 multi) 49 printf '%s\n' "${answer%%:*}" 50 for word in ${answer#*:}; do 51 printf '%s\n' "$choices" | grep -- "^$word " | head -n 1 52 done 53 ;; 54 query) 55 printf '%s\n' "$answer" 56 row "$answer" 57 ;; 58 *) 59 row "$answer" 60 ;; 61 esac