-
Notifications
You must be signed in to change notification settings - Fork 5
/
zpyi.zsh
70 lines (58 loc) · 1.79 KB
/
zpyi.zsh
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
###########################################
# Python interpreter fall-back for zsh #
# Original Author: #
# Saksham Sharma (sakshamsharma) #
# Email: [email protected] #
###########################################
command_not_found_handler() {
PIPE_DIR=${XDG_CACHE_HOME:-$HOME/.cache}
if [ ! -d "$PIPE_DIR" ]; then
mkdir -p "$PIPE_DIR"
fi
INPUT_PIPE=$PIPE_DIR/.zpyi_in
ARGS_PIPE=$PIPE_DIR/.zpyi_args
# Remove any remaining commands
rm -f $INPUT_PIPE $ARGS_PIPE
# New fifo pipe
mkfifo $INPUT_PIPE
if [ "$#" -ne 1 ]; then
touch $ARGS_PIPE
for var in "${@:2}"
do
echo "${var}" >> $ARGS_PIPE
done
fi
# Need to put to background
echo -e "$1" > $INPUT_PIPE &
# Arcane zsh invokation alert
#
# ${(%):-%x} below will
# be interpolated as
# the absolute path to the file
# this line is written in.
# see man zshmisc for this and other
# sanity testing obscure zsh magic
# This allows the zpyi repo to be checked out
# and sourced from anywhere in the filesystem
# and still work.
zpyi_dir=$(dirname ${(%):-%x})
if [[ -a ${zpyi_dir}/zpyi.py ]]; then
if [ "$#" -ne 1 ]; then
python ${zpyi_dir}/zpyi.py $INPUT_PIPE $ARGS_PIPE
rm $ARGS_PIPE
else
python ${zpyi_dir}/zpyi.py $INPUT_PIPE
fi
else
echo "Couldn't find ${zpyi_dir}/zpyi.py...Downloading again might help."
fi
# Optionally uncomment the below lines if
# you do not use python so often
# and would like to see the default error instead of
# the python errors
#
# if [ $? -ne 0 ]
# then
# echo "zsh, python: Command not found or invalid => $1"
# fi
}