.dotfiles/zsh/scripts

143 lines
3.7 KiB
Plaintext
Raw Normal View History

2022-02-10 20:11:56 +01:00
#--------------------------------
# filename: /zsh/scripts
#--------------------------------
# Search for string in all files in pwd
function f() {
grep -rnw ./ -e "$1"
}
# mkdir and cd
function mkcd() {
2022-11-13 19:19:00 +01:00
mkdir -p "$@" && cd "$_" || exit;
2022-02-10 20:11:56 +01:00
}
# "Fake" sha256sum
2022-11-13 19:19:00 +01:00
if [[ ! -x $(which sha256sum) ]]; then
function sha256sum() {
2022-11-13 19:19:00 +01:00
shasum -a256 "$1"
}
fi
2022-10-11 13:42:09 +02:00
# Tar.gz with GZIP or PIGZ
function targz() {
if [ -z "$1" ]; then
echo "usage: targz filename.tar.gz directory"
return
fi
2022-11-13 19:19:00 +01:00
if [[ ! -x $(which pigz) ]]; then
tar -czf "$1" "$2"
2022-10-11 13:42:09 +02:00
echo "gzip"
else
2022-11-13 19:19:00 +01:00
tar -c "$2" | pigz > "$1"
2022-10-11 13:42:09 +02:00
echo "pigz"
fi
}
2023-02-02 23:36:17 +01:00
# Export manpage as PDF
function manp() {
man -t $1 | pstopdf -o $TMPDIR/$1.pdf && open $TMPDIR/$1.pdf && sleep 1 && rm $TMPDIR/$1.pdf
}
2022-02-10 20:11:56 +01:00
# pushover notification
function pushover() {
2023-10-20 16:45:06 +02:00
curl -s \
--form-string "token=$pushover_token" \
--form-string "user=$pushover_user" \
--form-string "title=$HOST" \
--form-string "message=$1" \
https://api.pushover.net/1/messages.json
2022-02-10 20:11:56 +01:00
}
2024-07-16 15:07:33 +02:00
# png to jpg 95
2022-02-10 20:11:56 +01:00
function png2web() {
findpath=$1
2022-11-13 19:19:00 +01:00
: "${findpath:="."}"
find "$findpath" -name '*.png' | while read -r f ; do
2022-02-10 20:11:56 +01:00
dir=$(dirname "$f");
file=$(basename "$f");
name="${file%.*}";
2024-07-16 15:07:33 +02:00
magick "$f" pnm:- | cjpeg -progressive -optimize -quality 95 > "$dir/$name.jpg" </dev/null
2022-02-10 20:11:56 +01:00
rm -f "$f";
done
}
2022-02-10 21:29:04 +01:00
function jpeg1024() {
2024-07-16 15:07:33 +02:00
magick "$1" -resize '1024x1024\>' -background black -flatten TGA:- | cjpeg -targo -quality 90 -optimize -progressive -outfile "$1".jpg
2022-02-14 00:00:11 +01:00
}
2022-04-19 17:48:35 +02:00
# Make PDFs look like scanned
# src: https://gist.github.com/andyrbell/25c8632e15d17c83a54602f6acde2724?permalink_comment_id=3295405#gistcomment-3295405
function pdf-like-scanned () {
OUT=$(basename "$1" .pdf)
convert -density 150 "$1" -rotate "$([ $((RANDOM % 2)) -eq 1 ] && echo -)0.$(($RANDOM % 4 + 5))" \
-attenuate 0.4 +noise Multiplicative -attenuate 0.03 +noise Multiplicative -sharpen 0x1.0 \
-colorspace Gray "$OUT"_scanned.pdf
}
2022-06-27 22:00:43 +02:00
# Extract files
# src: https://github.com/mawalu/dotfiles/blob/master/zsh/.zsh/functions.zsh#L21
function extract () {
2022-11-13 19:19:00 +01:00
if [ -f "$1" ] ; then
2022-06-27 22:00:43 +02:00
case $1 in
2022-11-13 19:19:00 +01:00
*.tar.bz2) tar xjf "$1" ;;
*.tar.gz) tar xzf "$1" ;;
*.bz2) bunzip2 "$1" ;;
*.rar) unrar x "$1" ;;
*.gz) gunzip "$1" ;;
*.tar) tar xf "$1" ;;
*.tbz2) tar xjf "$1" ;;
*.tbz) tar xjf "$1" ;;
*.tgz) tar xzf "$1" ;;
*.zip) unzip "$1" ;;
*.Z) uncompress "$1" ;;
*.7z) 7z x "$1" ;;
*) echo "'$1' cannot be extracted via extract()" ;;
2023-10-20 16:45:06 +02:00
esac
2022-06-27 22:00:43 +02:00
else
echo "'$1' is not a valid file"
fi
}
2023-06-07 13:22:28 +02:00
2024-04-29 23:11:47 +02:00
# Download provided url to $HOME/Downloads
function dl() {
if [ -z "$1" ]; then
echo "usage: dl url (directory)"
return
fi
if [[ -x $(which aria2) ]]; then
aria2c --dir="$HOME/Downloads" --continue=true --max-connection-per-server=5 "$1"
else
(cd "$HOME/Downloads" && curl -O "$1")
fi
}
2023-06-07 13:22:28 +02:00
# Monitor IP/Host and report with Pushover
pnotify-online() {
local ip_address="$1"
echo "Monitoring $ip_address …"
2023-06-07 13:22:28 +02:00
while true; do
if ping -c 1 -W 1 "$ip_address" >/dev/null; then
2024-04-29 23:16:22 +02:00
echo -e "\07*Ding*"
2023-06-07 13:22:28 +02:00
echo "$1 is online now."
pushover "$1 is online now."
break
fi
done
}
pnotify-offline() {
local ip_address="$1"
echo "Monitoring $ip_address …"
2023-06-07 13:22:28 +02:00
while true; do
if ! ping -c 1 -W 1 "$ip_address" >/dev/null; then
2024-04-29 23:16:22 +02:00
echo -e "\07*Ding*"
2023-06-07 13:22:28 +02:00
echo "$1 is offline now."
pushover "$1 is offline now."
break
fi
done
}