Advertisement
Guest User

Untitled

a guest
Jan 11th, 2019
811
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 7.90 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. stdin_exists() {
  4.     ! [ -t 0 ]
  5. }
  6.  
  7. last_arg="${@: -1}"
  8.  
  9. if ! stdin_exists; then
  10.     if test -f "$last_arg"; then
  11.         set -- "${@:1:$(($#-1))}" # shift last
  12.         exec < "$last_arg"
  13.     else
  14.         exit 0
  15.     fi
  16. fi
  17.  
  18. # free bindings
  19. # alt-q # taken
  20. # alt-x
  21. # alt-y
  22. # ctrl-alt-a
  23. # ctrl-alt-b
  24. # ctrl-alt-c # taken
  25. # ctrl-alt-d
  26. # ctrl-alt-e
  27. # ctrl-alt-f # taken
  28. # ctrl-alt-g
  29. # ctrl-alt-h
  30. # ctrl-alt-i
  31. # ctrl-alt-j
  32. # ctrl-alt-k
  33. # ctrl-alt-l
  34. # ctrl-alt-m
  35. # ctrl-alt-n
  36. # ctrl-alt-o
  37. # ctrl-alt-p
  38. # ctrl-alt-q
  39. # ctrl-alt-r
  40. # ctrl-alt-s
  41. # ctrl-alt-t
  42. # ctrl-alt-u
  43. # ctrl-alt-v
  44. # ctrl-alt-w
  45. # ctrl-alt-x # taken
  46. # ctrl-alt-y
  47. # ctrl-alt-z
  48.  
  49. # This is so we have LINES and COLUMNS to play with
  50. eval `resize`
  51.  
  52. tf_input="$(nix tf fzf txt || echo /dev/null)"
  53. trap "rm \"$tf_input\" 2>/dev/null" 0
  54. exec < <(tee "$tf_input")
  55.  
  56. # {q}  --  current query string
  57. #  {}  --  current file
  58. # {+}  --  selected files
  59. #          missing a way to select ALL the input. instead, capture the
  60. #          input into a variable. actually, capture to a file so fzf
  61. #          streams the input.
  62.  
  63. #preview="lit {} | umn | xargs head -n $LINES"
  64. #preview="lit {} | fzf-scope -n $LINES"
  65.  
  66. preview="fzf-scope"
  67.  
  68. preview_hidden=y
  69. do_sort=y
  70.  
  71. # preview_opts="right:30%"
  72. preview_opts="up:30%"
  73.  
  74. minimise=n
  75.  
  76. ANSI=n
  77. MULTI=y
  78. COMPLETE_WITH_PREVIEW=n
  79.  
  80. do_top=y
  81. do_reverse=n
  82. while [ $# -gt 0 ]; do opt="$1"; case "$opt" in
  83.     -m) {
  84.         minimise=y
  85.         shift
  86.     }
  87.     ;;
  88.  
  89.     -R) {
  90.         # rosie match all things
  91.         :
  92.     }
  93.     ;;
  94.    
  95.  
  96.     -nm) {
  97.         MULTI=n
  98.         shift
  99.     }
  100.     ;;
  101.  
  102.     -A) {
  103.         ANSI=y
  104.         shift
  105.     }
  106.     ;;
  107.  
  108.     -p) {
  109.         preview_hidden=n
  110.         shift
  111.     }
  112.     ;;
  113.  
  114.     -pcomplete) {
  115.         COMPLETE_WITH_PREVIEW=y
  116.         shift
  117.     }
  118.     ;;
  119.  
  120.     -rev|--reverse) {
  121.         do_reverse=y
  122.         shift
  123.     }
  124.     ;;
  125.  
  126.     -nr|--noreverse) {
  127.         do_reverse=n
  128.         shift
  129.     }
  130.     ;;
  131.  
  132.     --top) {
  133.         do_bottom=n
  134.         shift
  135.     }
  136.     ;;
  137.  
  138.     --bottom) {
  139.         do_bottom=y
  140.         shift
  141.     }
  142.     ;;
  143.  
  144.     -s) {
  145.         do_sort=y
  146.         shift
  147.     }
  148.     ;;
  149.  
  150.     +s) {
  151.         do_sort=n
  152.         shift
  153.     }
  154.     ;;
  155.  
  156.     -p|-pcmd) {
  157.         preview="fzf-scope -n $lines"
  158.         shift
  159.     }
  160.     ;;
  161.  
  162.     -pscript) {
  163.         preview="$2"
  164.         shift
  165.         shift
  166.     }
  167.     ;;
  168.  
  169.     -r) {
  170.         preview_opts="right:50%"
  171.         shift
  172.     }
  173.     ;;
  174.  
  175.     -rr) {
  176.         preview_opts="right:30%"
  177.         shift
  178.     }
  179.     ;;
  180.  
  181.     -t) {
  182.         preview_opts="up:30%"
  183.         shift
  184.     }
  185.     ;;
  186.  
  187.     -b) {
  188.         preview_opts="down:1"
  189.         shift
  190.     }
  191.     ;;
  192.  
  193.     *) break;
  194. esac; done
  195.  
  196. filter_in() {
  197.     if test "$minimise" = "y"; then
  198.         mnm
  199.     else
  200.         cat
  201.     fi
  202. }
  203.  
  204. filter_out() {
  205.     if test "$minimise" = "y"; then
  206.         umn
  207.     else
  208.         cat
  209.     fi
  210. }
  211.  
  212. CMD="$(
  213. for (( i = 1; i < $#; i++ )); do
  214.    eval ARG=\${$i}
  215.    aq "$ARG"
  216.    printf ' '
  217. done
  218. eval ARG=\${$i}
  219. aq "$ARG"
  220. )"
  221.  
  222. opts=""
  223.  
  224. if test "$MULTI" = "y"; then
  225.     opts+=" -m "
  226. fi
  227.  
  228. if test "$ANSI" = "y"; then
  229.     opts+=" --ansi "
  230. fi
  231.  
  232. if ! test "$do_sort" = "y"; then
  233.     opts+=" +s "
  234. fi
  235.  
  236. if test "$do_reverse" = "y"; then
  237.     IFS= read -rd '' input < <(cat /dev/stdin)
  238.     exec < <(s chomp <<< "$input" | tac)
  239. fi
  240.  
  241. if ! test "$do_bottom" = "y"; then
  242.     opts+=" --reverse "
  243. fi
  244.  
  245. if [ -n "$preview" ]; then
  246.     if test "$preview_hidden" = "y"; then
  247.         preview_opts="$preview_opts:hidden"
  248.     fi
  249.  
  250.     opts+=" --preview=$(aqf "lit {} | $preview") --preview-window=$preview_opts "
  251.  
  252.     # opts+="--preview=\"lit {} | umn | xargs head\" --preview-window=up:30%"
  253.     # opts+="--preview=\"lit {} | umn | xargs file\" --preview-window=down:1"
  254. fi
  255.  
  256. bindings=""
  257. #bindings+="ctrl-a:select-all+accept,"
  258. bindings+="alt-a:select-all,"
  259. bindings+="f1:abort,"
  260. # bindings+="alt-f1:abort," # there is no alt-f1 annoyingly
  261. bindings+="alt-u:deselect-all,"
  262. bindings+="ctrl-k:kill-line,"
  263. bindings+="alt-k:jump,"
  264. bindings+="tab:toggle-preview,"
  265.  
  266. # But right (and C-M-Right) is used to move along the pattern at the
  267. # top, not just the results list
  268. # bindings+="right:toggle,"
  269. bindings+="ctrl-alt-f:toggle,"
  270. bindings+="alt-z:toggle,"
  271. bindings+="ctrl-alt-x:up+toggle,"
  272. bindings+="ctrl-alt-c:down+toggle,"
  273.  
  274. bindings+="up:up,"
  275. bindings+="down:down,"
  276. bindings+="ctrl-p:up+toggle,"
  277. bindings+="ctrl-n:down+toggle,"
  278. bindings+="change:top," # triggered whenever the query string is changed.
  279. # alt-0 doesn't work.
  280. # bindings+="alt-0:top,"
  281. bindings+="alt-t:top,"
  282. # No function to go to the last result
  283. # bindings+="alt-4:bottom,"
  284.  
  285. # tf_input
  286.  
  287. bindings+="alt-r:execute-silent(lit {+} | awk 1 | head -n 1 | u drn | tm -f -S -tout spv -xargs ranger),"
  288. bindings+="alt-o:execute-silent(lit {+} | awk 1 | head -n 1 | tm -f S -tout spv -xargs rifle),"
  289. bindings+="alt-l:execute-silent(lit {+} | tm -f -S -tout spv v),"
  290. bindings+="alt-e:execute-silent(lit {+} | tm -f -S -tout spv -xargs sp),"
  291. # I want ctrl-e to go to the end of the line
  292. # bindings+="ctrl-e:execute-silent(lit {+} | tm -f -S -tout sph -xargs sp),"
  293. bindings+="alt-v:execute-silent(lit {+} | tm -f -S -tout spv -xargs v),"
  294. bindings+="alt-x:execute-silent(lit {+} | tm -f -S -tout sph -xargs v),"
  295. # bindings+="ctrl-alt-v:execute-silent(lit {+} | tm -f -S -tout sph -xargs v),"
  296. bindings+="alt-q:execute-silent(lit {+} | tm -f -S -tout spv -xargs edit-with),"
  297. bindings+="alt-s:execute-silent(lit {+} | umn | u drn | xargs tm -d sph -c),"
  298. bindings+="alt-h:execute-silent(lit {+} | umn | u drn | xargs tm -d spv -c),"
  299. bindings+="alt-w:execute-silent(tm -f -d spv 'v '$tf_input),"
  300. # bindings+="alt-h:execute-silent(lit {+} | tm -f -S -tout sph -xargs v),"
  301. bindings+="alt-f:execute-silent(lit {+} | fzf-thing),"
  302. bindings+="alt-i:execute-silent(lit {+} | awk 1 | $preview -n -1 | tm -f -S -i spv -noerror 'fzf -nm'),"
  303.  
  304. # TODO scrape-file-fast.sh doesn't catch them all and scrape-files.sh is slow
  305. # bindings+="alt-y:execute-silent(lit '$preview' | scrape-files.sh | awk 1 | tm -f -S -i spv -noerror 'fzf -nm'),"
  306. bindings+="alt-p:execute-silent(lit {+} | fzf-thing)," # this should put the preview in a new window
  307. #bindings+="alt-1:execute-silent(lit {+} | fzf-thing)," # alt-1 does not work
  308. bindings+="alt-n:execute-silent(lit {+} | u fn | xc),"
  309. bindings+="alt-d:execute-silent(lit {+} | awk 1 | xargs -l1 dirname | xc -i -n),"
  310. bindings+="alt-g:execute-silent(lit {+} | awk 1 | xargs -l1 realpath | xc -i -n),"
  311. bindings+="alt-c:execute-silent(lit {+} | umn | s chomp | sed 's/#[^#]\+$//' | sed 's/\s\+$//' | xc -i -n),"
  312. bindings+="alt-m:execute-silent(lit {+} | mnm | s chomp | xc -i -n),"
  313. bindings+="alt-j:execute-silent(lit {+} | awk 1 | $preview | head -n 1 | s chomp | xc -i)," # copy first result of preview
  314. bindings="$(p "$bindings" | sed 's/.$//' | qne)"
  315.  
  316. opts+=" --bind=$(apq "$bindings") "
  317.  
  318. cmd="$HOME/source/git/fzf/bin/fzf --algo=v2 $opts $CMD"
  319.  
  320. # pl "$cmd" | tv &>/dev/null
  321.  
  322. # This does not allow us to catch fzf's error code
  323. # filter_in | eval "$cmd" | filter_out
  324.  
  325. my_out="$(filter_in | eval "$cmd" | { if test $ANSI = y; then strip-ansi; else cat; fi; })"
  326. ret="$?"
  327. printf -- "%s" "$my_out" | filter_out
  328. exit "$ret"
  329.  
  330. # exit 130 means cancelled
  331.  
  332. #| {
  333. #    if test "$COMPLETE_WITH_PREVIEW" = "y"; then
  334. #        # filter through the preview command
  335. #        # Or instead of doing this, I can make a new binding
  336. #        # Both these options are difficult to make work. Instead, don't
  337. #        use the -pcomplete option
  338. #        :
  339. #    else
  340. #        cat
  341. #    fi
  342. #}
  343.  
  344. # fzf --multi --preview='head -10 {+}'
  345. # git log --oneline | fzf --multi --preview 'git show {+1}'
  346. # e.g. fzf --preview='head -$LINES {}'
  347. # ls -l | fzf --preview="echo user={3} when={-4..-2}; cat {-1}" --header-lines=1
  348.  
  349. # KEY BINDINGS
  350. # --bind
  351. # alt-c:
  352.  
  353. # Create some bindings
  354. # This must not create an error
  355. # find-all-no-git.sh | fzf -p --multi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement