forked from xwmx/nb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nb-completion.bash
171 lines (140 loc) · 4.03 KB
/
nb-completion.bash
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#!/usr/bin/env bash
###############################################################################
# __ _
# \ \ _ __ | |__
# \ \ | '_ \| '_ \
# / / | | | | |_) |
# /_/ |_| |_|_.__/
#
# [nb] Command line and local web note-taking, bookmarking, and archiving with
# plain text data storage, encryption, filtering and search, pinning, #tagging,
# Git-backed versioning and syncing, Pandoc-backed conversion, global and local
# notebooks, customizable color themes, [[wiki-style linking]], plugins, and
# more in a single portable, user-friendly script.
#
# https://github.com/xwmx/nb
###############################################################################
_nb_subcommands() {
# _nb_cache_completions()
#
# Usage:
# _nb_cache_completions <path>
#
# Description:
# Cache completions for `nb`. Generating completions can be slow and
# native shell caching doesn't appear to help.
_nb_cache_completions() {
local _cache_path="${1:-}"
[[ -z "${_cache_path:-}" ]] && return 0
# Remove outdated cache files.
local _base_cache_path="${_cache_path%-*}"
local __suffix=
for __suffix in "zsh" "v1"
do
if [[ -e "${_base_cache_path:?}-${__suffix:?}" ]]
then
rm -f "${_base_cache_path:?}-${__suffix:?}"
fi
done
# Rebuild completion cache.
local _commands=()
IFS=$'\n' _commands=($(nb subcommands))
local _notebooks=()
IFS=$'\n' _notebooks=($(nb notebooks --names --no-color --unarchived))
local _completions=()
IFS=$'\n' _completions=(${_commands[@]})
local _commands_cached=
local _notebooks_cached=
if [[ -e "${_cache_path}" ]]
then
local _counter=0
while IFS= read -r __line
do
_counter=$((_counter+1))
if [[ "${_counter}" == 1 ]]
then
_commands_cached="${__line}"
elif [[ "${_counter}" == 2 ]]
then
_notebooks_cached="${__line}"
else
break
fi
done < "${_cache_path}"
fi
if [[ "${_commands_cached}" != "${_commands[*]:-}" ]] ||
[[ "${_notebooks_cached}" != "${_notebooks[*]:-}" ]]
then
# Construct <notebook>:<subcommand> completions.
local __notebook=
for __notebook in "${_notebooks[@]}"
do
local __command=
for __command in "${_commands[@]}"
do
if [[ -n "${__notebook:-}" ]] &&
[[ -n "${__command:-}" ]]
then
_completions+=("${__notebook}:${__command}")
fi
done
done
local _directory_path=
_directory_path="$(dirname "${_cache_path}")"
mkdir -p "${_directory_path}"
if [[ -f "${_cache_path:?}" ]]
then
rm -f "${_cache_path:?}"
fi
touch "${_cache_path:?}"
{
(IFS=$' '; printf "%s\\n" "${_commands[*]}")
(IFS=$' '; printf "%s\\n" "${_notebooks[*]}")
printf "%s\\n" "${_completions[@]}"
} >> "${_cache_path}"
fi
}
local _nb_dir=
_nb_dir="$(nb env | grep 'NB_DIR' | cut -d = -f 2)"
if [[ -z "${_nb_dir:?}" ]] ||
[[ ! -e "${_nb_dir}" ]]
then
return 0
elif [[ -L "${_nb_dir}" ]]
then
if hash "realpath" 2>/dev/null
then
_nb_dir="$(realpath "${_nb_dir}")"
else
_nb_dir="$(readlink "${_nb_dir}")"
fi
fi
if [[ ! -d "${_nb_dir}" ]]
then
return 0
fi
local _cache_path="${_nb_dir:?}/.cache/nb-completion-cache-v2"
local _completions_cached=()
if [[ ! -e "${_cache_path}" ]]
then
_nb_cache_completions "${_cache_path}"
fi
if [[ -e "${_cache_path}" ]]
then
local _counter=0
local __line=
while IFS= read -r __line
do
_counter=$((_counter+1))
if [[ "${_counter}" -gt 2 ]]
then
_completions_cached+=("${__line}")
fi
done < "${_cache_path}"
(_nb_cache_completions "${_cache_path}" &)
fi
local _current="${COMP_WORDS[COMP_CWORD]}"
COMPREPLY=()
COMPREPLY=($(compgen -W "${_completions_cached[*]}" -- "${_current}"))
}
complete -F _nb_subcommands nb