generated from srid/rib-sample
-
-
Notifications
You must be signed in to change notification settings - Fork 150
/
neuron-search
executable file
·32 lines (26 loc) · 1.09 KB
/
neuron-search
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
#!/usr/bin/env bash
set -euo pipefail
NOTESDIR=${1}
FILTERBY=${2}
SEARCHFROMFIELD=${3}
EXTENSIONS=${4}
OPENCMD=$(envsubst -no-unset -no-empty <<<${5})
EXACT=""
: ${NEURON_SEARCH_CONTEXT_ABOVE:=10}
# Use --exact for full text search
if [ -z "${FILTERBY}" ]; then
EXACT="--exact"
fi
cd ${NOTESDIR}
# The LC_ALL here is fix the "illegal byte sequence" error
# See https://github.com/srid/neuron/pull/445#discussion_r513066111
{ (rg --no-heading --line-number --with-filename --sort path "${FILTERBY}" -g "${EXTENSIONS}" || echo) \
&& rg --no-heading --no-line-number --with-filename --sort path "${FILTERBY}" -g "${EXTENSIONS}" \
--files-without-match | awk '{ printf "%s:0:# %s\n", $0, $0}'; } \
| ( [ ${FILTERBY} ] && LC_ALL=C sort -t: -k1,3 -r || cat ) \
| ( [ ${FILTERBY} ] && LC_ALL=C sort -t: -k1,1 -u || cat )\
| fzf ${EXACT} --tac --no-sort -d ':' -n ${SEARCHFROMFIELD}.. \
--preview 'bat --style=plain --color=always --highlight-line={2} {1}' \
--preview-window ':+{2}-'"$NEURON_SEARCH_CONTEXT_ABOVE" \
| awk -F: "{printf \"${NOTESDIR}/%s\", \$1}" \
| xargs -I % ${OPENCMD} %