cute bash functions

This commit is contained in:
Christoph J. Scherr 2025-01-29 16:23:50 +01:00
parent 0d9a365ddc
commit d644581eb2
Signed by: PlexSheep
GPG Key ID: 9EB784BB202BB7BB
1 changed files with 122 additions and 2 deletions

124
.zshrc
View File

@ -1,6 +1,7 @@
### ENVVARS
# zmodload zsh/zprof # for profiling, also uncomment last line
source "$HOME/.zprofile"
source "$HOME/.config/user-dirs.dirs"
export TIMEFMT=$'\nreal\t%E\nuser\t%U\nsys\t%S\ncpu\t%P'
@ -18,6 +19,7 @@ export VISUAL=$(which nvim)
### Aliases
alias e="eza -la"
alias l="lsd -lah --date \"$DATEFMT\" --hyperlink=auto"
alias ll="lsd -lh --date \"$DATEFMT\" --hyperlink=auto"
alias la="lsd -a --date \"$DATEFMT\" --hyperlink=auto"
@ -50,7 +52,9 @@ alias bell="tput bel"
alias fd=fdfind
alias nobak="touch .nobackup"
alias securerm="shred -zun 100"
# alias shutdown="confirm shutdown"
alias mkpatch="diff -Naru"
alias shrug="echo -ne ¯\\\_(ツ)_/¯"
alias bak=backup
### Functions
function random() {
@ -128,6 +132,123 @@ function confirm-important() {
echo "aborting"
fi
}
function writepatch() {
target=$1
fname=$(basename $target)
mkdir -p a b
cp -r $target a
cp -r $target b
nvim b/$fname
diff -Naru --color='auto' a/$fname b/$fname
}
backup() {
local compress=0
local src=""
# Parse options
while getopts "z" opt; do
case $opt in
z) compress=1 ;;
*) echo "Usage: backup [-z] file_or_dir"; return 1 ;;
esac
done
shift $((OPTIND-1))
# Get source file/dir after option processing
src="$1"
if [ -z "$src" ]; then
echo "Error: no source specified"
return 1
fi
# Check if source exists
if [ ! -e "$src" ]; then
echo "Error: $src does not exist"
return 1
fi
# Create backup based on type and options
if [ $compress -eq 1 ]; then
tar -I zstd -cf "${src}.tar.zstd" "$src"
elif [ -d "$src" ]; then
cp -r "$src" "${src}.bak.d"
else
cp "$src" "${src}.bak"
fi
}
restore() {
local remove=0
# Parse options
while getopts "r" opt; do
case $opt in
r) remove=1 ;;
*) echo "Usage: restore [-r] backup_file"; return 1 ;;
esac
done
shift $((OPTIND-1))
# Check if argument was provided
local src="$1"
if [ -z "$src" ]; then
echo "Error: no backup file specified"
echo "Usage: restore [-r] backup_file"
return 1
fi
# Check if source exists
if [ ! -e "$src" ]; then
echo "Error: $src does not exist"
return 1
fi
# Function to handle overwrite confirmation
confirm_overwrite() {
echo -n "$1 already exists. Overwrite? [y/N] "
read answer
[[ $answer =~ ^[Yy] ]]
return $?
}
# Determine backup type and restore accordingly
if [[ "$src" == *.tar.zstd ]]; then
local dest="${src%.tar.zstd}"
if [ -e "$dest" ]; then
if ! confirm_overwrite "$dest"; then
echo "Restore cancelled"
return 1
fi
rm -rf "$dest"
fi
tar -I zstd -xf "$src"
[ $remove -eq 1 ] && rm "$src"
elif [[ "$src" == *.bak.d ]]; then
local dest="${src%.bak.d}"
if [ -e "$dest" ]; then
if ! confirm_overwrite "$dest"; then
echo "Restore cancelled"
return 1
fi
rm -rf "$dest"
fi
cp -r "$src" "$dest"
[ $remove -eq 1 ] && rm -rf "$src"
elif [[ "$src" == *.bak ]]; then
local dest="${src%.bak}"
if [ -e "$dest" ]; then
if ! confirm_overwrite "$dest"; then
echo "Restore cancelled"
return 1
fi
rm -f "$dest"
fi
cp "$src" "$dest"
[ $remove -eq 1 ] && rm "$src"
else
echo "Error: $src is not a recognized backup format"
return 1
fi
}
### ---- zsh options -------------------------------------
@ -304,7 +425,6 @@ function load_nvm() {
# gpg is cool actually
# crypto stuff with my gpg keys
export IDENTITY="Christoph J. Scherr <software@cscherr.de>"
export KEY_TYPE=ed25519
export KEYID=0E777B31ACC5B69B7096C050A466E5C5D6792EE9
gpgencrypt() {