-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy patharch-wiki
executable file
·126 lines (112 loc) · 2.15 KB
/
arch-wiki
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
#!/usr/bin/bash
ROOT=/usr/share/doc/arch-wiki-markdown
LESS="vim -u /usr/share/vim/vim7*/macros/less.vim -n"
usage()
{
cat << EOF
usage: $0 options
This script run the test1 or test2 over a machine.
OPTIONS:
-h Show this message
-g Test type, can be ‘test1′ or ‘test2′
-#1 -1 for first match, -2 for second ...
-p Plain text mode. No interactive mode.
See $ROOT
EOF
}
# we require input
if [[ $# == 0 ]]; then
usage
exit 0
fi
# parse options
AWESOME_STYLE=1
while [[ $# > 0 ]]; do
case $1 in
-h|--help)
usage
exit
;;
-g|--grep)
shift
GLOBAL_SEARCH="$1"
shift
;;
-n|--normal)
AWESOME_STYLE=0
shift
;;
-l|--list)
find $ROOT
exit 0
;;
-v|--version)
cat $ROOT/version
exit 0
;;
*)
break
;;
esac
done
# grep
if [[ -n $GLOBAL_SEARCH ]]; then
echo "Searching for $GLOBAL_SEARCH"
(cd $ROOT; grep --color=always -rn "$GLOBAL_SEARCH" | less -X -F -R)
exit
fi
# search title
KEY=N/A
NTH=0
while [ -n "$1" ]; do
case $1 in
-*) NTH=${1##-}; AWESOME_STYLE=0 ;;
*) KEY=$1 ;;
esac
shift
done
# trim/search
NTH=$((NTH - 1))
DOC=( $(ls $ROOT | grep -i $KEY) )
# open a doc
open() {
$LESS -c 'set syntax=markdown' $ROOT/${DOC[$1]}
exit 0
}
# no match
if [[ ${#DOC[*]} == 0 ]]; then
echo "Can't find $KEY"
exit 1
fi
# single match or nth is specified
if [[ ${#DOC[*]} == 1 ]] || [[ $NTH != -1 ]]; then
open $NTH
else
# menu
for ((cnt=0; cnt<${#DOC[*]}; cnt ++)); do
if [ $AWESOME_STYLE -eq 1 ] ; then
echo -n "[$((cnt+1))] "
fi
echo ${DOC[$cnt]}
done
# now ask the user for number
if [ $AWESOME_STYLE -eq 1 ] ; then
upto=${#DOC[*]}
echo ; # just for nice formatting
while true; do
read -p "Please choose a number from the list [1-$upto|q]: " input
case $input in
[Qq]*) exit;;
[0-9]*)
if ((1<=$input && $input<=$upto)) ; then
echo "Opening" ${DOC[$input-1]};
open $(($input-1))
else
echo "Invalid number" $input;
fi
;;
*) echo "Invalid option. Use q to quit.";;
esac
done
fi
fi