Browse code

Implement support scripts for `tmux` and `dvtm`

This script adds two scripts that allows spawning a new window/pane in
`tmux` and `dvtm` in which `fzy` will be called.

Options data on the standard input are passed seamlessly, e.g.

$ find -type f | fzy-tmux
$ ag -g '' | fzy-dvtm -l $(tput lines)

Frank LENORMAND authored on 08/02/2017 06:34:18
Showing 1 changed files
1 1
new file mode 100755
... ...
@@ -0,0 +1,50 @@
1
+#!/bin/sh
2
+
3
+_echo() {
4
+    printf %s\\n "$*"
5
+}
6
+
7
+fatal() {
8
+    _echo "$*" >&2
9
+    exit 1
10
+}
11
+
12
+main() {
13
+    if [ -z "${DVTM_CMD_FIFO}" ]; then
14
+        fatal "No DVTM_CMD_FIFO variable detected in the environment"
15
+    fi
16
+
17
+    readonly PATH_DIR_TMP=$(mktemp -d)
18
+    readonly PATH_FIFO_IN="${PATH_DIR_TMP}/in"
19
+    readonly PATH_FIFO_OUT="${PATH_DIR_TMP}/out"
20
+    readonly PATH_FIFO_RET="${PATH_DIR_TMP}/ret"
21
+
22
+    if [ -z "${PATH_DIR_TMP}" ]; then
23
+        fatal "Unable to create a temporary directory"
24
+    fi
25
+
26
+    args=""
27
+    for i in "$@"; do
28
+        if [ -z "${args}" ]; then
29
+            args="\\'${i}\\'"
30
+        else
31
+            args="${args} \\'${i}\\'"
32
+        fi
33
+    done
34
+
35
+    mkfifo "${PATH_FIFO_IN}" "${PATH_FIFO_OUT}"
36
+
37
+    _echo \
38
+        "create 'fzy ${args} < \\'${PATH_FIFO_IN}\\' > \\'${PATH_FIFO_OUT}\\' 2>&1; echo $? > \\'${PATH_FIFO_RET}\\''" \
39
+        > "${DVTM_CMD_FIFO}"
40
+    cat <&0 > "${PATH_FIFO_IN}" &
41
+    cat < "${PATH_FIFO_OUT}"
42
+
43
+    readonly CODE_RET=$(head -n 1 "${PATH_FIFO_RET}")
44
+
45
+    rm -rf "${PATH_DIR_TMP}"
46
+
47
+    exit "${CODE_RET}"
48
+}
49
+
50
+main "$@"