-
Notifications
You must be signed in to change notification settings - Fork 0
/
papis-fzf
executable file
·113 lines (89 loc) · 3.33 KB
/
papis-fzf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#!/usr/bin/bash
# MADE by Twix ::
# https://github.com/Twix53791
#========================================
# Handles papis-fzf multiprocesses safely
#
# Communicates with papis-fzf.pl through
# named pipes (fifo).
# This wrapper only :
# - set working directories
# - run fzf-papis when papis-fzf.pl asks
# - run nano when papis-fzf.pl asks
# - trap exit
#
# papis-fzf.pl:
# - handles all the logic
# - handles command background processes
# - displays everything that is just text to stdout
# - exit running the exit::command
# This script will handle the command, to close
# safely all the processes (trap '_exit' EXIT)
#========================================
## Sets working directories
papisfzfdir=$HOME/.papis-fzf
indexdir=$HOME/.cache/papis-fzf
DIRECTORIES+=("$indexdir")
[[ ! -d $indexdir ]] && mkdir $indexdir
configdir=$HOME/.config/papis-fzf
DIRECTORIES+=("$configdir")
tmpdir=/tmp/papis-fzf
DIRECTORIES+=("$tmpdir")
[[ ! -d $tmpdir ]] && mkdir $tmpdir
#tmplog=$tmpdir/papis-fzf.log
FIFO_TO_PL="$tmpdir/fifo-sh-to-pl-$$"
FIFO_TO_SH="$tmpdir/fifo-pl-to-sh-$$"
[[ ! -p $FIFO_TO_PL ]] && mkfifo $FIFO_TO_PL
[[ ! -p $FIFO_TO_SH ]] && mkfifo $FIFO_TO_SH
## Traps exit (safe exit). Trap many signals, included INT (ctrl-c)
trap '_exit' EXIT
_exit () {
pkill fzf-papis # ! important ! fix stty bug if test killed before fzf
kill $pid 2>/dev/null
rm $FIFO_TO_PL $FIFO_TO_SH
}
## Debug value
[[ $@ == *"-d"* || $@ == *"--debug"* ]] && debug=1
[[ $debug == 1 && $@ == *2* ]] && debug=2 # gross, but enough
## Runs papis-fzf.pl
$papisfzfdir/papis-fzf.pl $FIFO_TO_PL $FIFO_TO_SH "${DIRECTORIES[@]}" "$@" &
pid=$! # get the pid of the papis-fzf.pl process
## Handles requests from papis-fzf.pl via FIFO_TO_SH
output=0
while :; do
# Reads the fifo
# output is null only if the fzf-papis process is interrupted
# If null, it will exec the PREVIOUS $input command
# = it 'refresh' the fzf-papis menu
[[ -n $output ]] && input=$(cat $FIFO_TO_SH)
[[ $debug == 2 ]] &&
echo -e "====\npapis-fzf DEBUG :: $FIFO_TO_SH received :" &&
echo -e "$input\e[0m"
[[ $input == *":--exit--"* ]] && exit # from commands::exit
if [[ $input =~ ^:edit ]]; then
input="${input:6}" # removes ':edit ' key from the string
eval $input # :edit command in papis-fzf.pl
output=$? # 0 OR 1
elif [[ $input =~ ^:read ]]; then
read -s -r -N1 output
elif [[ $input =~ ^:print ]]; then
echo "$input" | tail -n +2 2>/dev/null
exit
else
output=$(eval $input) # fzf-papis
# Output = fzf-papis selection OR null is interrupted
# The output is a simple string, each fzf entries separated
# by a new line.
fi
# Sends the output to papis-fzf.pl.
# NOTE: the '--' indicates to io::fifo_in the end of the output
if [[ -n $output ]]; then
echo -en "$output\n--\n" > $FIFO_TO_PL
[[ $debug == 2 ]] && echo -e "====\npapis-fzf DEBUG :: output sent to $FIFO_TO_PL :" && echo "$output"
fi
# Goto next loop: it will wait for the fifo input.
# In practical terms, it will be instantaneous, as papis-fzf.pl
# sends back an instruction straightaway.
sleep .1
done