This repository has been archived by the owner on Jan 23, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
suggest
executable file
·75 lines (61 loc) · 2.54 KB
/
suggest
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
#!/usr/bin/bash
#
# SPDX-FileCopyrightText: 2019 Dmytro Kolomoiets <amerlyq@gmail.com> and contributors
#
# SPDX-License-Identifier: Apache-2.0
#
#%SUMMARY: suggest completion for command prefix [relative to basedir]
#%USAGE: $ ./$0 /path/to/basedir/cmd/pfx [/path/to/basedir] [find_args..]
#%IDEA: reduce number of suggestions (show only API aliases but not content)
#% * . => .{bb,cf,jr}
#% * .j => .jr/
#% * .jr => .jr/{a,b,c,d}
#%
#%PERF: 76ms total $ wiw kirie/suggest ~/.cache/wiw/.cf
set -o errexit -o noclobber -o noglob -o nounset -o pipefail
# THINK: prevent alias symlinks pointing to outside dirs -- or not ?
# HACK:IDEA: wiw "(bin|sys)/" -> may directly point to /usr/bin :)
all=
find_args=()
case ${1-}
in -a|--all) all=1 && shift
esac
# MAYBE:BET: make "$1=basedir" obligatory and pass "-r|--relative" OR "-p|--absolute" to make args simpler
pfx=${1-}
base=${2-}
(($#>1)) && find_args=( "${@:3}" )
# ATT! don't use PWD as default directory
# * BAD: it's too unexpected for "kirie" suite which must use its own defaults
# * SECU:PERF: current dir can be HOME -- must prevent searching of all contained files indiscriminately
[[ ${pfx:0:1} == / ]] || pfx=${SUITE_CACHE_HOME:?}/$pfx
nm=${pfx##*/}
dir=${pfx%$nm}
# DECI:(LC_ALL=C): arbitrary sorting of punctuation VS proper sorting of any UTF8
findexe(){ local d args fmt='%p\n'
d=$1 && shift
if [[ -n $base ]]; then
fmt=$(realpath --no-symlinks --relative-base="$base" -- "$d")
[[ $fmt == /* ]] && exit 2
[[ $fmt == . ]] && fmt='%P\n' || fmt+='/%P\n'
fi
# IDEA: return only aliases when "" or "*/", but search within aliases when "?*"
((all)) && args=( -L ) || args=( -H )
args+=( "$d" -mindepth 1 \( -name '_*' -o -name '.git' \) -prune -o )
((all)) && args+=( -type f ) || args+=( \( -xtype f -o \( -xtype d -type l \) \) )
args+=( -executable "$@" "${find_args[@]}" )
[[ ${find_args[*]:+x} ]] || args+=( -printf "$fmt" )
find "${args[@]}" | LC_ALL=C sort --ignore-case
}
if [[ $nm != . && $nm != .. ]]; then
# [[ -z $pfx ]] && { findexe "$PWD"; exit; }
[[ -d $pfx ]] && { findexe "$pfx"; exit; }
# BUG:FIXME: must be relative if $base
[[ -e $pfx ]] && { printf '%s\n' "$pfx"; exit; }
fi
# [[ -z $dir ]] && { findexe "$PWD" -path "${PWD}/${nm}?*"; exit; }
[[ -d $dir ]] && { findexe "$dir" -path "${pfx}?*"; exit; }
[[ -e $dir ]] && { printf '%s\n' "$dir"; exit; }
# ALT:(always exit 1): FAILED: inconsistency between ::
# * "~/.cache/wiw/sss" -- no error for unknown file in existing dir
# * "~/.cache/wiw/sss/xxx" -- ERROR for unknown file in unknown dir
exit 0