(bash) refactor

This commit is contained in:
juancwu 2025-09-04 21:22:46 -04:00
commit 52ca330c12

View file

@ -1,16 +1,11 @@
# -------------- Aliases # -------------- Aliases
alias gs="git status" alias gs="git status"
alias gp="git pull"
alias gP="git push"
alias lg='lazygit'
alias ls='lsd' alias ls='lsd'
# ll alias breakdown
# -A includes hidden files but excludes . and ..
# -l displays the listing in long format, showing file attributes such as permissions
# -F appends a character to each entry in the listing to indicate the file type (e.g '/' for directories and '*' for executables)
alias ll="lsd -AlF" alias ll="lsd -AlF"
# la alias breakdown
# -A list all entries without ./ and ../
alias la="lsd -A" alias la="lsd -A"
# Add an "alert" alias for long running commands. Use like so: # Add an "alert" alias for long running commands. Use like so:
@ -30,220 +25,220 @@ INFO=$'\033[39;44mINFO:\033[0m'
# fuzzy cd into specific folders from anywhere # fuzzy cd into specific folders from anywhere
# Deeply searches through commonly used directory trees # Deeply searches through commonly used directory trees
fcd() { fcd() {
local selected_dir="" local selected_dir=""
# Common directories to exclude
local exclude_args=(
"-E" ".git"
"-E" "node_modules"
"-E" "vendor"
"-E" ".cache"
"-E" "dist"
"-E" "build"
"-E" "target"
"-E" ".next"
"-E" ".nuxt"
"-E" "__pycache__"
"-E" ".pytest_cache"
"-E" ".venv"
"-E" "venv"
"-E" "env"
"-E" ".env"
"-E" "coverage"
"-E" ".nyc_output"
"-E" ".sass-cache"
"-E" "bower_components"
"-E" ".idea"
"-E" ".vscode"
"-E" ".vs"
"-E" "*.egg-info"
"-E" ".tox"
"-E" ".mypy_cache"
"-E" ".ruff_cache"
"-E" ".turbo"
"-E" "out"
"-E" "tmp"
"-E" ".svn"
"-E" ".hg"
"-E" ".bzr"
)
if [ $# -eq 1 ]; then # Common directories to exclude
selected_dir=$({ local exclude_args=(
# Deep search in .config (but not too deep to avoid noise) "-E" ".git"
fd -t d -t l --max-depth 3 "${exclude_args[@]}" . "$HOME/.config" 2>/dev/null "-E" "node_modules"
"-E" "vendor"
# Shallow search in .cache (usually don't need deep access here) "-E" ".cache"
fd -t d --max-depth 1 "${exclude_args[@]}" . "$HOME/.cache" 2>/dev/null "-E" "dist"
"-E" "build"
# Search in ghq projects (your git repositories) "-E" "target"
fd -t d "${exclude_args[@]}" . "$HOME/ghq" 2>/dev/null "-E" ".next"
"-E" ".nuxt"
# Search home directories (excluding common unnecessary ones) "-E" "__pycache__"
fd -t d --max-depth 3 "${exclude_args[@]}" \ "-E" ".pytest_cache"
-E "Library" \ "-E" ".venv"
-E "Applications" \ "-E" "venv"
-E ".Trash" \ "-E" "env"
-E ".local" \ "-E" ".env"
-E ".npm" \ "-E" "coverage"
-E ".cargo" \ "-E" ".nyc_output"
-E ".rustup" \ "-E" ".sass-cache"
-E ".gem" \ "-E" "bower_components"
-E ".rbenv" \ "-E" ".idea"
-E ".pyenv" \ "-E" ".vscode"
-E ".nvm" \ "-E" ".vs"
-E ".docker" \ "-E" "*.egg-info"
-E ".vagrant" \ "-E" ".tox"
-E ".m2" \ "-E" ".mypy_cache"
-E ".gradle" \ "-E" ".ruff_cache"
-E ".android" \ "-E" ".turbo"
-E ".wine" \ "-E" "out"
-E "snap" \ "-E" "tmp"
. "$HOME" 2>/dev/null "-E" ".svn"
} | sort -u | fzf --filter="$1" --select-1 --exit-0 | head -1) "-E" ".hg"
else "-E" ".bzr"
selected_dir=$({ )
# Deep search in .config (but not too deep to avoid noise)
fd -t d -t l --max-depth 3 "${exclude_args[@]}" . "$HOME/.config" 2>/dev/null
# Shallow search in .cache (usually don't need deep access here)
fd -t d --max-depth 1 "${exclude_args[@]}" . "$HOME/.cache" 2>/dev/null
# Search in ghq projects (your git repositories)
fd -t d "${exclude_args[@]}" . "$HOME/ghq" 2>/dev/null
# Search home directories (excluding common unnecessary ones)
fd -t d --max-depth 3 "${exclude_args[@]}" \
-E "Library" \
-E "Applications" \
-E ".Trash" \
-E ".local" \
-E ".npm" \
-E ".cargo" \
-E ".rustup" \
-E ".gem" \
-E ".rbenv" \
-E ".pyenv" \
-E ".nvm" \
-E ".docker" \
-E ".vagrant" \
-E ".m2" \
-E ".gradle" \
-E ".android" \
-E ".wine" \
-E "snap" \
. "$HOME" 2>/dev/null
} | sort -u | fzf)
fi
if [ -n "$selected_dir" ]; then if [ $# -eq 1 ]; then
cd "$selected_dir" selected_dir=$({
if [[ -f .nvmrc ]]; then # Deep search in .config (but not too deep to avoid noise)
NVMRC_VERSION=$(cat .nvmrc) fd -t d -t l --max-depth 3 "${exclude_args[@]}" . "$HOME/.config" 2>/dev/null
CURRENT_VERSION=$(nvm current)
if [ "$NVMRC_VERSION" != "$CURRENT_VERSION" ]; then # Shallow search in .cache (usually don't need deep access here)
nvm use fd -t d --max-depth 1 "${exclude_args[@]}" . "$HOME/.cache" 2>/dev/null
fi
fi # Search in ghq projects (your git repositories)
else fd -t d "${exclude_args[@]}" . "$HOME/ghq" 2>/dev/null
echo -e "$ERROR No selection made."
# Search home directories (excluding common unnecessary ones)
fd -t d --max-depth 3 "${exclude_args[@]}" \
-E "Library" \
-E "Applications" \
-E ".Trash" \
-E ".local" \
-E ".npm" \
-E ".cargo" \
-E ".rustup" \
-E ".gem" \
-E ".rbenv" \
-E ".pyenv" \
-E ".nvm" \
-E ".docker" \
-E ".vagrant" \
-E ".m2" \
-E ".gradle" \
-E ".android" \
-E ".wine" \
-E "snap" \
. "$HOME" 2>/dev/null
} | sort -u | fzf --filter="$1" --select-1 --exit-0 | head -1)
else
selected_dir=$({
# Deep search in .config (but not too deep to avoid noise)
fd -t d -t l --max-depth 3 "${exclude_args[@]}" . "$HOME/.config" 2>/dev/null
# Shallow search in .cache (usually don't need deep access here)
fd -t d --max-depth 1 "${exclude_args[@]}" . "$HOME/.cache" 2>/dev/null
# Search in ghq projects (your git repositories)
fd -t d "${exclude_args[@]}" . "$HOME/ghq" 2>/dev/null
# Search home directories (excluding common unnecessary ones)
fd -t d --max-depth 3 "${exclude_args[@]}" \
-E "Library" \
-E "Applications" \
-E ".Trash" \
-E ".local" \
-E ".npm" \
-E ".cargo" \
-E ".rustup" \
-E ".gem" \
-E ".rbenv" \
-E ".pyenv" \
-E ".nvm" \
-E ".docker" \
-E ".vagrant" \
-E ".m2" \
-E ".gradle" \
-E ".android" \
-E ".wine" \
-E "snap" \
. "$HOME" 2>/dev/null
} | sort -u | fzf)
fi
if [ -n "$selected_dir" ]; then
cd "$selected_dir"
if [[ -f .nvmrc ]]; then
NVMRC_VERSION=$(cat .nvmrc)
CURRENT_VERSION=$(nvm current)
if [ "$NVMRC_VERSION" != "$CURRENT_VERSION" ]; then
nvm use
fi
fi fi
else
echo -e "$ERROR No selection made."
fi
} }
# clone repository # clone repository
cl() { cl() {
if [[ $# -eq 0 ]]; then if [[ $# -eq 0 ]]; then
# help text # help text
echo "Usage: cl REPOSITORY_NAME" echo "Usage: cl REPOSITORY_NAME"
echo "Usage: cl REPOSITORY_URL" echo "Usage: cl REPOSITORY_URL"
echo "Usage: cl (hub|lab) REPOSITORY_NAME" echo "Usage: cl (hub|lab) REPOSITORY_NAME"
echo "Usage: cl (hub|lab) NAMESPACE REPOSITORY_NAME" echo "Usage: cl (hub|lab) NAMESPACE REPOSITORY_NAME"
return 0 return 0
fi
local url=$1
local ghq_dir="$HOME/ghq"
local namespace=""
local repository_name=""
# extract project name
if [[ $url =~ git@git(lab|hub)\.com:([^/]+)/([^/]+)\.git ]]; then
local namespace="${BASH_REMATCH[2]}"
local repository_name="${BASH_REMATCH[3]}"
elif [[ $url =~ https://git(lab|hub)\.com/([^/]+)/([^/]+)\.git ]]; then
local namespace="${BASH_REMATCH[2]}"
local repository_name="${BASH_REMATCH[3]}"
elif [[ $# -ne 0 ]]; then
repository_name=$1
namespace="juancwu"
# default domain to github
local domain="hub"
# possible domain given
if [[ $# -eq 2 ]]; then
domain=$1
repository_name=$2
fi fi
if [[ $# -eq 3 ]]; then
local url=$1 # it has been given a domain, namespace and repository name
local ghq_dir="$HOME/ghq" domain=$1
local namespace="" namespace=$2
local repository_name="" repository_name=$3
# extract project name
if [[ $url =~ git@git(lab|hub)\.com:([^/]+)/([^/]+)\.git ]]; then
local namespace="${BASH_REMATCH[2]}"
local repository_name="${BASH_REMATCH[3]}"
elif [[ $url =~ https://git(lab|hub)\.com/([^/]+)/([^/]+)\.git ]]; then
local namespace="${BASH_REMATCH[2]}"
local repository_name="${BASH_REMATCH[3]}"
elif [[ $# -ne 0 ]]; then
repository_name=$1
namespace="juancwu"
# default domain to github
local domain="hub"
# possible domain given
if [[ $# -eq 2 ]]; then
domain=$1
repository_name=$2
fi
if [[ $# -eq 3 ]]; then
# it has been given a domain, namespace and repository name
domain=$1
namespace=$2
repository_name=$3
fi
url="git@git$domain.com:$namespace/$repository_name.git"
else
echo -e "$ERROR Invalid URL format"
return 1
fi fi
url="git@git$domain.com:$namespace/$repository_name.git"
else
echo -e "$ERROR Invalid URL format"
return 1
fi
# check if directory for project exists or not # check if directory for project exists or not
local repository_dir="${ghq_dir}/${namespace}/${repository_name}" local repository_dir="${ghq_dir}/${namespace}/${repository_name}"
if [[ ! -d $repository_dir ]]; then if [[ ! -d $repository_dir ]]; then
mkdir -p $repository_dir mkdir -p $repository_dir
fi fi
git clone $url $repository_dir git clone $url $repository_dir
} }
# delete local branches that don't exists in remote repository # delete local branches that don't exists in remote repository
git-prune() { git-prune() {
git fetch --prune git fetch --prune
git branch -vv | grep '\[origin/.*: gone\]' | awk '{print $1}' | xargs git branch -d git branch -vv | grep '\[origin/.*: gone\]' | awk '{print $1}' | xargs git branch -d
} }
# fuzzy find branches and switch to selected branch # fuzzy find branches and switch to selected branch
gc() { gc() {
if [ $# -eq 1 ]; then if [ $# -eq 1 ]; then
local selected_branch=$(git branch | fzf --filter="$1" --select-1 --exit-0 | head -1 | sed 's/^[ \*]*//') local selected_branch=$(git branch | fzf --filter="$1" --select-1 --exit-0 | head -1 | sed 's/^[ \*]*//')
git checkout "$selected_branch" git checkout "$selected_branch"
return return
fi fi
local selected_branch=$(git branch | fzf | sed 's/^[ \*]*//') local selected_branch=$(git branch | fzf | sed 's/^[ \*]*//')
if [ -n "$selected_branch" ]; then if [ -n "$selected_branch" ]; then
git checkout "$selected_branch" git checkout "$selected_branch"
else else
echo "No branch selected" echo "No branch selected"
fi fi
} }
# fuzzy find remote branches and switch to selected branch # fuzzy find remote branches and switch to selected branch
gcr() { gcr() {
git fetch git fetch
if [ $# -eq 1 ]; then if [ $# -eq 1 ]; then
local selected_branch=$(git branch -r | fzf --filter="$1" --select-1 --exit-0 | head -1 | sed -E 's/^([ \*]*origin\/[\ *]*)*//') local selected_branch=$(git branch -r | fzf --filter="$1" --select-1 --exit-0 | head -1 | sed -E 's/^([ \*]*origin\/[\ *]*)*//')
git checkout "$selected_branch" git checkout "$selected_branch"
return return
fi fi
local selected_branch=$(git branch -r | fzf | sed -E 's/^([ \*]*origin\/[\ *]*)*//')
if [ -n "$selected_branch" ]; then local selected_branch=$(git branch -r | fzf | sed -E 's/^([ \*]*origin\/[\ *]*)*//')
git checkout "$selected_branch"
else if [ -n "$selected_branch" ]; then
echo "No branch selected" git checkout "$selected_branch"
fi else
echo "No branch selected"
fi
} }
export GOPATH=$HOME/go export GOPATH=$HOME/go