From 25daf931e011ece1b29acef621e53e3a654ae12d Mon Sep 17 00:00:00 2001 From: Nathan Hyland Date: Tue, 5 Oct 2021 09:18:21 -0700 Subject: [PATCH 1/3] added view and diff --- gh-branch | 44 ++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 40 insertions(+), 4 deletions(-) diff --git a/gh-branch b/gh-branch index 1661948..ecb69ea 100755 --- a/gh-branch +++ b/gh-branch @@ -8,14 +8,25 @@ Usage: gh branch Displays an interactive branch switcher that lists local branches in relation to the pull requests in the repository. The selected branch is checked out. -With \`--static\`, print a non-interactive list of branches. +Flags: + --static Print a non-interactive list of branches. + -d|--diff Print a diff preview of the changes. + -v|--view Print a view of the PR description if there is one. + Dependencies: fzf +Optional dependencies: + glow: make \`gh branch --view\` print a pretty and colorized version of the PR view. + website: https://github.com/charmbracelet/glow + bat: make \`gh branch --diff\` print a colorized version of the diff. + website: https://github.com/sharkdp/bat EOF # You can also select multiple branches with Tab and press Ctrl-D to delete them. } static= +diff= +view= while [ $# -gt 0 ]; do case "$1" in -h|--help) @@ -25,6 +36,14 @@ while [ $# -gt 0 ]; do --static) static=1 ;; + -d|--diff) + [ -n "$view" ] && echo "view and diff can not both be set" && exit 1 + diff=1 + ;; + -v|--view) + [ -n "$diff" ] && echo "view and diff can not both be set" && exit 1 + view=1 + ;; *) help >&2 exit 1 @@ -49,6 +68,7 @@ list_prs() { } state headRefName + body } } } @@ -59,8 +79,8 @@ list_prs() { {{- $stateColor := "green" -}} {{- if eq .state "CLOSED" }}{{ $stateColor = "red" }} {{- else if eq .state "MERGED" }}{{ $stateColor = "magenta" }}{{ end -}} - {{- .number | printf "#%.0f" | color $stateColor -}} - {{- .author.login | printf " by %s\n" -}} + {{- .number | printf "\t#%.0f" | color $stateColor -}} + {{- .author.login | printf "\tby %s\n" -}} {{- end -}} ' } @@ -103,7 +123,23 @@ choose() { local rendered rendered="$(render)" || return 1 #--multi --bind "ctrl-d:execute-silent(git branch -D {+1})+reload(\"$0\" --static)" - fzf --ansi <<<"$rendered" + if [ -n "$view" ]; then + fzf \ + --preview="echo {5} | cut -c2- | xargs gh pr view | if type -p glow &>/dev/null; then glow --style dark -; else cat; fi" \ + --header " scroll preview down/up, scroll next/prev PR" \ + --preview-window 'up,60%,border-bottom,+{2}+3/3,~3', \ + --bind ctrl-d:preview-down,ctrl-u:preview-up \ + --ansi <<<"$rendered" + elif [ -n "$diff" ]; then + fzf \ + --preview="echo {5} | cut -c2- | xargs gh pr diff | if type -p bat &>/dev/null; then bat --style=plain --color=always; else cat; fi" \ + --header " scroll preview down/up, scroll next/prev PR" \ + --preview-window 'up,60%,border-bottom,+{2}+3/3,~3', \ + --bind ctrl-d:preview-down,ctrl-u:preview-up \ + --ansi <<<"$rendered" + else + fzf --ansi <<<"$rendered" + fi } selected="$(choose)" From 2c7cd435736485f35212bb45db9403c6f88d49ac Mon Sep 17 00:00:00 2001 From: Nathan Hyland Date: Tue, 5 Oct 2021 09:26:24 -0700 Subject: [PATCH 2/3] removed graphql body request --- gh-branch | 1 - 1 file changed, 1 deletion(-) diff --git a/gh-branch b/gh-branch index ecb69ea..8650308 100755 --- a/gh-branch +++ b/gh-branch @@ -68,7 +68,6 @@ list_prs() { } state headRefName - body } } } From e553d01c299e54ab6ad87f18952b728a87aaadb2 Mon Sep 17 00:00:00 2001 From: Nathan Hyland Date: Tue, 5 Oct 2021 14:44:33 -0700 Subject: [PATCH 3/3] shorter flag --- gh-branch | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gh-branch b/gh-branch index 8650308..f841687 100755 --- a/gh-branch +++ b/gh-branch @@ -131,7 +131,7 @@ choose() { --ansi <<<"$rendered" elif [ -n "$diff" ]; then fzf \ - --preview="echo {5} | cut -c2- | xargs gh pr diff | if type -p bat &>/dev/null; then bat --style=plain --color=always; else cat; fi" \ + --preview="echo {5} | cut -c2- | xargs gh pr diff | if type -p bat &>/dev/null; then bat -p -l diff --color=always; else cat; fi" \ --header " scroll preview down/up, scroll next/prev PR" \ --preview-window 'up,60%,border-bottom,+{2}+3/3,~3', \ --bind ctrl-d:preview-down,ctrl-u:preview-up \