forked from OpenGenus/cosmos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
filename_formatter.sh
147 lines (130 loc) · 3.79 KB
/
filename_formatter.sh
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
# What is this?
# auto convert files' name
#
# e.g.,
# 1)
# dir="aaa/bbb/ccc/ddd.cpp"
# convert:
# ddd.cpp => DDD.cpp
# ddd.cpp => Ddd.cpp
#
# 2)
# dir="multi/path/sort/bubble_sort/BuBble-_-sort.cpp"
# convert:
# BuBble-_-sort.cpp => bubble_sort.cpp (using directory name)
# BuBble-_-sort.cpp => Bubble_Sort.cpp
# BuBble-_-sort.cpp => BubbleSort.cpp
# BuBble-_-sort.cpp => Bubble-Sort.cpp
# ,etc.
#
# How to use?
# 1. copy this script to where your directory contains files
# 2. exec
YELLOW='\033[1;33m'
NC='\033[0m'
echo "\n${YELLOW}- Warnning: if target-file-name is exist, will be override !!! ${NC}-\n"
# define
use_directory_name=0
use_type_name=1
use_default_case=0
use_lower_case=1
use_upper_case=2
use_ignore_case=3
use_default_line=0
use_under_line=1
use_dash_line=2
use_ignore_line=3
re='^[0-1]+$'
echo "Please type your choice to convert files' name ( without '.' and 'extension' )"
echo "0) Use directory name"
echo "1) Use type name"
while [ 1 ]
do
read choice
if [[ $choice =~ $re ]]; then
break
fi
echo "wrong input"
done
if [ $choice -eq $use_type_name ]; then
read name
else
name="${PWD##*/}"
fi
re='^[0-3]+$'
echo "Please type your choice to convert (alpha)"
echo "0) Use default"
echo "1) Convert upper case to lower case (default)"
echo "2) Convert lower case to upper case"
echo "3) Not convert"
while [ 1 ]
do
read alpha_choice
if [[ $alpha_choice =~ $re ]]; then
break
fi
echo "Wrong input"
done
if [ $alpha_choice -eq $use_default_case ]|| [ $alpha_choice -eq $use_lower_case ]; then
name=`echo $name | tr [:upper:] [:lower:]`
elif [ $alpha_choice -eq $use_upper_case ]; then
name=`echo $name | tr [:lower:] [:upper:]`
fi
re='^[0-3]+$'
echo "Please type your choice to convert ('-' and '_')"
echo "0) Use default"
echo "1) Convert '-' and '_' to '_' (default)"
echo "2) Convert '-' and '_' to '-'"
echo "3) Not convert"
while [ 1 ]
do
read line_choice
if [[ $line_choice =~ $re ]]; then
break
fi
echo "Wrong input"
done
if [ $line_choice -eq $use_default_line ]||[ $line_choice -eq $use_under_line ]; then
name=`echo $name | tr - _`
elif [ $line_choice -eq $use_dash_line ]; then
name=`echo $name | tr _ -`
fi
# backup
date="backup_`date`"
`mkdir "$date"`
`cp *.* "$date"`
# match
for full_file_name in *.c *.cs *.cpp *.elm *.exs *.f *.go *.hs *.java *.jl *.js *.kt *.m *.php *.py *.rb *.rs *.sh *.swift *.ts
do
temp=(${full_file_name//\./ })
# check 1: escape file if has multi-layer extension
# check 2: escape if directory not has current extension file
if [ ${#temp[@]} -ge 3 ]||[[ ${temp[0]} == '*' ]]; then
continue
fi
file_name=${temp[0]}
file_ext=${temp[1]}
# convert file name with pattern
converted_file_name=$file_name
if [ $alpha_choice -eq $use_default_case ]||[ $alpha_choice -eq $use_lower_case ]; then
converted_file_name=`echo $converted_file_name | tr [:upper:] [:lower:]`
elif [ $alpha_choice -eq $use_upper_case ]; then
converted_file_name=`echo $converted_file_name | tr [:lower:] [:upper:]`
fi
if [ $line_choice -eq $use_default_line ]||[ $line_choice -eq $use_under_line ]; then
converted_file_name=`echo $converted_file_name | tr - _`
elif [ $line_choice -eq $use_dash_line ]; then
converted_file_name=`echo $converted_file_name | tr _ -`
fi
# convert file
converted_file_name="$converted_file_name.$file_ext"
converted_target_file_name="$name.$file_ext"
# ignore line
converted_file_name=${converted_file_name//-}
converted_file_name=${converted_file_name//_}
converted_target_file_name=${converted_target_file_name//-}
converted_target_file_name=${converted_target_file_name//_}
if [[ ^$converted_file_name$ == ^$converted_target_file_name$ ]]; then
`git mv --force $full_file_name $name.$file_ext`
fi
done