forked from dcreemer/1pass
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbash_completion.sh
78 lines (74 loc) · 2.52 KB
/
bash_completion.sh
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
#! /usr/bin/env bash
# add fuzzyfind (fzf) completion for 1pass objects
function _fzf_complete_1pass() {
local doFzf=false
local cword="${COMP_WORDS[$COMP_CWORD]}"
if _should_1p_fzf_complete; then
doFzf=true
local trigger=${FZF_COMPLETION_TRIGGER-'**'}
if [[ -z "$cword" ]]; then
COMP_WORDS[$COMP_CWORD]=$trigger
elif [[ "$cword" != *"$trigger" ]]; then
COMP_WORDS[$COMP_CWORD]="$cword$trigger"
fi
fi
local item
local words=("${COMP_WORDS[@]::${#COMP_WORDS[@]}-1}")
for i in "${!words[@]}"; do
local curr=${words[$i]}
if [[ $i -ne 0 ]] && [[ ${curr} != "-"* ]]; then
item=${curr}
break
fi
done
# Avoid any aliases that might be set
local opcmd="command 1pass"
local rcmd=""
if [[ -z "$item" ]]; then
rcmd="${opcmd}"
else
rcmd="${opcmd} -l \"$item\""
fi
if ${doFzf}; then
_fzf_complete --reverse --prompt="1pass> " -- "${@}" < <(eval "$rcmd")
else
COMPREPLY=()
local search
# The rest adapted from https://stackoverflow.com/a/1146716/190100
search=$(eval echo "$cword" 2>/dev/null || eval echo "$cword'" 2>/dev/null || eval echo "$cword\"" 2>/dev/null || "" )
local IFS=$'\n'
while read -r line; do COMPREPLY+=("$line"); done < <(compgen -W "$(_1p_entries "${rcmd}")" -- "$search")
local escaped_single_qoute="'\''"
local i=0
for entry in ${COMPREPLY[*]}
do
if [[ "${cword:0:1}" == "'" ]]
then
# started with single quote, escaping only other single quotes
# [']bla'bla"bla\bla bla --> [']bla'\''bla"bla\bla bla
COMPREPLY[$i]="${entry//\'/${escaped_single_qoute}}"
elif [[ "${cword:0:1}" == "\"" ]]
then
# started with double quote, escaping all double quotes and all backslashes
# ["]bla'bla"bla\bla bla --> ["]bla'bla\"bla\\bla bla
entry="${entry//\\/\\\\}"
COMPREPLY[$i]="${entry//\"/\\\"}"
else
# no quotes in front, escaping _everything_
# [ ]bla'bla"bla\bla bla --> [ ]bla\'bla\"bla\\bla\ bla
entry="${entry//\\/\\\\}"
entry="${entry//\'/\'}"
entry="${entry//\"/\\\"}"
COMPREPLY[$i]="${entry// /\\ }"
fi
(( i++ ))
done
fi
}
complete -F _fzf_complete_1pass -o default -o bashdefault 1pass
function _should_1p_fzf_complete() {
${ONEPASS_FZF_COMPLETE:-true} && declare -f _fzf_complete > /dev/null 2>&1
}
function _1p_entries() {
eval "${@}" | sed -e "{" -e 's#\\#\\\\#g' -e "s#'#\\\'#g" -e 's#"#\\\"#g' -e "}"
}