-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpick
executable file
·66 lines (61 loc) · 1.33 KB
/
pick
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
#!/bin/bash
set -eu
set -o pipefail
help() {
cat <<EOT
Select items from a list, interactively or not.
pick -h|--help
This help
pick [-g|--grep] pattern ...
Only include results matching <pattern>.
If multiple patterns are given, all must match.
pick -i|--ignore pattern ...
Exclude results matching <pattern>.
pick -p|--percol ...
Interactively filter the resources with Percol.
pick --skip-header-line ...
Ignore the first line of input.
EOT
}
INCLUDE=""
EXCLUDE=""
PERCOL=""
SKIP_HEADER_LINE=""
while (( ${#*} > 0 )); do
case $1 in
-h|--help)
help
exit
;;
-g|--grep)
shift
INCLUDE="$INCLUDE|$1"
shift
;;
-i|--ignore)
shift
EXCLUDE="$EXCLUDE|$1"
shift
;;
-p|--percol)
shift
PERCOL=1
;;
--skip-header-line)
shift
SKIP_HEADER_LINE=1
;;
*)
INCLUDE="$INCLUDE|$1"
shift
;;
esac
done
INCLUDE="${INCLUDE#|}"
EXCLUDE="${EXCLUDE#|}"
CMD="awk '1 ${SKIP_HEADER_LINE:+&& NR>1 }${INCLUDE:+&& /$INCLUDE/ }${EXCLUDE:+&& !/$EXCLUDE/ }{ print \$1 \" \" \$2; }'"
if [[ -n "$PERCOL" ]]; then
eval $CMD | column -t | percol
exit
fi
eval $CMD