.dotfiles/zsh/scripts

128 lines
3.3 KiB
Plaintext

#--------------------------------
# filename: /zsh/scripts
#--------------------------------
# Search for string in all files in pwd
function f() {
grep -rnw ./ -e "$1"
}
# mkdir and cd
function mkcd() {
mkdir -p "$@" && cd "$_" || exit;
}
# "Fake" sha256sum
if [[ ! -x $(which sha256sum) ]]; then
function sha256sum() {
shasum -a256 "$1"
}
fi
# Tar.gz with GZIP or PIGZ
function targz() {
if [ -z "$1" ]; then
echo "usage: targz filename.tar.gz directory"
return
fi
if [[ ! -x $(which pigz) ]]; then
tar -czf "$1" "$2"
echo "gzip"
else
tar -c "$2" | pigz > "$1"
echo "pigz"
fi
}
# Export manpage as PDF
function manp() {
man -t $1 | pstopdf -o $TMPDIR/$1.pdf && open $TMPDIR/$1.pdf && sleep 1 && rm $TMPDIR/$1.pdf
}
# pushover notification
function pushover() {
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
}
# png to jpg 85
function png2web() {
findpath=$1
: "${findpath:="."}"
find "$findpath" -name '*.png' | while read -r f ; do
dir=$(dirname "$f");
file=$(basename "$f");
name="${file%.*}";
magick convert "$f" pnm:- | cjpeg -progressive -quality 85 > "$dir/$name.jpg" </dev/null
rm -f "$f";
done
}
function jpeg1024() {
magick "$1" -resize '1024x1024\>' -background black -flatten TGA:- | cjpeg -targo -quality 75 -optimize -progressive -outfile "$1".jpg
}
# 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
}
# Extract files
# src: https://github.com/mawalu/dotfiles/blob/master/zsh/.zsh/functions.zsh#L21
function extract () {
if [ -f "$1" ] ; then
case $1 in
*.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()" ;;
esac
else
echo "'$1' is not a valid file"
fi
}
# Monitor IP/Host and report with Pushover
pnotify-online() {
local ip_address="$1"
echo "Monitoring $ip_address …"
while true; do
if ping -c 1 -W 1 "$ip_address" >/dev/null; then
echo "$1 is online now."
pushover "$1 is online now."
break
fi
done
}
pnotify-offline() {
local ip_address="$1"
echo "Monitoring $ip_address …"
while true; do
if ! ping -c 1 -W 1 "$ip_address" >/dev/null; then
echo "$1 is offline now."
pushover "$1 is offline now."
break
fi
done
}