dotfiles/zsh/.config/zsh/functions
2023-11-10 20:57:01 -05:00

155 lines
2.5 KiB
Text

function advanced-git-checkout {
if [ $# -eq 0 ]; then
git branch \
| fzf \
| xargs -I {} git checkout {}
else
git checkout $@
fi
}
function ag-count {
if [ $# -lt 1 ]; then
echo Usage: ag-count SEARCHTERM
else
searchterm=$1; shift
ag --stats $searchterm | tail -n5
fi
}
function aws-profile {
if [ $# -eq 0 ]; then
echo Current AWS Profile: $AWS_PROFILE
else
export AWS_DEFAULT_PROFILE=$1
export AWS_PROFILE=$1
fi
}
function docker-compose-call {
if docker compose > /dev/null 2>&1; then
docker compose $@
else
docker-compose $@
fi
}
function find-local-bin {
local prefix=$1
local bin_dir="$prefix-bin"
local found_bin_dir=""
current_dir=$(pwd)
found_bin_dir=$current_dir/$bin_dir
while [ ! -d $found_bin_dir ]; do
if [[ $current_dir == $HOME ]]; then
break
fi
current_dir=$(realpath "$current_dir/..")
found_bin_dir=$current_dir/$bin_dir
done
echo $found_bin_dir
}
function g {
if [[ $# > 0 ]]; then
git $@
else
git status
fi
}
function git-date-added {
if [ $# -eq 0 ]; then
echo Usage: git-date-added FILENAME
else
git log --format=%aD $1 | tail -1
fi
}
function git-since-last-tag {
git log $(git describe --tags --abbrev=0)..HEAD --no-merges --oneline
}
function git-root {
if [[ "$(git rev-parse --is-inside-work-tree)" == "true" ]]; then
cd $(git root)
fi
}
function install-ruby {
if [ $# -eq 0 ]; then
echo Usage: install-ruby RUBY_VERSION
else
ruby-build $1 $HOME/.rubies/$1
fi
}
function password-generator {
if [ -x "$(command -v openssl)" ]; then
password=$(
openssl rand -base64 32 \
| head -c 32
)
else
password=$(
date +%s \
| sha256sum \
| base64 \
| head -c 32
)
fi
echo $password
}
function run {
if [ $# -lt 2 ]; then
echo Usage: run NUMBER COMMAND
else
number=$1; shift
for i in `seq $number`; do
eval "$@"
done
fi
}
function run-rails {
if [ -f bin/rails ]; then
bin/rails $@
else
bundle exec rails
fi
}
function test-rails {
if bundle exec rspec --help > /dev/null 2>&1; then
if [ -d spec ]; then
bundle exec rspec $@
else
ruby *_test.rb
fi
elif bundle exec rails version > /dev/null 2>&1; then
bundle exec rails test $@
else
if [[ $# > 0 ]]; then
ruby $@
else
ruby *_test.rb
fi
fi
}
function until-fail {
while "$@"; do :; done
}
function zsh-time {
for i in $(seq 1 10); do
time zsh -i -c exit
done
}