update bash script

This commit is contained in:
jc 2025-09-03 19:45:56 -04:00
commit 340f797fd0
3 changed files with 53 additions and 543 deletions

View file

@ -1,238 +0,0 @@
# -------------- Aliases
alias gs="git status"
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"
# la alias breakdown
# -A list all entries without ./ and ../
alias la="lsd -A"
# Add an "alert" alias for long running commands. Use like so:
# sleep 10; alert
alias alert='notify-send --urgency=normal -i "$([ $? = 0 ] && echo terminal || echo error)" "$(history|tail -n1|sed -e '\''s/^\s*[0-9]\+\s*//;s/[;&|]\s*alert$//'\'')"'
# add alias for clear, makes it easier
alias cc="clear"
# always select the clipboard
alias xc="xclip -selection clipboard"
alias xco="xclip -o -selection clipboard"
# Nice line headers for logs
ERROR=$'\033[39;41mERROR:\033[0m'
SUCCESS=$'\033[39;42mSUCCESS:\033[0m'
WARNING=$'\033[39;43mWARNING:\033[0m'
INFO=$'\033[39;44mINFO:\033[0m'
# -------------- Functions
# list directory and cd into it
sd() {
local path=${1:-.}
local result=$(ls -d ${path}/*/ 2> /dev/null | fzf)
if [[ -n "${result}" ]]; then
cd "${result}"
else
echo -e "$ERROR No directories found or no selection made."
fi
}
# list files and opens it in neovim
sf() {
local path=${2:-.}
if [[ $1 == "-h" ]]; then
local result=$(find ${path} -type f -name '.*' 2> /dev/null | fzf)
else
local result=$(find ${path} -type f 2> /dev/null | fzf)
fi
if [[ -n "${result}" ]]; then
nvim "${result}"
else
echo -e "$ERROR No files found or no selecteion made."
fi
}
# fuzzy cd into specific folders
fcd() {
local selected_dir=""
if [ $# -eq 1 ]; then
selected_dir=$({
find "$HOME/.config" -maxdepth 1 -type d
find "$HOME/.config" -maxdepth 1 -type l
find "$HOME/.cache" -maxdepth 1 -type d
find "$HOME/ghq" -mindepth 2 -maxdepth 2 -type d
ls -d -1 "$HOME/"/*/ | grep -v \.git
} | fzf --filter="$1" --select-1 --exit-0 | head -1)
else
selected_dir=$({
find "$HOME/.config" -maxdepth 1 -type d
find "$HOME/.config" -maxdepth 1 -type l
find "$HOME/.cache" -maxdepth 1 -type d
find "$HOME/ghq" -mindepth 2 -maxdepth 2 -type d
ls -d -1 "$HOME/"/*/ | grep -v \.git
} | 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
else
echo -e "$ERROR No selection made."
fi
}
# fuzzy find all directories from cwd
# or quickly select best match from query
cdf() {
local selected_dir=""
if [ $# -eq 1 ]; then
selected_dir=$(find . -type d | fzf --filter="$1" --select-1 --exit-0 | head -1)
else
selected_dir=$(find . -type d | fzf)
fi
if [ -n "$selected_dir" ]; then
cd "$selected_dir"
fi
}
# clone repository
cl() {
if [[ $# -eq 0 ]]; then
# help text
echo "Usage: cl REPOSITORY_NAME"
echo "Usage: cl REPOSITORY_URL"
echo "Usage: cl (hub|lab) REPOSITORY_NAME"
echo "Usage: cl (hub|lab) NAMESPACE REPOSITORY_NAME"
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
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
# check if directory for project exists or not
local repository_dir="${ghq_dir}/${namespace}/${repository_name}"
if [[ ! -d $repository_dir ]]; then
mkdir -p $repository_dir
fi
git clone $url $repository_dir
}
# get branch if available
parse-git-branch() {
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'
}
# delete local branches that don't exists in remote repository
git-prune() {
git fetch --prune
git branch -vv | grep '\[origin/.*: gone\]' | awk '{print $1}' | xargs git branch -d
}
# fuzzy find branches and switch to selected branch
gc() {
if [ $# -eq 1 ]; then
local selected_branch=$(git branch | fzf --filter="$1" --select-1 --exit-0 | head -1 | sed 's/^[ \*]*//')
git checkout "$selected_branch"
return
fi
local selected_branch=$(git branch | fzf | sed 's/^[ \*]*//')
if [ -n "$selected_branch" ]; then
git checkout "$selected_branch"
else
echo "No branch selected"
fi
}
# fuzzy find remote branches and switch to selected branch
gcr() {
git fetch
if [ $# -eq 1 ]; then
local selected_branch=$(git branch -r | fzf --filter="$1" --select-1 --exit-0 | head -1 | sed -E 's/^([ \*]*origin\/[\ *]*)*//')
git checkout "$selected_branch"
return
fi
local selected_branch=$(git branch -r | fzf | sed -E 's/^([ \*]*origin\/[\ *]*)*//')
if [ -n "$selected_branch" ]; then
git checkout "$selected_branch"
else
echo "No branch selected"
fi
}
# set bat theme
export BAT_THEME="ansi"
bc() {
mac=$(bluetoothctl devices | rg "$1" | awk '{print $2}')
bluetoothctl connect $mac
}
# helper function to fuzzy search files in the current working directory
ed() {
local f=""
if [ $# -eq 1 ]; then
f=$(find . | grep -Ev "node_modules|\.git/" | fzf --filter="$1" --select-1 --exit-0 | head -1)
else
f=$(find . | grep -Ev "node_modules|\.git/" | fzf)
fi
if [ -n "$f" ]; then
nvim "$f"
fi
}
export GOPATH=$HOME/go
export PATH=$PATH:$GOPATH:$GOPATH/bin:/usr/local/go/bin
export EDITOR=nvim