-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdumBash.sh
142 lines (122 loc) · 3.83 KB
/
dumBash.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
#!/usr/bin/env bash
# vim: foldmethod=marker
# Configuration
COLORIZE="true"
ASK_REPLACE="true"
# Persian Letters Array
PERSIAN_LETTERS=(
'ش' 'ذ' 'ز' 'ی' 'ث' 'ب' 'ل' 'ا' 'ه' 'ت' 'ن' 'م' 'پ' 'د' 'خ' 'ح' 'ض' 'ق' 'س' 'ف' 'ع' 'ر' 'ص' 'ط' 'غ' 'ظ'
'ؤ' '\' 'ژ' 'ي' 'ٍ' 'إ' 'أ' 'آ' 'ّ' 'ة' '»' '«' 'ء' 'ٔ' ']' '[' 'ْ' 'ً' 'ئ' 'ُ' 'َ' 'ٰ' 'ٌ' 'ٓ' 'ِ\' 'ك'
'' '۱' '۲' '۳' '۴' '۵' '۶' '۷' '۸' '۹' '۰'
'÷' '\!' '٬' '٫' '﷼' '٪' '×' '،'
'ج' 'چ' 'ک' 'گ' 'و' '؛'
)
# English Letters Array
ENGLISH_LETTERS=(
'a' 'b' 'c' 'd' 'e' 'f' 'g' 'h' 'i' 'j' 'k' 'l' 'm' 'n' 'o' 'p' 'q' 'r' 's' 't' 'u' 'v' 'w' 'x' 'y' 'z'
'A' 'B' 'C' 'D' 'E' 'F' 'G' 'H' 'I' 'J' 'K' 'L' 'M' 'N' 'O' 'P' 'Q' 'R' 'S' 'T' 'U' 'V' 'W' 'X' 'Y' 'Z'
'`' '1' '2' '3' '4' '5' '6' '7' '8' '9' '0'
'~' '\!' '@' '\#' '\$' '\%' '\^' '\&'
'[' ']' ';' "'" ',' '"'
)
# Return texts in a new way
function logger () {
# Colors
WHITE="\e[0;97m"
BLUE="\e[0;94m"
YELLOW="\e[0;93m"
GREEN="\e[0;92m"
RED="\e[0;91m"
RESET="\e[0m"
# Inputs
COLOR="$1"
INPUT="$2"
PREFX="$3"
# Set null colors to white
if [[ -z $COLOR || $COLORIZE != "true" ]]; then
COLOR="WHITE"
fi
# Customizable "No new line"
if [[ $PREFX == "nobreak" ]]; then
ADDON="-en"
else
ADDON="-e"
fi
# Return
if [[ $BASH ]]; then
echo $ADDON "${!COLOR}$INPUT${RESET}"
elif [[ $ZSH_NAME ]]; then
echo $ADDON "${(P)COLOR}$INPUT${RESET}"
fi
}
# Handle command not found
function command_not_found_handler () {
# Copy `input` to `new` variable
NEW=$*
# Replace $PersianLetters with $EnglishLetters
ARRLEN=${#PERSIAN_LETTERS[@]}
for ((i = 0 ; i < $ARRLEN ; i++)); do
LETTER_FA="${PERSIAN_LETTERS[$i]}"
LETTER_EN=${ENGLISH_LETTERS[$i]}
NEW=${NEW//["$LETTER_FA"]/$LETTER_EN}
done
# Check the difference between old and new input
DIFF_CHECK=$(diff <(printf "%s" "$*" ) <(printf "%s" "$NEW"))
# DIFF_CHECK=$(printf "%s %s" "$*" "$NEW" | diff)
# Check if there was any replaces in string
if [[ -z $DIFF_CHECK ]]; then
logger "RED" "bash: $*: command not found"
return 1
else
# Check if we need to ask to replace command
if [[ $ASK_REPLACE == "true" ]]; then
# logger "red" "bash: $*: command not found, " "nobreak"
logger "BLUE" "Did you mean ${NEW}? [y/N] " "nobreak"
read ASK
case "${ASK}" in
[Yy][Ee][Ss]|Y|y|[غِ][ثٍ][سئ]|[غِ][سئ]|ص|ب|د|صحیح|بله|درست|یس|غ|ِ)
logger "GREEN" "Running: " "nobreak"
logger "" "${NEW}"
if [ "${NEW}" == "exit" ]; then
TEMP_VAR="$(ps -p $(ps -p $$ -o ppid=))"
TEMP_VAR="$(printf "%s" "${TEMP_VAR}" | awk 'NR==2 {print $1}')"
kill -s TERM "${TEMP_VAR}"
fi
eval $NEW
if [ "$?" -ne 0 ];then
return 1
fi
;;
'')
logger "GREEN" "Running: " "nobreak"
logger "" "${NEW}"
if [ "${NEW}" == "exit" ]; then
TEMP_VAR="$(ps -p $(ps -p $$ -o ppid=))"
TEMP_VAR="$(printf "%s" "${TEMP_VAR}" | awk 'NR==2 {print $1}')"
kill -s TERM "${TEMP_VAR}"
fi
eval $NEW
if [ "$?" -ne 0 ]; then
return 1
fi
;;
[Nn][Oo]|N|n)
return 1
;;
*)
return 1
;;
esac
else
logger "YELLOW" "$* -> $NEW"
logger "GREEN" "Running: " "nobreak"
logger "" "${NEW}..."
eval $NEW
exit 1
fi
fi
}
# Handle command not found - bash function
function command_not_found_handle () {
command_not_found_handler $*
}