-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathbash_commander
More file actions
301 lines (283 loc) · 7.61 KB
/
bash_commander
File metadata and controls
301 lines (283 loc) · 7.61 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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
# Declarations for bash commander
if [ "$USER" = "root" ]; then
# Use brown color to keep user aware of root privileges.
COMMANDER='no=\33[m:nr=\33[0;37;43m:bo=\33[1;37m:br=\33[1;37;43m:di=\33[0;33m'
PS1="\[\033[43m\](\h) \W\[\033[m\033[1m\] \\\$\[\033[m\] "
else
COMMANDER='no=\33[m:nr=\33[0;37;43m:bo=\33[1;37m:br=\33[1;37;43m:di=\33[0;36m'
PS1="\[\033[44m\](\h) \W\[\033[m\033[1m\] \\\$\[\033[m\] "
fi
declare -x DIALOGRC=/usr/local/etc/bash_dialog
function commander_start_file () { # Start file
if [[ `expr "$1" : '[a-zA-Z0-9_./,:@+-]*$'` != "0" ]]; then
f="$1"
else
f="'$1'"
fi
case "$1" in
*.c) COMMANDER_LINE="${EDITOR:-vi} $f" ;;
*.h) COMMANDER_LINE="${EDITOR:-vi} $f" ;;
*.cpp) COMMANDER_LINE="${EDITOR:-vi} $f" ;;
*.tar) COMMANDER_LINE="tar tvf $f | less" ;;
*.tar.Z) COMMANDER_LINE="tar tvzf $f | less" ;;
*.tar.gz) COMMANDER_LINE="tar tvzf $f | less" ;;
*.tgz) COMMANDER_LINE="tar tvzf $f | less" ;;
*.tar.bz2) COMMANDER_LINE="tar tvjf $f | less" ;;
*.zip) COMMANDER_LINE="unzip -l $f | less" ;;
*.rar) COMMANDER_LINE="unrar vb $f | less" ;;
*) if [ -f "$1" -a -x "$1" ]; then
COMMANDER_LINE="./$f"
# Switch to line mode
return 0
fi ;;
esac
# Stay in panel mode
return 1
}
function commander_f1 () { # Help
${DIALOG:-dialog} --title " Help " --cr-wrap --msgbox "\
Cursor Movement Delete\n\
Char left ^B Char left ^H or <BS>\n\
Char right ^F Under cursor ^D or <Del>\n\
Start of line ^A All line ^U\n\
End of line ^E To end of line ^K\n\
\n\
Other\n\
Reread directory F2\n\
Panels Select/Unselect ^T or <Ins>\n\
Switch panels ^I or <Tab> Previous command ^P\n\
Panels on/off ^O Next command ^N\n\
Show dot files ^X a Run file or\n\
Enter directory ^M or <Ret>\n\
Use file name ^J or <LF>\n\
" 0 0
return 0
}
function commander_f2 () { # Change directory
# Select fom list: $HOME or / or another panel,
# or /mnt/foo or /Volume/bar or /media/xyz
declare -a args
args=("$2" "")
if [ "$3" != "$2" -a "$3" != "/" -a "$3" != "$HOME" ]; then
args[2]="$3"
args[3]=""
fi
if [ "$HOME" != "$2" -a "$HOME" != "/" ]; then
args[4]="$HOME"
args[5]=""
fi
if [ "/" != "$2" ]; then
args[6]="/"
args[7]=""
fi
n=8
for d in /mnt/* /media/* /Volumes/*; do
case "$d" in
*\*)
;;
*)
args[$((n++))]="$d"
args[$((n++))]=""
;;
esac
done
exec 3>&1
value=`${DIALOG:-dialog} --title " Change directory " --menu \
"Select directory:" 0 0 5 "${args[@]}" 2>&1 1>&3`
retval=$?
exec 3>&-
unset args
if [ $retval = 0 ]; then
cd "$value"
return 1 # Re-read a directory
fi
return 0
}
function commander_f3 () { # View current file
if [ -f "$1" -a -r "$1" ]; then
${VIEWER:-vi -R} "$1"
fi
return 0
}
function commander_f4 () { # Edit current file
if [ -f "$1" -a -w "$1" ]; then
${EDITOR:-vi} "$1"
return 1 # Re-read a directory
fi
return 0
}
function commander_f5 () { # Copy file(s)
f="$1"; shift
d="$1"; shift
d2="$1"; shift
if [ $# -gt 0 ]; then
# Several tagged files/dirs
n="$#"
exec 3>&1
value=`${DIALOG:-dialog} --title " Copy " --inputbox \
"Copy $n files to:" 0 0 "$d2" 2>&1 1>&3`
retval=$?
exec 3>&-
if [ $retval != 0 ]; then
return 0
fi
# Check if the target already exists
if [ ! -d "$value" ]; then
${DIALOG:-dialog} --title " Error " --ok-label "Copying aborted"\
--msgbox "$value - nonexistent directory"
return 0
fi
all=0
for f; do
if [ $all != 1 -a -e "$value/$f" ]; then
# Target already exists, ask user for overwrite
${DIALOG:-dialog} --title " Copy " --ok-label "Overwrite" \
--extra-button --extra-label "All" \
--no-label "Cancel" \
--yesno "File exists: $value/$f" 0 0
case $? in
0) ;; # Overwrite
3) all=1 ;; # Overwrite All
*) return 0 ;; # Cancel
esac
fi
${DIALOG:-dialog} --infobox "Copying $f\nto $value/" 0 0
cp -p -R -f "$f" "$value"
done
return 1 # Re-read a directory
fi
# Single file or directory
exec 3>&1
value=`${DIALOG:-dialog} --title " Copy " --inputbox \
"Copy file $f to:" 0 0 "$d2/$f" 2>&1 1>&3`
retval=$?
exec 3>&-
if [ $retval != 0 ]; then
return 0
fi
# Check if the target already exists
if [ -d "$value" ]; then
target="$value/$f"
else
target="$value"
fi
if [ -e "$target" ]; then
# Target already exists, ask user for overwrite
${DIALOG:-dialog} --title " Copy " --yes-label "Overwrite" \
--no-label "Cancel" --yesno "File exists: $target" 0 0
if [ $? != 0 ]; then
return 0
fi
fi
${DIALOG:-dialog} --infobox "Copying $f\nto $value" 0 0
cp -p -R -f "$f" "$value"
return 1 # Re-read a directory
}
function commander_f6 () { # Move file(s)
f="$1"; shift
d="$1"; shift
d2="$1"; shift
if [ $# -gt 0 ]; then
# Several tagged files/dirs
n="$#"
exec 3>&1
value=`${DIALOG:-dialog} --title " Move " --inputbox \
"Rename or move $n files to:" 0 0 "$d2" 2>&1 1>&3`
retval=$?
exec 3>&-
if [ $retval != 0 ]; then
return 0
fi
# Check if the target already exists
if [ ! -d "$value" ]; then
${DIALOG:-dialog} --title " Error " --ok-label "Renaming aborted"\
--msgbox "$value - nonexistent directory"
return 0
fi
all=0
for f; do
if [ $all != 1 -a -e "$value/$f" ]; then
# Target already exists, ask user for overwrite
${DIALOG:-dialog} --title " Move " --ok-label "Overwrite" \
--extra-button --extra-label "All" \
--no-label "Cancel" \
--yesno "File exists: $value/$f" 0 0
case $? in
0) ;; # Overwrite
3) all=1 ;; # Overwrite All
*) return 0 ;; # Cancel
esac
fi
${DIALOG:-dialog} --infobox "Moving $f\nto $value/" 0 0
rm -rf "$value/$f"
mv -f "$f" "$value"
done
return 1 # Re-read a directory
fi
# Single file or directory
exec 3>&1
value=`${DIALOG:-dialog} --title " Move " --inputbox \
"Rename or move file $f to:" 0 0 "$d2/$f" 2>&1 1>&3`
retval=$?
exec 3>&-
if [ $retval != 0 ]; then
return 0
fi
# Check if the target already exists
if [ -d "$value" ]; then
target="$value/$f"
else
target="$value"
fi
if [ -e "$target" ]; then
# Target already exists, ask user for overwrite
${DIALOG:-dialog} --title " Move " --yes-label "Overwrite" \
--no-label "Cancel" --yesno "File exists: $target" 0 0
if [ $? != 0 ]; then
return 0
fi
fi
${DIALOG:-dialog} --infobox "Moving $f\nto $value" 0 0
rm -rf "$target"
mv -f "$f" "$value"
return 1 # Re-read a directory
}
function commander_f7 () { # Make directory
exec 3>&1
value=`${DIALOG:-dialog} --title " Make directory " --inputbox \
"Create the directory:" 0 0 "" 2>&1 1>&3`
retval=$?
exec 3>&-
if [ $retval != 0 ]; then
return 0
fi
mkdir -p "$value"
return 1 # Re-read a directory
}
function commander_f8 () { # Delete file(s)
f="$1"; shift
d="$1"; shift
d2="$1"; shift
if [ $# -gt 0 ]; then
# Several tagged files/dirs
n="$#"
${DIALOG:-dialog} --title " Delete " --yes-label "Delete" \
--no-label "Cancel" \
--yesno "Do you wish to delete $n files?" 0 0
if [ $? != 0 ]; then
return 0
fi
${DIALOG:-dialog} --infobox "Deleting $n files" 0 0
rm -rf "$@"
return 1 # Re-read a directory
fi
# Single file or directory
${DIALOG:-dialog} --title " Delete " --yes-label "Delete" --no-label "Cancel" \
--yesno "Do you wish to delete: $f" 0 0
if [ $? != 0 ]; then
return 0
fi
${DIALOG:-dialog} --infobox "Deleting $f" 0 0
rm -rf "$f"
return 1 # Re-read a directory
}