-
Notifications
You must be signed in to change notification settings - Fork 28
/
commonrc
208 lines (173 loc) · 3.64 KB
/
commonrc
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
#! /usr/bin/env bash
# Github: https://github.com/Karmenzind/dotfiles-and-scripts
# Last Modified: 2024-01-18 16:22:14
aur_helper=yay
distro=$(cat /etc/os-release | grep '^ID=' | sed 's/ID=//')
bold='\033[1m'
origin='\033[0m'
black='\E[30;1m'
red='\E[31;1m'
green='\E[32;1m'
yellow='\E[33;1m'
blue='\E[34;1m'
magenta='\E[35;1m'
cyan='\E[36;1m'
white='\E[37;1m'
cut_off='--------------------------------------------'
# Color-echo
# $1: message
# $2: color
cecho() {
echo -e "${2:-${bold}}${1}"
tput sgr0 # Reset to normal.
}
echo_run() {
cecho "≫ $1" ${bold}${cyan}
}
put_error() {
cecho "$1" ${red}
}
echo_error() {
cecho "[✘] $1" ${red}
}
echo_wrong() {
cecho "[✘] $1" ${red}${bold}
}
echo_warn() {
# cecho "[⚠ Warn] $1" ${yellow}
cecho "[⚡] $1" ${yellow}
}
echo_ok() {
cecho "[✔] $1" ${bold}${green}
}
exit_with_msg() {
cecho "$1" ${bold}${red}
exit -1
}
pacman_conf=/etc/pacman.conf
no_root() {
if [[ $USER = 'root' ]]; then
echo_wrong "Do not use root."
exit -1
fi
}
# $1 prompt
put_cutoff() {
_line="\n${cut_off}\n"
cecho $_line $cyan
if [[ -n "$1" ]]; then
echo -e "$1"
cecho $_line $cyan
fi
}
do_install() {
if [[ $distro == arch ]]; then
sudo pacman -S --needed --noconfirm "$@"
else
if command -V apt-get >/dev/null; then
sudo apt-get install -y "$@"
else
cecho "[WARN] No available installer. Ignored installing $@"
return 1
fi
fi
}
is_wsl() {
test -f /proc/sys/fs/binfmt_misc/WSLInterop
}
apti() {
if command -v aptitute >/dev/null; then
sudo aptitude install -y "$@"
else
sudo apt-get install -y "$@"
fi
}
display_array() {
local tmp_array=($@)
local length=${#tmp_array[@]}
for ((i = 0; i < $length; i++)); do
echo "$i ${tmp_array[$i]}"
done
echo
}
# enhanced `read` - name a range for checking
# $1 input range, e.g. 123 Yn abcd (case insensitive)
# $2 variable's name
check_input() {
local _range=$1
local is_valid=no
local _default=${_range:0:1}
while [[ $is_valid = 'no' ]]; do
read -p "Input: " ans
[[ -z "$ans" ]] && ans=$_default
ans=$(echo $ans | tr '[A-Z]' '[a-z]')
if [[ "$_range" = *$ans* ]]; then
is_valid=yes
else
put_error "Valid answer: $_range (default=$_default):"
fi
done
[[ -n $2 ]] && read $2 <<<$ans
}
check_yn() {
check_input yn
if [[ $ans == 'y' ]]; then
return 0
else
return 1
fi
}
put_suspend() {
cecho "
Type Ctrl+C to exit
or type any key to continue" $cyan
read whatever
}
cmd_not_found() {
if command -V $1 >/dev/null; then
return 1
else
return 0
fi
}
# -----------------------------------------------------------------------------
# basic
# -----------------------------------------------------------------------------
enable_multilib() {
sudo cat >>/etc/pacman.conf <<EOF
[multilib]
Include = /etc/pacman.d/mirrorlist
EOF
}
check_multilib_support() {
echo 'Check multilib support ...'
if [[ -z "grep '^\[multilib\]' $pacman_conf" ]]; then
multilib_enabled=0
echo "Add multilib repo support? (Y/n)"
check_input yn
[[ $ans = 'y' ]] && enable_multilib && multilib_enabled=1
sudo pacman -Sy
else
multilib_enabled=1
fi
((multilib_enabled == 0)) && enable_multilib
echo "Support multilib: $multilib_enabled"
}
# -----------------------------------------------------------------------------
# basic
# -----------------------------------------------------------------------------
# $1 expr that will be passed to bc
# return 0 if nothing's wrong
valid_by_bc() {
local res
res=$(bc <<<"$1")
((res == 1)) || return 255
}
setup_pipx() {
do_install pipx
pipx ensurepath
}
pipx_install() {
sudo PIPX_HOME=/opt/pipx PIPX_BIN_DIR=/usr/local/bin PIPX_MAN_DIR=/usr/local/share/man pipx install "$@"
}
__loaded_commonrc=1