-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsuggest.sh
executable file
·149 lines (127 loc) · 2.83 KB
/
suggest.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
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
#!/usr/bin/env bash
if ! type -f bpkg-utils &>/dev/null; then
echo "error: bpkg-utils not found, aborting"
exit 1
fi
# shellcheck source=lib/utils/utils.sh
source "$(which bpkg-utils)"
## output usage
usage () {
echo "usage: bpkg-suggest [-h|--help] <query>"
}
count_lines () {
local -i count=0
while read -r line; do
echo "$line"
(( count++ ))
done
return "$count"
}
prefix_lines_with_length () {
while read -r line; do
echo "$(echo -n "$line" | wc -c | tr -d ' ')" "$line"
done
return $?
}
sort_lines () {
prefix_lines_with_length | sort -n | awk '{ $1=""; print $0 }'
return $?
}
suggest () {
local count=0
local query="$1"
case "$query" in
-h|--help)
usage
return 0
;;
*)
if [ "-" = "${query:0:1}" ]; then
echo >&2 "error: Unknown argument \`$query'"
return 1
fi
;;
esac
find_suggestions "$@" | sort_lines | count_lines
count=$? ## count is stored in last return value
if (( count > 0 )); then
if (( count == 1 )); then
{
echo
bpkg_message "green" " suggest" "1 result found"
} >&2
else
{
echo
bpkg_message "green" " suggest" "$count result(s) found"
} >&2
fi
else
{
echo
bpkg_message "red" " suggest" "Couldn't find anything to match \`$query'"
} >&2
return 1
fi
return 0
}
## main
find_suggestions () {
local paths seen find_supports_maxdepth
declare -a paths=()
declare -a seen=()
local query="$1"
case "$query" in
-h|--help)
usage
return 0
;;
*)
if [ "-" = "${query:0:1}" ]; then
echo >&2 "error: Unknown argument \`$query'"
return 1
fi
;;
esac
if find --help 2>/dev/null | grep 'maxdepth' >/dev/null 2>&1; then
find_supports_maxdepth=1
else
find_supports_maxdepth=0
fi
IFS=':' read -r -a paths <<< "$PATH"
for (( i = 0; i < ${#paths[@]}; ++i )); do
local path="${paths[$i]}"
local skip=0
## omit non existent paths
if ! test -d "$path"; then
continue
else
for (( n = 0; n < "${#seen[@]}"; ++n )); do
if [ "$path" = "${seen[$n]}" ]; then
skip=1;
break;
fi
done
## check if skip needed
if [ "1" = "$skip" ]; then
continue
fi
fi
## mark seen
seen+=("$path")
if (( find_supports_maxdepth == 1 )); then
# echo find "$path" -name "$query*" -prune -print -maxdepth 1 >&2
find "$path" -name "$query*" -prune -print -maxdepth 1 2>/dev/null
else
echo >&2 " warn: Using 'find' command with '-maxdepth' option. Results may appear slowly"
find "$path" -name "$query*" -prune -print -maxdepth 1 2>/dev/null
fi
done
return $?
}
## export or run
if [[ ${BASH_SOURCE[0]} != "$0" ]]; then
export -f suggest
else
suggest "$@"
fi