-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbashrc_extension
503 lines (422 loc) · 15.2 KB
/
bashrc_extension
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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
#!/bin/bash
#
# bashrc_extension - part of the Arch-Setup project
# Copyright (C) 2024-2025, JustScott, [email protected]
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
# Meant to be sourced by the $HOME/.bashrc file. This allows you to keep
# private information in your .bashrc, and non-private information in here.
# if in a tty
[[ -z "$DISPLAY" && -z "$WAYLAND_DISPLAY" ]] && {
#stty -a | grep "rows 24; columns 80;" &>/dev/null && {
# Default to smaller foot terminal sizing to prevent breakage
#stty rows 40 cols 171 &>/dev/null # river with foot
#stty rows 46 cols 171 &>/dev/null # dwm with st
#}
alias external_tty="stty rows 32 cols 135 &>/dev/null"
alias primary_tty="stty rows 46 cols 159 &>/dev/null"
#stty rows 46 cols 159 &>/dev/null # river with foot
# enable color
export TERM=xterm-256color
}
export PAGER="bat --paging=always --style=plain"
export EDITOR=nvim
alias e=$EDITOR
export CALCURSE_PAGER="bat --paging=always --style=plain --language=markdown"
alias grep='grep --color=auto'
alias ls='ls --color=auto'
alias gl='in_git_repo && git log --pretty=format:"%C(yellow)%h%Creset %C(green)%D%Creset %n %s%n"'
alias gla='in_git_repo && git log --pretty=format:"%C(yellow)%h%Creset %C(green)%D%Creset %n %s%n%n%b%n"'
alias nb='in_git_repo && git checkout $(git branch | tr -d " " | grep -v "*" | fzf)'
export HISTFILESIZE=10000
export HISTSIZE=10000
alias ehist='history -a;eval "$(hist_fzf)"'
alias hist="history -a;hist_fzf"
alias up='ping quad9.net -c 4'
alias newsboat='newsboat -c ~/.config/newsboat/cache.db'
alias dlynx="lynx -cfg=$HOME/.config/lynx/lynx.cfg"
alias sroot="sudo snapper -c root"
alias shome="sudo snapper -c home"
alias mf="man \$(ls /usr/share/man/man1 | awk -F. '{print \$1}' | fzf --reverse) 2>/dev/null"
alias service="ls /lib/systemd/system/ | fzf --reverse --preview 'systemctl status {}' --preview-window '70%'"
if which bemenu &>/dev/null; then
alias bemenu-run="bemenu-run --fn 'monospace [14]' -w"
alias bemenu="bemenu --fn 'monospace [14]' -w"
fi
if [[ -n "$WAYLAND_DISPLAY" ]]; then
alias 1monitor="wlr-randr --output eDP-1 --on --pos 0,0 --output DP-10 --off --output DP-9 --off"
alias 2monitors="wlr-randr --output eDP-1 --off --output DP-10 --on --pos 2256,0 --output DP-9 --on --pos 4176,0"
alias 3monitors="wlr-randr --output eDP-1 --on --pos 0,0 --output DP-10 --on --pos 2256,0 --output DP-9 --on --pos 4176,0"
if uname -r | grep "pinetab2" &>/dev/null
then
if wlr-randr | grep "HDMI-A-1" &>/dev/null; then
alias 1monitor="wlr-randr --output DSI-1 --off --output HDMI-A-1 --on --pos 0,0"
alias 2monitors="wlr-randr --output DSI-1 --on --transform 270 --pos 0,1280 --output HDMI-A-1 --on --pos 0,0"
alias 2monitors_other="wlr-randr --output DSI-1 --on --transform 270 --pos 0,1075 --output HDMI-A-1 --on --pos 1280,0"
fi
fi
elif which dwm &>/dev/null; then
main_monitor="eDP-1"
middle_monitor="DP-2-2"
right_monitor="DP-2-1"
home_monitor="DP-2-2"
alias 3monitors="set-monitors $main_monitor $middle_monitor $right_monitor"
alias 2monitors="set-monitors $middle_monitor $right_monitor;xrandr --output $main_monitor --off"
alias 1monitor="set-monitors $main_monitor;xrandr --output $middle_monitor --off --output $right_monitor --off --output $home_monitor --off"
alias 2home_monitors="set-monitors $main_monitor $home_monitor"
alias 1home_monitor="set-monitors $home_monitor; xrandr --output $main_monitor --off"
fi
# pacman/yay + fzf
if which pacman &>/dev/null; then
# Package list : list all installed packages
alias pl="pacman -Qq | fzf --preview 'pacman -Qi {}' --layout=reverse --preview-window '70%'"
# Package search (find) : list all packages ( 'ps' is taken by system utility )
alias pf="pacman -Slq | fzf --preview 'pacman -Si {}' --layout=reverse --preview-window '70%'"
fi
if which yay &>/dev/null; then
# Package list : list all installed packages
alias yl="yay -Qq | fzf --preview 'yay -Qi {}' --layout=reverse --preview-window '70%'"
# Package search (find) : list all packages
alias yf="yay -Slq | fzf --preview 'yay -Si {}' --layout=reverse --preview-window '70%'"
fi
# Saves any running VirtualMachines before shutting down
safe_shutdown() {
{ which calcurse || type calcurse; } &>/dev/null && {
[[ -f $HOME/.local/share/calcurse/.calcurse.pid ]] && {
echo "You need to quit calcurse first.">&2
return 1
}
}
if { which virsh || type virsh; } &>/dev/null
then
{ which vm || type vm; } &>/dev/null && {
vm save --all
}
fi
return 0
}
shutdown_wrapper() {
safe_shutdown || return 1
# Gives the user so many seconds to cancel the shutdown
for i in {5..1}; do
echo -ne "\rShutting Down In: $i"
sleep 1
done
echo -e "\n"
shutdown $@
}
alias shutdown=shutdown_wrapper
reboot_wrapper() {
safe_shutdown || return 1
# Gives the user so many seconds to cancel the reboot
echo -n -e "\nRebooting In"
for number in {5..1}
do
echo -n "...$number"
sleep 1
done
echo -e "\n"
reboot $@
}
alias reboot=reboot_wrapper
hist_fzf() {
# Removes duplicates using the hashmap
declare -A hist_hashmap
echo "$(tac $HOME/.bash_history | while IFS= read -r line
do
[[ -n "$line" ]] && {
[[ -v hist_hashmap["$line"] ]] || {
echo $line
hist_hashmap["$line"]=""
}
}
done | fzf --reverse)"
}
wttr() {
if [[ -n "$LOCATION" ]]
then
curl -s wttr.in/$LOCATION?u
else
echo "Must set the \$LOCATION variable first!"
return 1
fi
}
htmd() {
{ which html2text || type html2text; } &>/dev/null \
&& bat --language=md <(html2text <(curl -L --silent "$1")) \
|| exit 1
}
spell() {
word=$(echo $1 | tr '[:upper:]' '[:lower:]')
html=$(curl -L --silent "https://duckduckgo.com/?q=spell+$word&ia=answer")
echo $html | grep "appears to be spelled correctly" &>/dev/null && {
echo "Spelled Correctly"; return 0;
} || {
alternatives=$(echo $html | sed -n 's/.*Suggestions:<\/i>\(.*\)<\/a>.*/\1/p' | sed 's/<[^>]*>//g')
echo "Spelled wrong, possible alternative spellings: $alternatives" >&2
return 1
}
}
define() {
word=$(echo $1 | tr '[:upper:]' '[:lower:]')
spell "$word" >/dev/null && {
uppercase_word=$(echo $word | tr '[:lower:]' '[:upper:]' )
URL="https://www.merriam-webster.com/dictionary/$word"
definition=$(curl -L --silent "$URL" \
| grep --ignore-case "the meaning of $word is" | grep -o 'content=".*"' \
| sed "s/content=//; s/The meaning of $uppercase_word is //; s/. How to use $word in a sentence.//" \
| sed 's/"$//; s/"//' | sed 's/ Did you know?//')
[[ -n "$definition" ]] && {
echo "$definition"
return 0
} || {
echo "Spell right, but no definition found." >&2
return 1
}
}
return 1
}
# Easy daily journal stored in notes by date
j() {
months_directory=$HOME/notes/Writing/Journal/$(date +"%Y")/$(date +"%B")
mkdir -p $months_directory 2>/dev/null
$EDITOR $months_directory/$(date +"%d").md
}
blog() {
blog_directory=$HOME/notes/Writing/Blog
mkdir -p $blog_directory 2>/dev/null
lf $blog_directory || {
echo "Failed to open blog... is lf installed?"
(exit 1)
}
}
# sets wall paper
wp() {
new_wallpaper_file_path="$1"
wallpaper_file_path=$HOME/.wallpaper
if ! [[ -f "$new_wallpaper_file_path" ]]
then
echo "[ERROR] Specified wallpaper path doesn't exist"
return 1
fi
if [[ -n "$DISPLAY" && -z "$WAYLAND_DISPLAY" ]]
then
cp "$new_wallpaper_file_path" $wallpaper_file_path
feh --bg-scale $wallpaper_file_path
elif [[ -n "$WAYLAND_DISPLAY" ]]
then
cp "$new_wallpaper_file_path" $wallpaper_file_path
# Kill existing swaybg command
if [[ -f "/tmp/.swaybg_pid" ]];then
kill $(cat /tmp/.swaybg_pid)
rm /tmp/.swaybg_pid
fi
nohup swaybg --mode fill --image $wallpaper_file_path &>/dev/null & \
echo $! > /tmp/.swaybg_pid
else
echo "[ERROR] Need a graphical environment active to set wallpaper"
return 1
fi
}
wpf() {
[[ -z "$DISPLAY" && -z "$WAYLAND_DISPLAY" ]] && {
echo "[ERROR] Need a graphical environment active to set wallpaper"
return 1
}
WALLPAPER_DIRECTORY=$HOME/Pictures/Wallpapers
mkdir -p $WALLPAPER_DIRECTORY &>/dev/null
[[ -f "$HOME/.wallpaper" ]] && cp $HOME/.wallpaper $HOME/.old_wallpaper
if [[ -n "$WAYLAND_DISPLAY" ]]; then
new_wallpaper=$( \
find $WALLPAPER_DIRECTORY -type f \
| fzf --reverse\
--preview="swaybg --mode fill --image {}" \
--preview-window=0% \
)
elif which dwm &>/dev/null; then
new_wallpaper=$( \
find $WALLPAPER_DIRECTORY -type f \
| fzf --reverse\
--preview="feh --bg-scale {}" \
--preview-window=0% \
)
fi
if [[ -n "$new_wallpaper" ]]
then
wp $new_wallpaper
else
wp $HOME/.old_wallpaper
fi
}
play() {
search="$@"
if [[ ${#@} == 0 && -f $HOME/.play_history ]]
then
if { which fzf || type fzf; } &>/dev/null
then
search="$(cat $HOME/.play_history | fzf --reverse)"
else
echo "[ERROR] You need to pass a search term after play"
return 1
fi
fi
if { which ytfzf || type ytfzf; } &>/dev/null
then
if [[ -n "$search" ]]
then
# Avoid repeats
if ! grep "$search" $HOME/.play_history &>/dev/null
then
echo "$search" >> $HOME/.play_history
fi
ytfzf -Am "$search"
fi
else
echo -e "\n - [ERROR] ytfzf not installed - \n"
fi
}
presnap() {
description="$1"
configs=($(sudo snapper list-configs | awk 'NR>2 {print $1}'))
for config in ${configs[@]}
do
sudo snapper -c $config create -d "$description" -t pre
done
}
postsnap() {
description="$1"
configs=($(sudo snapper list-configs | awk 'NR>2 {print $1}'))
for config in ${configs[@]}
do
pre_number=$(sudo snapper -c $config list | grep "pre" | grep "$description" | awk '{print $1}')
sudo snapper -c $config create -d "$description" -t post --pre-number "$pre_number"
done
}
deletesnap() {
description="$1"
configs=($(sudo snapper list-configs | awk 'NR>2 {print $1}'))
for config in ${configs[@]}
do
pre_number=$(sudo snapper -c $config list | grep "pre" | grep "$description" | awk '{print $1}')
snap_number=$(sudo snapper -c $config list | grep "$pre_number" | grep "$description" | awk -F '|' '{print $1}')
sudo snapper -c $config delete $snap_number
done
}
batt() {
if ls /sys/class/power_supply/*/capacity &>/dev/null
then
for capacity_path in $(ls /sys/class/power_supply/*/capacity)
do
batt_path=$(dirname $(ls $capacity_path))
batt_name=$(basename $batt_path)
batt_percentage=$(cat $capacity_path)
[[ "$(cat $batt_path/status 2> /dev/null)" == "Charging" ]] \
&& charging="+" \
|| charging="-"
[[ $batt_percentage -gt 99 ]] && { batt_percentage=100; unset charging; }
echo "$(basename $batt_name): $charging$batt_percentage%"
done
else
echo "No Batteries Found">&2
return 1
fi
}
f() {
fzf_path=$(find . -type d -o -type f | fzf --reverse)
[[ -d $fzf_path ]] && lf $fzf_path
[[ -f $fzf_path ]] && nvim $fzf_path -c "cd $(dirname $fzf_path)"
}
p() {
pass_command=$1
copy=false
if [[ "$pass_command" == "c" ]]; then
copy=true
fi
ORIGINAL_PWD="$PWD"
[[ -n "$PASSWORD_STORE_DIR" ]] \
&& cd $PASSWORD_STORE_DIR \
|| cd $HOME/.password-store
fzf_password=$(find . -type f -name "*.gpg" | sed 's/^\.\///; s/\.gpg$//' | fzf --reverse)
cd "$ORIGINAL_PWD"
if [[ -n "$fzf_password" ]]
then
case $pass_command in
"")
pass show "$fzf_password"
return 0
;;
"c")
password=$(pass "$fzf_password")
;;
*)
pass $pass_command "$fzf_password"
return 0
;;
esac
fi
if $copy; then
if [[ -n "$WAYLAND_DISPLAY" ]] && echo $password | wl-copy; then
echo "Copied with wl-copy! Clearing in 20 seconds"
(sleep 20 && echo "" | wl-copy &) &>/dev/null
elif which xclip &>/dev/null && echo $password | xclip -selection clipboard; then
echo "Copied with xclip! Clearing in 20 seconds"
(sleep 20 && echo "" | xclip -selection clipboard &) &>/dev/null
else
echo "Failed to copy with wl-copy or xclip"
fi
else
[[ -n "$password" ]] && echo "$password"
fi
}
space() {
filesystem_type=$(stat -f -c "%T" /)
[[ "$filesystem_type" == "btrfs" ]] && {
size=$(btrfs filesystem usage / 2>/dev/null | grep "Device size:" | awk '{print $3}')
used=$(btrfs filesystem usage / 2>/dev/null | grep "Device allocated:" | awk '{print $3}')
available=$(btrfs filesystem usage / 2>/dev/null | grep "Device unallocated:" | awk '{print $3}')
printf " %-12s %-12s %-12s\n" "Size" "Used" "Avail"
printf "%-12s %-12s %-12s\n" "$size" "$used" "$available"
} || {
[[ "$filesystem_type" == "ext4" || "$filesystem_type" == "ext2/ext3" ]] \
&& df -h / \
|| echo "Unsupported filesystem type: '$filesystem_type'"
}
}
change_timezone() {
new_timezone=$(timedatectl list-timezones | fzf --reverse)
if [[ -n "$new_timezone" ]]; then
sudo ln -sf /usr/share/zoneinfo/$new_timezone /etc/localtime
sudo systemctl restart systemd-timesyncd
fi
}
# Check if currently in a git repo
in_git_repo() {
if git status &>/dev/null;
then
return 0
else
echo "Not in a git repository"
return 1
fi
}
# Change to branch, git log, then change back to original branch
glb() {
if in_git_repo;
then
original_branch="$(git branch | grep "*" | tr -d " *")"
new_branch="$(git branch | grep -v "*" | tr -d " " | fzf)"
git checkout $new_branch && gl
git checkout $original_branch
fi
}