-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathchrepo
More file actions
executable file
·218 lines (180 loc) · 4.65 KB
/
chrepo
File metadata and controls
executable file
·218 lines (180 loc) · 4.65 KB
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#!/bin/bash
#
# chrepo
unset BLD && declare BLD="\e[1m"
unset UND && declare UND="\e[4m"
unset OFF && declare OFF="\e[m"
unset BLK && declare BLK="\e[0;90m"
unset RED && declare RED="\e[0;91m"
unset GRN && declare GRN="\e[0;92m"
unset YEL && declare YEL="\e[0;93m"
unset BLU && declare BLU="\e[0;94m"
unset MGT && declare MGT="\e[0;95m"
unset CYN && declare CYN="\e[0;96m"
unset WHT && declare WHT="\e[0;97m"
unset LIL && declare LIL="$CYN"
unset LNK && declare LNK="$UND\e[1;96m"
unset BBLK && declare BBLK="\e[1;90m"
unset BRED && declare BRED="\e[1;91m"
unset BGRN && declare BGRN="\e[1;92m"
unset BYEL && declare BYEL="\e[1;93m"
unset BBLU && declare BBLU="\e[1;94m"
unset BMGT && declare BMGT="\e[1;95m"
unset BCYN && declare BCYN="\e[1;96m"
unset BWHT && declare BWHT="\e[1;97m"
unset BLIL && declare BLIL="\e[1m$LIL"
unset BWOR && declare BWOR='\e[1;37;41m'
declare INF="$YEL"
declare MNU="$BYEL"
declare STA="$BGRN"
declare WRN="$BRED"
declare CAU="$BLIL"
declare URL="$CYN"
declare qstr="${MNU}'q'$OFF"
declare repomap=$HOME/.config/patchreview/repomap
declare repolist=$HOME/.config/patchreview/repolist
declare usagestr=$(
cat <<EOF
chrepo [reponame | show]
Description:
cd to a directory denoted by "repodir"
Create a ~/.config/patchreview/repomap file to map names
to repo directories, with the name in the first field and
the directory in the second. E.g...
rh7 /work/7/kernel
rh8 /work/7/kernel
Arguments:
show - dumps the contents of ~/.config/patchreview/repomap
reponame - the name mapped to a specific directory in the
~/.config/patchreview/repomap file
If you do not present a name from the repomap firle as an arugment,
you will get a numbered list of repo directories stored in your
repolist file.
If that file does not exist, then create it by putting the names
of your repo directories in ...
~/.config/patchreview/repomap
\0
EOF
)
declare norepolist=$(
cat <<EOF
The $repolist file containing a list of your repo directories does
not exist. Please create it.
\0
EOF
)
declare nomatch=$(
cat <<EOF
The repo name you submitted could not be found in
$repomap.
EOF
)
usage() {
echo -en "$usagestr"
}
#** getchar(): reads one char and echoes it to the screen
#
# Returns the char in the reference veriable.
# User does not need to press RETURN/ENTER
#
# $1 - reference variable to receive the char
#*
getchar() {
local -n uigc_char="$1"
read -r -n1 uigc_char
echo
return 0
}
#** promptgetchar(): print a prompt and get a char with echo
#
# Returns the character in the reference variable
# User does not need to press RETURN/ENTER
#
# $1 - prompt, may include escape chars
# $2 - reference variable to receive the char
#*
# Prints a prompt passed in $1 and returns the char in $2
#
promptgetchar() {
echo -en "$1"
getchar "$2"
return 0
}
#** _getuser_: reads user input
#
# Read multi char input terminated by RETURN/ENTER.
# Does not support backslash continuation lines.
#
# $1 - prompt string
# $2 - reference variable to receive user input
#*
_getuser_() {
local promptstring="$1"
local -n uigu_var="$2"
echo -ne "$INF$promptstring$OFF"
read -e -r uigu_var
eval "uigu_var=\"$uigu_var\""
}
#** loop_range_q()
#
# $1 - minimum
# $2 - maximum
# $3 - reference value to receive the user's choice
#
# Loop until user gives a number in the range defined by the args or
# enters 'q' to quit and return to the caller with the 'q'
#*
loop_range_q() {
local min="$1"
local max="$2"
local number
local cmd
local pstr=\
"$OFF${INF}Please enter a number between $MNU$min$INF and $MNU$max$OFF or $qstr: "
echo -e "${INF}Press $qstr$INF to return without making a selection.$OFF"
((max < 10)) && cmd=promptgetchar || cmd=_getuser_
# echo "cmd: $cmd"
while true; do
$cmd "$pstr" number
[ "$number" = "q" ] && return 1
if [[ $number =~ ^-?[0-9]+$ ]] && \
[[ $number -ge $min ]] && \
[[ $number -le $max ]]; then
eval "$3"="$number"
echo
return 0
fi
done
}
chrepo() {
[ ${#@} -eq 1 ] || { pwd; return 0; }
( [ "$1" == "help" ] || [ "$1" == "-h" ] ) && { usage; return 0; }
local arg="$1"
local repod=
local dary=()
local dndx=0
local dnum=
if [ -f "$repomap" ]; then
[ "$arg" == "show" ] && { cat "$repomap"; return 0; }
fi
line=$(grep -w $arg $repomap)
if [ "$line" ]; then
repod=$(echo $line | cut -d' ' -f2)
else
echo -e "$nomatch"
[ -f "$repolist" ] || { echo -e "$norepolist"; return 1; }
dary=($(cat $repolist))
for ((dndx = 0; dndx < ${#dary[@]}; ++dndx)); do
echo -e "$MNU$((dndx+1)). $OFF${dary[dndx]}"
done
loop_range_q 1 ${#dary[@]} dnum || return 1
repod=${dary[dnum-1]}
[ -d "$repod" ] || {
echo -e "$WRN$repod is not a directory!$OFF"
return 1
}
fi
cd $repod
pwd
return 0
}