-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyounameit
executable file
·203 lines (182 loc) · 5.47 KB
/
younameit
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
#!/usr/bin/env bash
#NOTE: This program is free software; you can redistribute it
#and/or modify it under the terms of the GNU General Public
#License as published by the Free Software Foundation; either
#version 3, or (at your option) any later version.
#This program is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
#GNU General Public License for more details.
#Bugs can be reported to Yu Zhang <[email protected]>.
BEFORE_FILE="younameit.before"
AFTER_FILE="younameit.after"
MV_CMD="mv"
CP_CMD="cp"
RM_CMD="rm -f"
VIM_CMD="vim"
FLAG_COPY=false
FLAG_FORCE=false
FLAG_QUIET=false
OPTION_VERBOSE="-v"
OPTION_FORCE=""
VERSION="$(basename $0), version 0.12"
USEAGE="usage: $(basename $0) [-hfqC] files_names ... \n
\n
\t-h\tshow this help text \n
\t-v\tshow verion imformation \n
\t-f\tallow overwriting \n
\t-q\tquiet mode which suppresses all console messages
"
# check vim
type $VIM_CMD > /dev/null 2>&1 ||
{
echo >&2 "Editor vim is not found. Abort!"
exit 1
}
# create before and after files
touch $BEFORE_FILE
echo "# before:" > $BEFORE_FILE
touch $AFTER_FILE
echo "# after:" > $AFTER_FILE
if (( 0 != $? )); then
echo "Cannot create temp files on the current working directory. Abort!"
exit 1
fi
# parse command line options
while getopts "Cqh?fv" OPTS; do
case $OPTS in
C)
FLAG_COPY=true
;;
f)
FLAG_FORCE=true
OPTION_FORCE="-f"
;;
q)
FLAG_QUIET=true
OPTION_VERBOSE=""
;;
v)
echo $VERSION
exit 0
;;
h|\?)
echo $VERSION
echo
echo -e $USEAGE
exit 0
;;
esac
done
# handle command line arguments
if (( $# == 0 )); then
ls -p --hide $BEFORE_FILE --hide $AFTER_FILE | grep -Ev '/' >> $BEFORE_FILE
ls -p --hide $BEFORE_FILE --hide $AFTER_FILE | grep -Ev '/' >> $AFTER_FILE
else
for ARG in $@
do
if [ "-" == ${ARG:0:1} ]; then continue
fi
if [ ! -e $ARG ]; then
if [ "true" != $FLAG_QUIET ]; then
echo "The file \"$ARG\" does not exist in current directory. Omitted."
fi
else
echo $ARG >> $BEFORE_FILE
echo $ARG >> $AFTER_FILE
fi
done
fi
# start vim
$VIM_CMD "-X" \
"--noplugin" \
"-c silent set number" \
"-c silent edit $AFTER_FILE" \
"-c silent vsplit $BEFORE_FILE" \
"-c silent wincmd l" \
"-c silent set nowrap"
if (( $? != 0 )); then
echo "Error when exiting vim. Abort!"
exit 1;
fi
# trim leading and trailing white spaces, blank lines, and lines beginning with the token '#'
#
# vim options
# "-X" do not connect to X server
# "--noplugin" do not load plugins
# "-e" \ start with ex mode
# "-s" \ silent mode, requires the option '-e' given before
# "-C" \ compatible (vi) mode
# "-c %s/\s\+$//" \ remove trailing white spaces for each line
# "-c %s/^\s\+//" \ remove leading whit spaces for each line
# "-c g/^$/d" \ remove blank lines
# "-c g/^#/d" \ remove '#' lines
# "-c wq" write buffer and quite
$VIM_CMD $BEFORE_FILE \
"-X" \
"--noplugin" \
"-e" \
"-s" \
"-C" \
"-c %s/\s\+$//" \
"-c %s/^\s\+//" \
"-c g/^$/d" \
"-c g/^#/d" \
"-c wq"
BEFORE_LINE_NUMBER=$(wc -l $BEFORE_FILE | cut -d" " -f1)
$VIM_CMD $AFTER_FILE \
"-X" \
"--noplugin" \
"-e" \
"-s" \
"-C" \
"-c %s/\s\+$//" \
"-c %s/^\s\+//" \
"-c g/^$/d" \
"-c g/^#/d" \
"-c wq"
AFTER_LINE_NUMBER=$(wc -l $AFTER_FILE | cut -d" " -f1)
if [ $BEFORE_LINE_NUMBER == $AFTER_LINE_NUMBER ]; then
exec 6<$BEFORE_FILE
while read -r AFTER_LINE; do
read -r BEFORE_LINE <&6
if [ ! -e $BEFORE_LINE ]; then
if [ "true" != $FLAG_QUIET ]; then
echo "Source file \"$BEFORE_LINE\" does not exist in current directory."
echo "Omit current file!"
fi
continue
fi
if [ -e $AFTER_LINE ]; then
if [ "true" != $FLAG_QUIET ]; then
echo "Target file \"$AFTER_LINE\" already exists in current directory."
fi
if [ "false" == $FLAG_FORCE ]; then
if [ "true" != $FLAG_QUIET ]; then
echo "No action is performed for \"$BEFORE_LINE\"!"
fi
continue
else
if [ "true" != $FLAG_QUIET ]; then
echo "Target file \"$AFTER_LINE\" is overwritten by \"$BEFORE_LINE\"!"
fi
fi
fi
if [ "true" == $FLAG_COPY ]; then
$CP_CMD $OPTION_VERBOSE $OPTION_FORCE $BEFORE_LINE $AFTER_LINE
else
$MV_CMD $OPTION_VERBOSE $OPTION_FORCE $BEFORE_LINE $AFTER_LINE
fi
done <$AFTER_FILE
else
echo "Line mismatching in files. Abort!"
exit 1
fi
if [ "true" != $FLAG_QUIET ]; then
echo "Removing temporary files ..."
fi
$RM_CMD $OPTION_VERBOSE $AFTER_FILE $BEFORE_FILE
if [ "true" != $FLAG_QUIET ]; then
echo "All set !"
fi
exit 0