-
Notifications
You must be signed in to change notification settings - Fork 0
/
.bash_functions
261 lines (227 loc) · 6.58 KB
/
.bash_functions
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
# PS1
function prompt {
local exitcode="$?"
# local black="\[\e[0;30m\]"
# local red="\[\e[0;31m\]"
# local green="\[\e[0;32m\]"
# local yellow="\[\e[0;33m\]"
# local blue="\[\e[0;34m\]"
# local purple="\[\e[0;35m\]"
# local cyan="\[\e[0;36m\]"
# local white="\[\e[0;37m\]"
# local orange="\[\e[0;91m\]"
#
# local bold_black="\[\e[30;1m\]"
local bold_red="\[\e[31;1m\]"
# local bold_green="\[\e[32;1m\]"
# local bold_yellow="\[\e[33;1m\]"
# local bold_blue="\[\e[34;1m\]"
# local bold_purple="\[\e[35;1m\]"
# local bold_cyan="\[\e[36;1m\]"
local bold_white="\[\e[37;1m\]"
# local bold_orange="\[\e[91;1m\]"
#
# local underline_black="\[\e[30;4m\]"
# local underline_red="\[\e[31;4m\]"
# local underline_green="\[\e[32;4m\]"
# local underline_yellow="\[\e[33;4m\]"
# local underline_blue="\[\e[34;4m\]"
# local underline_purple="\[\e[35;4m\]"
# local underline_cyan="\[\e[36;4m\]"
# local underline_white="\[\e[37;4m\]"
# local underline_orange="\[\e[91;4m\]"
#
# local background_black="\[\e[40m\]"
# local background_red="\[\e[41m\]"
# local background_green="\[\e[42m\]"
# local background_yellow="\[\e[43m\]"
# local background_blue="\[\e[44m\]"
# local background_purple="\[\e[45m\]"
# local background_cyan="\[\e[46m\]"
# local background_white="\[\e[47;1m\]"
# local background_orange="\[\e[101m\]"
local normal="\[\e[0m\]"
local reset_color="\[\e[39m\]"
local TITLEBAR
local ps_color
local ps_exitcode
case "$TERM" in
xterm*|rxvt-*|st-*|alacritty|screen)
TITLEBAR="\[\033]0;\w\007\]"
;;
*)
TITLEBAR=""
;;
esac
ps_color="$bold_white"
ps_exitcode=""
# 148 = ^Z 130 = ^C
if [[ "$exitcode" != "0" ]] && [[ "$exitcode" != "148" ]] && [[ "$exitcode" != "130" ]]; then
ps_color="$bold_red"
ps_exitcode="[\$?]"
fi
if [[ "$UID" -eq 0 ]] || [[ "${LANG: -6}" != ".UTF-8" ]]; then
export PS1="${TITLEBAR}${ps_color}[\w]\\\$${normal} "
else
PS1=""
#if command -v __git_ps1 >/dev/null 2>&1; then
# __git_ps1 "" "" "%s"
#fi
PS1="${TITLEBAR}${ps_color}┌─${ps_exitcode}[\w]${SSH_CLIENT:+" [ssh: $HOSTNAME]"}${PS1:+" [${PS1}${ps_color}]"}${normal}
${ps_color}└─[\\\$]${normal} "
PS2="└─[\\\$] "
export PS1 PS2
fi
}
# Push cd'ed directories into stack
function cd {
local dir _dirstack d i n found is_home
dir="$1"
if [[ "$dir" =~ ^\+([0-9]+)$ ]]; then
n="${BASH_REMATCH[1]}"
if [[ "$n" == "0" ]] || (( "${#DIRSTACK[@]}" <= "1" )) || (( "$n" >= "${#DIRSTACK[@]}" )); then
return 0
fi
dir="${DIRSTACK[$n]}"
popd -n "+$n" >/dev/null
elif [[ "$dir" =~ ^-$ ]]; then
if (( "${#DIRSTACK[@]}" <= "1" )); then
return 0
fi
dir="${DIRSTACK[1]}"
popd -n +1 >/dev/null
else
is_home=0
if [[ ! "$dir" ]]; then
dir="$HOME"
is_home=1
fi
if [[ -d "$dir" ]]; then
:
elif [[ -f "$dir" ]]; then
echo >&2 "bash: cd: $dir: Not a directory"
return 1
else
echo >&2 "bash: cd: $dir: No such file or directory"
return 1
fi
#if (( $is_home )) && (( "${#DIRSTACK[@]}" <= "1" )); then
# return 0
#fi
while [[ "$dir" =~ (.*)/$ ]]; do
dir="${BASH_REMATCH[1]}"
done
dir=$(builtin cd "$dir" && pwd)
if [[ "$dir" == "${DIRSTACK[0]}" ]]; then
return 0
fi
_dirstack=("${DIRSTACK[@]}")
i=1
while (( "$i" < "${#_dirstack[@]}" )); do
popd -n >/dev/null
i=$(( $i + 1 ))
done
i=$(( "${#_dirstack[@]}" - 1 ))
while (( "$i" > 0 )); do
if [[ "${_dirstack[$i]}" != "$dir" ]]; then
pushd -n "${_dirstack[$i]}" >/dev/null
fi
i=$(( $i - 1 ))
done
fi
if (( "${#DIRSTACK[@]}" >= "10" )); then
popd -n +9 >/dev/null
fi
pushd "$dir" >/dev/null 2>&1
if [[ -d "$dir" ]]; then
:
elif [[ -f "$dir" ]]; then
echo >&2 "bash: cd: $dir: Not a directory"
return 1
else
echo >&2 "bash: cd: $dir: No such file or directory"
return 1
fi
}
# ls recursively
function lr {
if [[ -n "$1" ]]; then
find "$1" -mindepth 1 -exec ls --color=auto -d {} +
else
find . -mindepth 1 -exec ls --color=auto -d {} +
fi
}
function getcrx {
echo 'https://clients2.google.com/service/update2/crx?response=redirect&acceptformat=crx2,crx3&prodversion=85&x=id%3D'"$1"'%26uc'
}
(
if [[ -f /etc/os-release ]]; then
. /etc/os-release
[[ $NAME == Slackware ]] && exit 0 || exit 1
else
exit 1
fi
) && {
function package_name {
local NAME="${1##*/}"
NAME="${NAME%-*}"
NAME="${NAME%-*}"
NAME="${NAME%-*}"
echo "$NAME"
}
}
function homestead {
( cd ~/Homestead && vagrant $* )
}
function urlencode {
# urlencode <string>
old_lc_collate=$LC_COLLATE
LC_COLLATE=C
local length="${#1}"
for (( i = 0; i < length; i++ )); do
local c="${1:i:1}"
case $c in
[a-zA-Z0-9.~_-]) printf "$c" ;;
*) printf '%%%02X' "'$c" ;;
esac
done
printf '\n'
LC_COLLATE=$old_lc_collate
}
function urldecode {
# urldecode <string>
local url_encoded="${1//+/ }"
printf '%b\n' "${url_encoded//%/\\x}"
}
function ypath {
printf "$(realpath -s "$1")" | xclip -sel c
}
function tmux-kill {
for i in $(tmux ls -F '#S'); do
tmux kill-session -t $i
done
}
# Usage: echo -n 'λ' | unicode-hex
function unicode-hex {
printf '%s' '\u'
iconv -f UTF8 -t ISO-10646 | xxd -s 2 -ps
}
function e {
local TMP;
if [[ "$1" == "-" ]]; then
TMP="$(mktemp /tmp/emacsstdinXXX)";
cat >"$TMP";
emacsclient -nw --eval "(let ((b (create-file-buffer \"*stdin*\"))) (switch-to-buffer b) (insert-file-contents \"${TMP}\") (delete-file \"${TMP}\"))"
else
emacsclient -nw --alternate-editor='emacs -nw' "$@"
fi;
}
function title {
if [[ "$(xprop WM_NAME)" =~ [^\"]*\"([^\"]*)\" ]]; then
echo "${BASH_REMATCH[1]}"
fi
}
# vim:ft=sh
# Local Variables:
# mode: Shell-script
# End: