forked from m42e/zsh-histdb-skim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzsh-histdb-skim.zsh
59 lines (50 loc) · 1.4 KB
/
zsh-histdb-skim.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
THIS_PATH=${0:a:h}
BIN_PATH=${THIS_PATH}/bin/zsh-histdb-skim
BIN_DIR=${THIS_PATH}/bin
histdb-skim-get-os(){
UNAME_STR=`uname -a`
if [[ ( $UNAME_STR =~ '.*Darwin.*' ) && ( $UNAME_STR =~ '.*x86_64.*') ]]; then
echo -n "darwin-x64"
fi
if [[ ( $UNAME_STR =~ '.*Linux.*' ) && ( $UNAME_STR =~ '.*x86_64.*') ]]; then
echo -n "linux-x64"
fi
}
histdb-skim-get-latest-version(){
curl -s https://github.com/m42e/zsh-histdb-skim/releases/latest | grep --color=never -o 'v[0-9]*\.[0-9]*\.[0-9]*'
}
histdb-skim-download(){
if [[ -z $(histdb-skim-get-os) ]]; then
echo "Could not find prebuild executable"
echo "Sorry, you have to do it yourself"
else
echo "Downloading binary"
mkdir -p ${BIN_DIR}
curl -s -JL https://github.com/m42e/zsh-histdb-skim/releases/download/$(histdb-skim-get-latest-version)/zsh-histdb-skim-$(histdb-skim-get-os) -o ${BIN_PATH}
chmod +x ${BIN_PATH}
fi
}
histdb-skim-ensure () {
if [[ ! -f ${BIN_PATH} ]]; then
histdb-skim-download
fi
}
histdb-skim-widget() {
histdb-skim-ensure
origquery=${BUFFER}
output=$( \
HISTDB_HOST=${HISTDB_HOST:-"'$(sql_escape ${HOST})'"} \
HISTDB_SESSION=$HISTDB_SESSION \
HISTDB_FILE=$HISTDB_FILE \
${BIN_PATH} "$origquery"\
)
if [ $? -eq 0 ]; then
BUFFER=$output
else
BUFFER=$origquery
fi
CURSOR=$#BUFFER
zle redisplay
}
zle -N histdb-skim-widget
bindkey '^R' histdb-skim-widget