dotfiles/.functions
2021-09-28 11:15:35 -07:00

231 lines
5 KiB
Bash

#!/usr/bin/env bash
# Create a new directory and enter it
cdm() {
mkdir -p "$1" && cd "$1";
}
# `which` but follow symlinks and check aliases and functions
where() {
is_alias=$(alias | grep "alias\ $1=" 2>/dev/null)
is_func=$(declare -F | grep "\ $1$" 2>/dev/null)
if [[ "$is_alias" != "" ]]; then
echo "${is_alias//alias\ /}"
elif [[ "$is_func" != "" ]]; then
${is_func}
elif [[ $(which "$1") != "" ]]; then
start=$(pwd)
info=$(ls -l "$(which "$1")")
first=$(dirname "$(echo "$info" | awk '{ print $9; }')")
cd "$first"
# if the file isn't a symlink, and so there's no second path listed,
# `dirname` helpfully returns '.' so `cd $second` executes harmlessly
second=$(dirname "$(echo "$info" | awk '{ print $11; }')")
cd "$second"
echo "$(pwd)/$1"
cd "$start"
fi
}
bunk() {
for line in "$@"; do
history -d "$line"
done
}
# Change working directory to the top-most Finder window location
# short for `cdfinder`
cdf() {
cd "$(osascript -e 'tell app "Finder" to POSIX path of (insertion location as alias)')" || exit 1
}
# brew install entr
# see bin/reload-safari
reload-for() {
if [[ -z "$1" ]]; then
echo "Error [return-for]: Specify the type(s) of files to watch."
return 1
fi
find_func="find $(pwd) -name '*.'$1"
for ext in "$@"; do
if [[ "$ext" != "$1" ]]; then
find_func="$find_func -o -name '*.'$ext";
fi
done
# ¯\_(ツ)_/¯ http://mywiki.wooledge.org/BashFAQ/048
eval "$find_func | entr reload-safari"
}
# brew install poppler
pdfhead() {
if [[ -n "$2" ]]; then
head_opt="$1"
pdf="$2"
else
pdf="$1"
head_opt="-10"
fi
pdftotext "$pdf" - | head "$head_opt"
}
pdftail() {
if [[ -n "$2" ]]; then
tail_opt="$1"
pdf="$2"
else
pdf="$1"
tail_opt="-10"
fi
pdftotext "$pdf" - | tail "$tail_opt"
}
# Create a .tar.gz archive, using `zopfli`, `pigz` or `gzip` for compression
targz() {
local tmpFile="${*%/}.tar";
tar -cvf "${tmpFile}" --exclude=".DS_Store" "${@}" || return 1;
size=$(
stat -f"%z" "${tmpFile}" 2> /dev/null; # OS X `stat`
stat -c"%s" "${tmpFile}" 2> /dev/null; # GNU `stat`
);
local cmd="";
if (( size < 52428800 )) && hash zopfli 2> /dev/null; then
# the .tar file is smaller than 50 MB and Zopfli is available; use it
cmd="zopfli";
else
if hash pigz 2> /dev/null; then
cmd="pigz";
else
cmd="gzip";
fi;
fi;
echo "Compressing .tar ($((size / 1000)) kB) using \`${cmd}\`…";
"${cmd}" -v "${tmpFile}" || return 1;
[ -f "${tmpFile}" ] && rm "${tmpFile}";
zippedSize=$(
stat -f"%z" "${tmpFile}.gz" 2> /dev/null; # OS X `stat`
stat -c"%s" "${tmpFile}.gz" 2> /dev/null; # GNU `stat`
);
echo "${tmpFile}.gz ($((zippedSize / 1000)) kB) created successfully.";
}
# Determine size of a file or total size of a directory
fs() {
if du -b /dev/null > /dev/null 2>&1; then
local arg=-sbh;
else
local arg=-sh;
fi
if [[ -n "$@" ]]; then
du $arg -- "$@";
else
du $arg .[^.]* ./*;
fi;
}
# Create a data URL from a file
dataurl() {
local mimeType
mimeType=$(file -b --mime-type "$1");
if [[ $mimeType == text/* ]]; then
mimeType="${mimeType};charset=utf-8";
fi
echo "data:${mimeType};base64,$(openssl base64 -in "$1" | tr -d '\n')";
}
# `o` with no arguments opens the current directory, otherwise opens
# the given location
o() {
if [ $# -eq 0 ]; then
open .;
else
open "$@";
fi;
}
rb() {
if [ "$1" = "" ]; then
eval 'git rebase -i @{u}'
else
git rebase -i HEAD~"$1"
fi
}
gam() {
curl -sL "$1.patch" | git am
}
grev() {
git status > /dev/null 2>&1
repocheck="$?"
if [[ "$repocheck" -gt 0 ]]; then
echo "Error [revv]: $repocheck"
return "$repocheck"
fi
back=""
if [[ "$1" != "" ]]; then
back="~$1"
fi
revision=$(git rev-parse HEAD"$back")
echo "$revision"
echo "$revision" | tr -d '\n' | pbcopy
}
mkredirect() {
cat <<EOF
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>This page has been moved to $1</title>
<meta http-equiv="refresh" content="0;url=$1">
</head>
<body>
</body>
</html>
EOF
}
dork () {
rubyversion=$(cat "$(pwd)/.ruby-version" | tr -d '\n')
rubystring="ruby"
if [[ "$rubyversion" != "" ]]; then
rubystring="ruby:${rubyversion}"
fi
imagename=$(echo "${rubystring}" | sed 's|\:|_|')
if [[ -f "/tmp/${imagename}.cid" ]]; then
docker start "$(cat /tmp/${imagename}.cid)"
docker exec -it "$(cat /tmp/${imagename}.cid)" /bin/sh -il -c "$1"
else
docker run --cidfile "/tmp/${imagename}.cid" -v "$(pwd)":/app -w /app "${rubystring}" /bin/sh -il -c "$1"
fi
}
nixpkg-install () {
package="nixpkgs.${1}"
nix-env -iA "${package}"
}
nixpkg-uninstall () {
nix-env -e "${1}"
}
nixruby () {
freedesktop="$(nixpath shared-mime-info)"
nix-shell -p \
zlib \
libxml2 \
postgresql \
sqlite \
libxslt \
shared-mime-info \
--run "CFLAGS=-Wno-error=format-overflow FREEDESKTOP_MIME_TYPES_PATH=${freedesktop}/share/mime/packages/freedesktop.org.xml ${*}"
}