-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path.bash_functions
405 lines (349 loc) · 12 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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
#!/bin/bash
# Plays a short notification sound
beep() {
play -q ~/dotfiles/sounds/beep.wav &>/dev/null
}
# Change directory and list everything inside it
cs() {
cd "$@" && ls
}
# Extract the IP address of a Docker container
dip() {
if command -v "docker" &>/dev/null; then
if [ -n "$1" ]; then
docker inspect "$1" --format '{{range .NetworkSettings.Networks }}{{ .IPAddress }}{{ end }}'
# || inspect this -> docker container ls --format "{{.ID}} {{.Image}}"|grep -i "$1"|awk '{print($1)}'
else
local container_ids="$(docker ps -q)"
if [ -n "$container_ids" ]; then
{
echo "Image Name IP" # Table heading
## And the containers' data
echo "${container_ids}" | xargs -n 1 docker inspect --format '{{ .Config.Image }} {{ .Name }} {{range .NetworkSettings.Networks }}{{ .IPAddress }}{{ end }}' | sed 's/ \// /'
} | column -t -s' '
else
echo "No running containers."
fi
fi
else
echo "Docker binary not found."
fi
}
# Extract archives - usage: extract <file>
# Taken from https://github.com/paulirish/dotfiles
function extract() {
if [ -f "$1" ] ; then
local filename=$(basename "$1")
local foldername="${filename%%.*}"
local fullpath=$(perl -e 'use Cwd "abs_path";print abs_path(shift)' "$1")
local didfolderexist=false
if [ -d "$foldername" ]; then
didfolderexist=true
read -p "$foldername already exists, do you want to overwrite it? (y/n) " -n 1
echo
if [[ $REPLY =~ ^[Nn]$ ]]; then
return
fi
fi
mkdir -p "$foldername" && cd "$foldername"
case $1 in
*.tar.bz2) tar xjf "$fullpath" ;;
*.tar.gz) tar xzf "$fullpath" ;;
*.tar.xz) tar Jxvf "$fullpath" ;;
*.tar.Z) tar xzf "$fullpath" ;;
*.tar) tar xf "$fullpath" ;;
*.taz) tar xzf "$fullpath" ;;
*.tb2) tar xjf "$fullpath" ;;
*.tbz) tar xjf "$fullpath" ;;
*.tbz2) tar xjf "$fullpath" ;;
*.tgz) tar xzf "$fullpath" ;;
*.txz) tar Jxvf "$fullpath" ;;
*.zip) unzip "$fullpath" ;;
*) echo "'$1' cannot be extracted via extract()" && cd .. && ! $didfolderexist && rm -r "$foldername" ;;
esac
else
echo "'$1' is not a valid file"
fi
}
# Git clone + cd
gcl() {
# shellcheck disable=SC2068,SC2164
git clone $@ && cd "$(basename "${2:-$1}" .git)"
}
# Git commit --amend --no-edit --no-verify with modified timestamp
gcanh() {
echo "This will modify the commit's date."
# "You can use expressions such as 'now', 'tomorrow' and specific dates/times (e.g. '7 Nov', '14:30')."
# "Relative values can be used as well (e.g. '+1 day', '+4 hours', '2 days ago', etc.)"
if [ -z "$1" ]; then
read -p "New date: "
if [ -z "${REPLY}" ]; then
echo "No input, aborting"
return
fi
else
REPLY="$*"
fi
FORMATTED_DATE=$(date -R --date="${REPLY}")
if [ "$?" -ne 0 ]; then
echo "Unexpected date format, aborting"
return
fi
export GIT_COMMITTER_DATE="${FORMATTED_DATE}"
git commit --amend --no-edit --no-verify --date="${FORMATTED_DATE}"
echo "Date set to ${FORMATTED_DATE}"
unset GIT_COMMITTER_DATE
}
# Git fetch new branch + checkout
gfb() {
git fetch origin $1:$1 && git checkout $1
}
# Git checkout + pull
gkp() {
git checkout $1 && git pull origin $1
}
# cd relative to home, because using `~/` is too much hassle
hcd() {
cd ~/$1
}
holidays() {
local countrycode=${1:-AR} # use AR by default
countrycode=${countrycode^^}
if [[ ${countrycode} == "UK" ]]; then
countrycode="GB_EN"
fi
local color_gray=$(echo -e "\e[2m")
local color_reset=$(echo -e "\e[0m")
# Define time period
local months_ahead=6
local cal_range=$(date -d "today" +%m/%Y)
for i in $(seq 1 ${months_ahead}); do
cal_range="${cal_range},$(date -d "today ${i} months" +%m/%Y)"
done
# Legal holidays only (use `-n` to include memorial days)
gcal -N --exclude-holiday-list-title --suppress-holiday-list-separator \
--date-format="DateBEGIN%1%D %<3#U%2: DateDayOfWeek%<3#K DateYear%>4#YDateEND" \
--suppress-calendar --disable-highlighting \
--cc-holidays="${countrycode}" ${cal_range} |
sed "s/(${countrycode})//" | # Remove country code from each line
# Format days ahead/behind
sed 's/ *= *\(.[0-9]*\) \(days\?\)/ (\1 \2)/' |
# Remove year and extra sign
sed 's/ DateYear[0-9]*//' |
# Sort items in a prettier? way
sed 's/\(.*\) \(.\) DateBEGIN\(.*\)\: DateDayOfWeek\(...\)DateEND\(.*\)/\3: \1 [\4] \5/' |
sed "s/\(.*\[\(Sat\|Sun\)\].*\)/${color_gray}\1${color_reset}/"
}
# Return a fragment of lorem ipsum
lipsum() {
lipsum_text="Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
if command -v xclip &>/dev/null; then
echo -n ${lipsum_text}|xclip -selection clip
else
echo ${lipsum_text}
fi
}
# List paths and friendly name of connected USB devices
# via https://unix.stackexchange.com/a/144735/95310
lsusbdev() {
for sysdevpath in $(find /sys/bus/usb/devices/usb*/ -name dev); do
(
syspath="${sysdevpath%/dev}"
devname="$(udevadm info -q name -p $syspath)"
[[ "$devname" == "bus/"* ]] && exit
eval "$(udevadm info -q property --export -p $syspath)"
[[ -z "$ID_SERIAL" ]] && exit
echo "/dev/$devname - $ID_SERIAL"
)
done
}
# Make directory and enter immediately
mkd() {
[ -n "$*" ] && mkdir -p "$@" && cd "$_"
}
# Make a directory with a YYYYMMDD timestamp and enter immediately
mkt() {
NEW_DIR="$(date "+%Y%m%d")"
if [ -n "$*" ]; then
NEW_DIR="${NEW_DIR} - $*"
fi
mkdir "${NEW_DIR}" && cd "${NEW_DIR}"
}
# Get your public IP + local one
myip() {
local ip_local=$(ip -o route get to 1.1.1.1 | sed -n 's/.*src \([0-9.]\+\).*/\1/p')
echo "Local IP: ${ip_local}"
local ip_external=$(curl --silent 'http://checkip.amazonaws.com/' 2>/dev/null)
echo "External IP: ${ip_external:-???}"
if command -v "nmcli" &>/dev/null; then
IFS=% read vpn_name vpn_device < <(nmcli -t -f NAME,TYPE,DEVICE c show --active|grep ":vpn"|cut -d ':' -f1,3 --output-delimiter '%')
if [ -n "${vpn_name}" ]; then
echo -n "VPN: ${vpn_name} "
if ifconfig "${vpn_device}"|grep -q -E '(00-){15}00'; then
echo '(active)'
else
echo '(inactive)'
fi
fi
fi
}
# Prints a beatiful list of recent branches of the current repository
# Arguments: [max_number_of_results [show_item_numbers]]
print_recent_branches() {
# ToDo: implement branch selector
# ToDo: drown this function in holy water and ask for absolution
# ToDo: verify the console is 256-color capable
local maxcount=${1:-10}
local refcount=0
local re_has_number=".*[0-9].*"
read current_branch < <(git rev-parse --abbrev-ref HEAD)
read my_name < <(git config --get user.name)
read console_width < <(tput cols)
##########
# Colors #
##########
local bold="\e[1m"
local color_reset="\e[0m"
local bg="\e[48;5;"
local fg="\e[38;5;"
local color_branch_fg="254m"
local color_branch_bg="27m"
local color_timestamp_fg="111m"
local color_author_fg="238m"
local color_author_me="214m"
local color_subject_fg="250m"
echo
while read ref; do
if [[ "$ref" == "$current_branch" ]]; then
continue
else
(( refcount++ ))
IFS="§" read -a log_line < <(git log -n1 $ref --pretty=format:"%cr§%an§%s")
# [0] relative time
# [1] author name
# [2] subject (short commit description)
local re_subject="^$ref (.*)"
# RE to check if subject starts with $ref
if [[ "${log_line[2]}" =~ $re_subject ]]; then
log_line[2]="${BASH_REMATCH[1]}"
# Check if $ref contains first word of subject
# only if first word contains a number (issue/ticket)
elif [[ ${log_line[2]%% *} =~ $re_has_number && "$ref" == *"${log_line[2]%% *}"* ]]; then
log_line[2]="${log_line[2]#* }"
fi
# Trim log line if necessary
local available_width=$(($console_width - ${#ref} - 6))
if [ $available_width -gt 0 ] && [ ${#log_line[2]} -gt $available_width ]; then
log_line[2]="${log_line[2]::$(($available_width - 1))}…"
fi
echo -ne " ${fg}${color_branch_bg}${bg}${color_branch_bg}${fg}${color_branch_fg}"
echo -ne "${bg}${color_branch_bg}${bold}${fg}${color_branch_fg}"
echo -ne "$ref${color_reset}"
echo -ne "${fg}${color_branch_bg}${fg}${color_subject_fg} "
echo -ne "${log_line[2]}\n "
#printf "%${#ref}s" ""
#echo -ne " "
echo -ne "${fg}${color_timestamp_fg}${log_line[0]}"
echo -ne "${fg}235m - "
echo -ne "${fg}${color_author_fg}${log_line[1]} "
# Uncomment this to show a shiny star near your name!
# if [[ "${log_line[1]}" == "$my_name" ]]; then
# echo -ne "${fg}${color_author_me} "
# fi
echo -ne "${color_reset}\n\n"
[ $refcount -ge $maxcount ] && break
fi
done < <(git for-each-ref --sort=-committerdate --format="%(refname:short)" refs/heads/)
echo -ne ${color_reset}
}
# Change ownership to current user
pwn() {
sudo chown "$USER":"$USER" $@
}
# Search for text within the current directory
qt() {
grep -ir --color=always "$*" . | less -RX
# │└─ search all files under each directory, recursively
# └─ ignore case
}
# Sets the background color to $SSH_TERM_BG when connected through SSH interactively
reset_term_bg_color() {
if [ -n "${SSH_CONNECTION}" ] && [[ $- == *i* ]] && [ -n "${SSH_TERM_BG}" ]; then
[ -n "${TMUX}" ] && /usr/bin/tmux select-pane -P "bg=#${SSH_TERM_BG}" || echo -e "\033]11;#${SSH_TERM_BG}\a"
fi
}
# SSH breaks my terminal colors, so I reset them using this
# ToDo: improve this by just doing a reset of colors
ssh() {
/usr/bin/ssh $@
source ~/.bash_prompt
}
# Define a title on the current terminal
title() {
echo -en "\033]0;$@\a"
}
# Overrides tmux to restore background color after using it through SSH
tmux() {
/usr/bin/tmux -2 $@ && reset_term_bg_color
}
# Go up $1 directories (and eventually enter $2)
# https://stackoverflow.com/a/34090540/854076
up() {
local p=
local i=${1:-1}
while (( i-- )); do
p+="../"
done
cd "$p$2" && pwd
}
# Opens a simple HTTP server in the current folder
# Args: [ws_port=8000]
webserver() {
if ! command -v python &>/dev/null; then
echo "Python is not installed."
exit 1
fi
local ws_port=${1:-8000}
local py_version=$(python --version|grep -oP '(?<=Python ).')
if [ "${py_version}" -eq 2 ]; then
python -m SimpleHTTPServer $ws_port
else
python -m http.server $ws_port
fi
}
# Reboot to Windows™
win() {
trap 'return' SIGINT
# Ask for sudo now, before the countdown
sudo -v &>/dev/null
# Countdown
for i in {4..1}; do
echo -ne "\rRebooting to Windows in $i seconds, press Ctrl+C to abort"
# Wait for any key press
read -t 1 -n 1 -s
# If a key is pressed, skip the countdown
if [ $? -eq 0 ]; then
break
fi
done
# Go to beginning of line and clear the line
echo -e "\r$(tput el)"
# Try altering the UEFI boot sequence; if not possible, change the GRUB default option
if command -v efibootmgr &>/dev/null; then
sudo efibootmgr -n "$(efibootmgr -v|grep -oP '(?<=Boot)[0-9]{4}(?=.+Win)')" &>/dev/null
elif command -v grub-reboot &>/dev/null; then
sudo grub-reboot "$(awk -F\' '/Win/ {print $2}' /boot/grub/grub.cfg)"
else
echo "No compatible tools were found, impossible to alter boot sequence."
return 1
fi
# Reboot if the next boot is successfully pointed to Windows
if [ $? -eq 0 ]; then
echo "Rebooting to Windows now!"
sleep 0.5
reboot
else
echo "Error preparing for reboot, aborting."
fi
}
alias reboot-to-windows='win'