Skip to content

Commit

Permalink
feat: Add support for gum in some configs and scripts
Browse files Browse the repository at this point in the history
Improve fzf support when $SHELL is not set to either bash or zsh
Add gum support in some configs and scripts
Improve git stash configs
  • Loading branch information
mike325 committed Aug 8, 2023
1 parent 9226e2a commit 36563fe
Show file tree
Hide file tree
Showing 7 changed files with 197 additions and 113 deletions.
4 changes: 2 additions & 2 deletions bin/fe.sh
Original file line number Diff line number Diff line change
Expand Up @@ -147,9 +147,9 @@ fe() {
local files
# shellcheck disable=SC2207
if [[ ! "$(uname -r)" =~ 4\.(4\.0-142|15.0-44) ]]; then
IFS=$'\n' files=($(fzf --query="$1" --multi --select-1 --exit-0 --preview-window 'right:60%' --preview '(bat --color=always {} || cat {}) 2> /dev/null' --height=60%))
IFS=$'\n' files=($(bash -c "$FZF_DEFAULT_COMMAND" | fzf --query="$1" --multi --select-1 --exit-0 --preview-window 'right:60%' --preview 'bat --color=always {} || cat {}' --height=60%))
else
IFS=$'\n' files=($(fzf --query="$1" --multi --select-1 --exit-0 --height=60%))
IFS=$'\n' files=($(bash -c "$FZF_DEFAULT_COMMAND" | fzf --query="$1" --multi --select-1 --exit-0 --height=60%))
fi
# shellcheck disable=SC2128
[[ -n $files ]] && ${EDITOR} "${files[@]}"
Expand Down
19 changes: 16 additions & 3 deletions git/bin/git-co
Original file line number Diff line number Diff line change
Expand Up @@ -364,8 +364,21 @@ verbose_msg "OS : ${OS}"

# TODO: Add commit log as preview
checkout() {
local gum_filter='gum filter --fuzzy --height=60 --width=0 --limit=1 --header "Select branch"'
local fzf_filter='fzf --select-1 --exit-0 --height=60%'

local filter
if hash fzf 2>/dev/null; then
filter="$fzf_filter"
elif hash gum 2>/dev/null ; then
filter="$gum_filter"
else
error_msg "Missing fzf and gum, cannot continue"
return 1
fi

local branch
branch=$( git branch --all | tr -d '*' | tr -d '+' | awk '{gsub("remotes/", "", $1) ; print $1}' | fzf --select-1 --exit-0 --height=60%)
branch=$( git branch --all | tr -d '*' | tr -d '+' | awk '{gsub("remotes/", "", $1) ; print $1}' | bash -c "$filter")
# NOTE: Assuming we just have 1 remote
# remote="$(git remote show)"
if [[ -n $branch ]]; then
Expand All @@ -377,15 +390,15 @@ checkout() {
}

# TODO: Add filter capabilities
if hash fzf 2>/dev/null; then
if hash fzf 2>/dev/null || hash gum 2>/dev/null; then
if git rev-parse --show-toplevel &>/dev/null; then
checkout
else
error_msg "No a git repository"
exit 1
fi
else
error_msg "FZF is not install"
error_msg "Missing FZF and gum"
exit 1
fi

Expand Down
2 changes: 1 addition & 1 deletion git/bin/git-fstash
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ function status_msg() {
fstash() {
local out q k sha
while out=$(
git stash list --pretty="%C(yellow)%h %>(14)%Cgreen%cr %C(blue)%gs" |
git stash list --pretty="%C(magenta)%gd %C(yellow)%h %>(14)%Cgreen%cr %C(blue)%gs" |
fzf --ansi --no-sort --query="$q" --print-query \
--expect=ctrl-d,ctrl-b --height=60%
); do
Expand Down
16 changes: 15 additions & 1 deletion git/bin/git-ignore
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,24 @@ __gi() {
}

if [ "$#" -eq 0 ]; then

gum_filter='gum filter --fuzzy --height=60 --width=0 --limit=1 --header "Select language"'
fzf_filter='fzf --multi --ansi --exit-0 --height=60%'

filter
if hash fzf 2>/dev/null; then
filter="$fzf_filter"
elif hash gum 2>/dev/null ; then
filter="$gum_filter"
else
error_msg "Missing fzf and gum, cannot continue"
exit 1
fi

IFS+=","
for item in $(__gi list); do
echo "$item"
done | fzf --multi --ansi | paste -s -d "," - |
done | bash -c "$filter" | paste -s -d "," - |
{
read -r result && { [[ -n $result ]] && __gi "$result"; }
}
Expand Down
138 changes: 72 additions & 66 deletions git/bin/git-wt
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,9 @@ while [[ $# -gt 0 ]]; do
-l | --list)
LIST=1
;;
-p | --pr | --pull-request)
LIST=1
;;
# -)
# while read -r from_stdin; do
# FROM_STDIN=("$from_stdin")
Expand Down Expand Up @@ -376,84 +379,87 @@ verbose_msg "OS : ${OS}"
# CODE Goes Here #
#######################################################################

if ! hash fzf 2>/dev/null || ! hash gum 2>/dev/null; then
gum_filter='gum filter --fuzzy --height=60 --width=0 --limit=1 --header "Select worktree"'
fzf_filter='fzf --select-1 --exit-0 --height=60% '

filter
if hash fzf 2>/dev/null; then
filter="$fzf_filter"
elif hash gum 2>/dev/null ; then
filter="$gum_filter"
else
error_msg "$NAME requires fzf and gum to work"
exit 1
fi

if hash fzf 2>/dev/null; then
if [[ $LIST -eq 1 ]]; then
git worktree list
elif [[ $REMOVE -eq 1 ]]; then
wt="$(git worktree list | fzf --select-1 --exit-0 --height=60% | awk '{print $1}')"
if [[ -n $wt ]]; then
status_msg "Removing worktree ${wt##*/}"
if gum confirm "Remove worktree: ${wt##*/}"; then
if ! git worktree remove "$wt"; then
error_msg "Failed to remove worktree"
exit 1
fi
if [[ $LIST -eq 1 ]]; then
git worktree list
elif [[ $REMOVE -eq 1 ]]; then
wt="$(git worktree list | bash -c "$filter" | awk '{print $1}')"
if [[ -n $wt ]]; then
status_msg "Removing worktree ${wt##*/}"
if gum confirm "Remove worktree: ${wt##*/}"; then
if ! git worktree remove "$wt"; then
error_msg "Failed to remove worktree"
exit 1
fi
fi
else
branch=$(git branch --all | tr -d '*' | tr -d '+' | awk '{gsub("remotes/", "", $1) ; print $1}' | fzf --select-1 --exit-0 --height=60%)
if [[ -n $branch ]]; then
verbose_msg "Selected branch: $branch"
if ! git worktree list | awk '{print $3}' | command grep -q "$branch"; then
# TODO: Add support for more remotes
if [[ $branch =~ ^origin/* ]]; then
if ! git branch --all | tr -d '*' | awk '{gsub("remotes/", "", $1) ; print $1}' | command grep -q -E "^${branch#origin/}"; then
status_msg "Creating local branch from remote $branch"
if ! git branch --track "${branch#origin/}" "remotes/$branch"; then
error_msg "Failed to create local branch"
exit 1
fi
fi
else
branch=$(git branch --all | tr -d '*' | tr -d '+' | awk '{gsub("remotes/", "", $1) ; print $1}' | bash -c "$filter")
if [[ -n $branch ]]; then
verbose_msg "Selected branch: $branch"
if ! git worktree list | awk '{print $3}' | command grep -q "$branch"; then
# TODO: Add support for more remotes
if [[ $branch =~ ^origin/* ]]; then
if ! git branch --all | tr -d '*' | awk '{gsub("remotes/", "", $1) ; print $1}' | command grep -q -E "^${branch#origin/}"; then
status_msg "Creating local branch from remote $branch"
if ! git branch --track "${branch#origin/}" "remotes/$branch"; then
error_msg "Failed to create local branch"
exit 1
fi
branch="${branch#origin/}"
fi
status_msg "Creating worktree"
branch_dir="${branch#FER-[0-9]*-}"
for i in feature fix bug feat refactor; do
branch_dir="${branch_dir#"$i"/}"
done
verbose_msg "Dir location: ${branch_dir} set to branch $branch"
if ! git worktree add "./../${branch_dir}" "$branch"; then
error_msg "failed to create worktree"
exit 1
fi
branch="${branch#origin/}"
fi
wt_dir="$(git worktree list | command grep "${branch#origin/}" | awk '{print $1}')"
verbose_msg "Moving to $wt_dir"
pushd "$wt_dir" 2>/dev/null || exit 1

git_dir="$(git rev-parse --git-dir)"
verbose_msg "Git dir: $git_dir"

project_files=(
".clang-format"
".clang-tidy"
".editorconfig"
".project.lua"
".projections.json"
".pre-commit-config.yaml"
"pyproject.toml"
)

status_msg "Copying project files"
for i in "${project_files[@]}"; do
if [[ -f "$git_dir/../../$i" ]]; then
verbose_msg "linking $i"
ln "$git_dir/../../$i" ./
fi
status_msg "Creating worktree"
branch_dir="${branch#FER-[0-9]*-}"
for i in feature fix bug feat refactor; do
branch_dir="${branch_dir#"$i"/}"
done

# TODO: Make this dynamic and create it with just C/C++ repos
[ ! -d ./.cache/clangd/pchs ] && mkdir -p './.cache/clangd/pchs/'
verbose_msg "Dir location: ${branch_dir} set to branch $branch"
if ! git worktree add "./../${branch_dir}" "$branch"; then
error_msg "failed to create worktree"
exit 1
fi
fi
wt_dir="$(git worktree list | command grep "${branch#origin/}" | awk '{print $1}')"
verbose_msg "Moving to $wt_dir"
pushd "$wt_dir" 2>/dev/null || exit 1

git_dir="$(git rev-parse --git-dir)"
verbose_msg "Git dir: $git_dir"

project_files=(
".clang-format"
".clang-tidy"
".editorconfig"
".project.lua"
".projections.json"
".pre-commit-config.yaml"
"pyproject.toml"
)

status_msg "Copying project files"
for i in "${project_files[@]}"; do
if [[ -f "$git_dir/../../$i" ]]; then
verbose_msg "linking $i"
ln "$git_dir/../../$i" ./
fi
done

# TODO: Make this dynamic and create it with just C/C++ repos
[ ! -d ./.cache/clangd/pchs ] && mkdir -p './.cache/clangd/pchs/'
fi
else
error_msg "FZF is not install"
exit 1
fi

#######################################################################
Expand Down
19 changes: 16 additions & 3 deletions git/gitconfig
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@
;###############################################################################

sc = !git --no-pager stash clear
sl = !git --no-pager stash list
sl = !git --no-pager stash list --pretty='%C(magenta)%gd %C(yellow)%h %>(14)%Cgreen%cr %C(blue)%gs'

;###############################################################################
; rev-parse shortcuts #
Expand All @@ -343,6 +343,19 @@
svnd = svn dcommit
svnl = svn log --oneline --show-commit

;###############################################################################
; gh integration
;###############################################################################

; TODO: Add new shell script to handle arguments in a single git-pr entry point
prl = "!gh pr list "

; NOTE: requires gum
pro = "!f(){ \
local pr=\"$(gh pr list | cut -f1,2 | gum filter --fuzzy --limit=1 --height=20 --header='Select PR' | cut -f1)\"; \
[[ -n $pr ]] && gh pr checkout \"$pr\"; \
}; f"

;###############################################################################
; Custom functions #
;###############################################################################
Expand Down Expand Up @@ -388,14 +401,14 @@
ulc = "!f(){ git reset --soft HEAD~${1}; }; f"

; Apply, pop or drop a specific stash
sw = "!f(){ git stash show -p stash@{${1:-0}}; }; f"
sw = "!f(){ git stash show --diff-algorithm=patience -B -C --find-copies-harder -p stash@{${1:-0}}; }; f"
sa = "!f(){ git --no-pager stash apply stash@{${1:-0}}; }; f"
sp = "!f(){ git --no-pager stash pop stash@{${1:-0}}; }; f"
ss = "!f(){ git --no-pager stash save $1; }; f"
sd = "!f(){ git --no-pager stash drop stash@{${1:-0}}; }; f"

; manage last stash
swl = "!f(){ git stash show -p stash@{$(($(git stash list | wc -l) - 1))}; }; f"
swl = "!f(){ git stash show --diff-algorithm=patience -B -C --find-copies-harder -p stash@{$(($(git stash list | wc -l) - 1))}; }; f"
sal = "!f(){ git --no-pager stash apply stash@{$(($(git stash list | wc -l) - 1))}; }; f"
spl = "!f(){ git --no-pager stash pop stash@{$(($(git stash list | wc -l) - 1))}; }; f"
sdl = "!f(){ git --no-pager stash drop stash@{$(($(git stash list | wc -l) - 1))}; }; f"
Expand Down
Loading

0 comments on commit 36563fe

Please sign in to comment.