From 50a69d7d26ab0c56e2ff6180428e285649abcb1b Mon Sep 17 00:00:00 2001 From: Xue Qianming Date: Sun, 14 Jun 2020 17:45:40 +0800 Subject: [PATCH] Add: allow customizing key-bindings --- lib/modules/branch.sh | 22 ++++++++++++++-------- lib/modules/log.sh | 16 ++++++++++------ lib/modules/reflog.sh | 16 ++++++++++------ lib/modules/status.sh | 24 +++++++++++++++--------- 4 files changed, 49 insertions(+), 29 deletions(-) diff --git a/lib/modules/branch.sh b/lib/modules/branch.sh index 98139be..cc30857 100644 --- a/lib/modules/branch.sh +++ b/lib/modules/branch.sh @@ -1,5 +1,11 @@ #!/usr/bin/env bash +GIT_FUZZY_BRANCH_WORKING_COPY_KEY=${GIT_FUZZY_BRANCH_WORKING_COPY_KEY:-Ctrl-P} +GIT_FUZZY_BRANCH_MERGE_BASE_KEY=${GIT_FUZZY_BRANCH_MERGE_BASE_KEY:-Alt-P} +GIT_FUZZY_BRANCH_COMMIT_LOG_KEY=${GIT_FUZZY_BRANCH_COMMIT_LOG_KEY:-Alt-L} +GIT_FUZZY_BRANCH_CHECKOUT_KEY=${GIT_FUZZY_BRANCH_CHECKOUT_KEY:-Alt-F} +GIT_FUZZY_BRANCH_DELETE_BRANCH_KEY=${GIT_FUZZY_BRANCH_DELETE_BRANCH_KEY:-Alt-D} + GF_BRANCH_RELOAD="reload(git fuzzy helper branch_menu_content)" GF_BRANCH_CHECKOUT="git fuzzy helper branch_checkout {1}" @@ -21,9 +27,9 @@ gf_fzf_branch() { BRANCH_HEADER=' Type to filter. '"${WHITE}Enter${NORMAL} to ${GREEN}ACCEPT${NORMAL}"' - '"${YELLOW}${BOLD}∆${NORMAL} ${GREEN}working copy${NORMAL} ${WHITE}Ctrl-P${NORMAL} $BRANCH_HEADER_BRANCH_CHECKOUT"' - '"${YELLOW}${BOLD}∆${NORMAL} ${GREEN}merge-base${NORMAL} ${WHITE}Alt-P${NORMAL} ${GREEN}${BOLD}checkout ${YELLOW}${BOLD}📁${NORMAL} ${WHITE}Alt-F${NORMAL}"' - '"${GREEN}commit log${NORMAL} ${WHITE}Alt-L${NORMAL} ${RED}${BOLD}delete branch ✗${NORMAL} ${WHITE}Alt-D${NORMAL}"' + '"${YELLOW}${BOLD}∆${NORMAL} ${GREEN}working copy${NORMAL} ${WHITE}$GIT_FUZZY_BRANCH_WORKING_COPY_KEY${NORMAL} $BRANCH_HEADER_BRANCH_CHECKOUT"' + '"${YELLOW}${BOLD}∆${NORMAL} ${GREEN}merge-base${NORMAL} ${WHITE}$GIT_FUZZY_BRANCH_MERGE_BASE_KEY${NORMAL} ${GREEN}${BOLD}checkout ${YELLOW}${BOLD}📁${NORMAL} ${WHITE}$GIT_FUZZY_BRANCH_CHECKOUT_KEY${NORMAL}"' + '"${GREEN}commit log${NORMAL} ${WHITE}$GIT_FUZZY_BRANCH_COMMIT_LOG_KEY${NORMAL} ${RED}${BOLD}delete branch ✗${NORMAL} ${WHITE}$GIT_FUZZY_BRANCH_DELETE_BRANCH_KEY${NORMAL}"' ' @@ -31,11 +37,11 @@ Type to filter. '"${WHITE}Enter${NORMAL} to ${GREEN}ACCEPT${NORMAL}"' gf_fzf_one -m \ --header "$BRANCH_HEADER" \ $BRANCH_CHECKOUT_BINDING \ - --bind 'alt-f:execute(git fuzzy helper branch_checkout_files {1})' \ - --bind "alt-d:$GF_BRANCH_DELETE_BINDING" \ - --bind 'alt-l:execute(git fuzzy log {1})' \ - --bind 'ctrl-p:execute(git fuzzy diff {1})' \ - --bind 'alt-p:execute(git fuzzy diff "$(git merge-base "'"$GF_BASE_BRANCH"'" {1})" {1})' \ + --bind $GIT_FUZZY_BRANCH_CHECKOUT_KEY':execute(git fuzzy helper branch_checkout_files {1})' \ + --bind "$GIT_FUZZY_BRANCH_DELETE_BRANCH_KEY:$GF_BRANCH_DELETE_BINDING" \ + --bind $GIT_FUZZY_BRANCH_COMMIT_LOG_KEY':execute(git fuzzy log {1})' \ + --bind $GIT_FUZZY_BRANCH_WORKING_COPY_KEY':execute(git fuzzy diff {1})' \ + --bind $GIT_FUZZY_BRANCH_MERGE_BASE_KEY':execute(git fuzzy diff "$(git merge-base "'"$GF_BASE_BRANCH"'" {1})" {1})' \ --preview 'git fuzzy helper branch_preview_content {1}' | \ awk '{ print $1 }' } diff --git a/lib/modules/log.sh b/lib/modules/log.sh index ed066a2..b7db654 100644 --- a/lib/modules/log.sh +++ b/lib/modules/log.sh @@ -1,12 +1,16 @@ #!/usr/bin/env bash +GIT_FUZZY_LOG_WORKING_COPY_KEY=${GIT_FUZZY_LOG_WORKING_COPY_KEY:-Ctrl-P} +GIT_FUZZY_MERGE_BASE_KEY=${GIT_FUZZY_MERGE_BASE_KEY:-Alt-P} +GIT_FUZZY_COMMIT_KEY=${GIT_FUZZY_COMMIT_KEY:-Alt-D} + # shellcheck disable=2016 GF_LOG_HEADER=' Use '"${YELLOW}|${NORMAL} to separate CLI args for ${MAGENTA}git log${NORMAL} vs ${MAGENTA}git diff${NORMAL}. ${WHITE}Enter${NORMAL} to ${GREEN}ACCEPT${NORMAL}"' - '"${YELLOW}${BOLD}∆${NORMAL} ${GREEN}working copy${NORMAL} ${WHITE}Ctrl-P${NORMAL} ${GRAY}-- search messages${NORMAL} ${MAGENTA}--grep=Foo${NORMAL}"' - '"${YELLOW}${BOLD}∆${NORMAL} ${GREEN}merge-base${NORMAL} ${WHITE}Alt-P${NORMAL} ${GRAY}-- search patch${NORMAL} ${MAGENTA}-G 'Foo'${NORMAL}"' - '"${YELLOW}${BOLD}∆${NORMAL} ${GREEN}commit${NORMAL} ${WHITE}Alt-D${NORMAL} ${GRAY}-- customize patch${NORMAL} ${MAGENTA}-G 'Foo' | -W -- foo.c${NORMAL}"' + '"${YELLOW}${BOLD}∆${NORMAL} ${GREEN}working copy${NORMAL} ${WHITE}$GIT_FUZZY_LOG_WORKING_COPY_KEY${NORMAL} ${GRAY}-- search messages${NORMAL} ${MAGENTA}--grep=Foo${NORMAL}"' + '"${YELLOW}${BOLD}∆${NORMAL} ${GREEN}merge-base${NORMAL} ${WHITE}$GIT_FUZZY_MERGE_BASE_KEY${NORMAL} ${GRAY}-- search patch${NORMAL} ${MAGENTA}-G 'Foo'${NORMAL}"' + '"${YELLOW}${BOLD}∆${NORMAL} ${GREEN}commit${NORMAL} ${WHITE}$GIT_FUZZY_COMMIT_KEY${NORMAL} ${GRAY}-- customize patch${NORMAL} ${MAGENTA}-G 'Foo' | -W -- foo.c${NORMAL}"' ' @@ -18,9 +22,9 @@ gf_fzf_log() { --header "$GF_LOG_HEADER" \ --preview 'git fuzzy helper log_preview_content {1} {q}' \ --bind 'change:reload(git fuzzy helper log_menu_content {q})' \ - --bind 'alt-d:execute(git fuzzy diff {1}^ {1})' \ - --bind 'ctrl-p:execute(git fuzzy diff {1})' \ - --bind 'alt-p:execute(git fuzzy diff "$(git merge-base "'"$GF_BASE_BRANCH"'" {1})" {1})' + --bind $GIT_FUZZY_COMMIT_KEY':execute(git fuzzy diff {1}^ {1})' \ + --bind $GIT_FUZZY_LOG_WORKING_COPY_KEY':execute(git fuzzy diff {1})' \ + --bind $GIT_FUZZY_MERGE_BASE_KEY':execute(git fuzzy diff "$(git merge-base "'"$GF_BASE_BRANCH"'" {1})" {1})' } gf_log() { diff --git a/lib/modules/reflog.sh b/lib/modules/reflog.sh index e6c43f8..a57c01d 100644 --- a/lib/modules/reflog.sh +++ b/lib/modules/reflog.sh @@ -1,12 +1,16 @@ #!/usr/bin/env bash +GIT_FUZZY_REFLOG_WORKING_COPY_KEY=${GIT_FUZZY_REFLOG_WORKING_COPY_KEY:-Ctrl-P} +GIT_FUZZY_REFLOG_MERGE_BASE_KEY=${GIT_FUZZY_REFLOG_MERGE_BASE_KEY:-Alt-P} +GIT_FUZZY_REFLOG_COMMIT_KEY=${GIT_FUZZY_REFLOG_COMMIT_KEY:-Alt-D} + # shellcheck disable=2016 GF_REFLOG_HEADER=' Use '"${YELLOW}|${NORMAL} to separate CLI args for ${MAGENTA}git reflog${NORMAL} vs ${MAGENTA}git diff${NORMAL}. ${WHITE}Enter${NORMAL} to ${GREEN}ACCEPT${NORMAL}"' - '"${YELLOW}${BOLD}∆${NORMAL} ${GREEN}working copy${NORMAL} ${WHITE}Ctrl-P${NORMAL} ${GRAY}-- search messages${NORMAL} ${MAGENTA}--grep=Foo${NORMAL}"' - '"${YELLOW}${BOLD}∆${NORMAL} ${GREEN}merge-base${NORMAL} ${WHITE}Alt-P${NORMAL} ${GRAY}-- search patch${NORMAL} ${MAGENTA}-G 'Foo'${NORMAL}"' - '"${YELLOW}${BOLD}∆${NORMAL} ${GREEN}commit${NORMAL} ${WHITE}Alt-C${NORMAL} ${GRAY}-- customize patch${NORMAL} ${MAGENTA}-G 'Foo' | -W -- foo.c${NORMAL}"' + '"${YELLOW}${BOLD}∆${NORMAL} ${GREEN}working copy${NORMAL} ${WHITE}$GIT_FUZZY_REFLOG_WORKING_COPY_KEY${NORMAL} ${GRAY}-- search messages${NORMAL} ${MAGENTA}--grep=Foo${NORMAL}"' + '"${YELLOW}${BOLD}∆${NORMAL} ${GREEN}merge-base${NORMAL} ${WHITE}$GIT_FUZZY_REFLOG_MERGE_BASE_KEY${NORMAL} ${GRAY}-- search patch${NORMAL} ${MAGENTA}-G 'Foo'${NORMAL}"' + '"${YELLOW}${BOLD}∆${NORMAL} ${GREEN}commit${NORMAL} ${WHITE}$GIT_FUZZY_REFLOG_COMMIT_KEY${NORMAL} ${GRAY}-- customize patch${NORMAL} ${MAGENTA}-G 'Foo' | -W -- foo.c${NORMAL}"' ' @@ -18,9 +22,9 @@ gf_fzf_reflog() { --header "$GF_REFLOG_HEADER" \ --preview 'git fuzzy helper reflog_preview_content {1} {q}' \ --bind 'change:reload(git fuzzy helper reflog_menu_content {q})' \ - --bind 'alt-d:execute(git fuzzy diff {1}^ {1})' \ - --bind 'ctrl-p:execute(git fuzzy diff {1})' \ - --bind 'alt-p:execute(git fuzzy diff "$(git merge-base "'"$GF_BASE_BRANCH"'" {1})" {1})' + --bind $GIT_FUZZY_REFLOG_COMMIT_KEY':execute(git fuzzy diff {1}^ {1})' \ + --bind $GIT_FUZZY_REFLOG_WORKING_COPY_KEY':execute(git fuzzy diff {1})' \ + --bind $GIT_FUZZY_REFLOG_MERGE_BASE_KEY':execute(git fuzzy diff "$(git merge-base "'"$GF_BASE_BRANCH"'" {1})" {1})' } gf_reflog() { diff --git a/lib/modules/status.sh b/lib/modules/status.sh index 13e9d0b..eccd13a 100644 --- a/lib/modules/status.sh +++ b/lib/modules/status.sh @@ -1,13 +1,19 @@ #!/usr/bin/env bash # shellcheck disable=2016 +GIT_FUZZY_STATUS_ADD_KEY=${GIT_FUZZY_STATUS_ADD_KEY:-Alt-S} +GIT_FUZZY_STATUS_EDIT_KEY=${GIT_FUZZY_STATUS_EDIT_KEY:-Alt-E} +GIT_FUZZY_STATUS_COMMIT_KEY=${GIT_FUZZY_STATUS_EDIT_KEY:-Alt-C} +GIT_FUZZY_STATUS_RESET_KEY=${GIT_FUZZY_STATUS_RESET_KEY:-Alt-R} +GIT_FUZZY_STATUS_DISCARD_KEY=${GIT_FUZZY_STATUS_DISCARD_KEY:-Alt-U} + GF_STATUS_HEADER=' Type to filter. '"${WHITE}Enter${NORMAL} to ${GREEN}ACCEPT${NORMAL}"' '"${GRAY}-- (${NORMAL}*${GRAY}) editor: ${MAGENTA}${EDITOR} ${NORMAL}${GF_EDITOR_ARGS}${NORMAL}"' - '" * ${GREEN}${BOLD}edit ✎${NORMAL} ${WHITE}Alt-E${NORMAL}"' - '"${GREEN}all ☑${NORMAL} ${WHITE}Alt-A${NORMAL} ${GREEN}stage ${BOLD}⇡${NORMAL} ${WHITE}Alt-S${NORMAL} ${RED}${BOLD}discard ✗${NORMAL} ${WHITE}Alt-U${NORMAL}"' - '"${GREEN}none ☐${NORMAL} ${WHITE}Alt-D${NORMAL} ${GREEN}reset ${RED}${BOLD}⇣${NORMAL} ${WHITE}Alt-R${NORMAL} * ${RED}${BOLD}commit ${NORMAL}${RED}⇧${NORMAL} ${WHITE}Alt-C${NORMAL}"' + '" * ${GREEN}${BOLD}edit ✎${NORMAL} ${WHITE}$GIT_FUZZY_STATUS_EDIT_KEY${NORMAL}"' + '"${GREEN}all ☑${NORMAL} ${WHITE}Alt-A${NORMAL} ${GREEN}stage ${BOLD}⇡${NORMAL} ${WHITE}$GIT_FUZZY_STATUS_ADD_KEY${NORMAL} ${RED}${BOLD}discard ✗${NORMAL} ${WHITE}$GIT_FUZZY_STATUS_DISCARD_KEY${NORMAL}"' + '"${GREEN}none ☐${NORMAL} ${WHITE}Alt-D${NORMAL} ${GREEN}reset ${RED}${BOLD}⇣${NORMAL} ${WHITE}$GIT_FUZZY_STATUS_RESET_KEY${NORMAL} * ${RED}${BOLD}commit ${NORMAL}${RED}⇧${NORMAL} ${WHITE}$GIT_FUZZY_STATUS_COMMIT_KEY${NORMAL}"' ' gf_fzf_status() { @@ -16,21 +22,21 @@ gf_fzf_status() { gf_fzf -m --header "$GF_STATUS_HEADER" \ --header-lines=2 \ - --expect='alt-e,alt-c' \ + --expect='$GIT_FUZZY_STATUS_EDIT_KEY,$GIT_FUZZY_STATUS_COMMIT_KEY' \ --nth=2 \ --preview 'git fuzzy helper status_preview_content {1} {2..}' \ - --bind "alt-s:execute-silent(git fuzzy helper status_add {+2..})+down+$RELOAD" \ - --bind "alt-r:execute-silent(git fuzzy helper status_reset {+2..})+down+$RELOAD" \ - --bind "alt-u:execute-silent(git fuzzy helper status_discard {2..})+$RELOAD" + --bind "$GIT_FUZZY_STATUS_ADD_KEY:execute-silent(git fuzzy helper status_add {+2..})+down+$RELOAD" \ + --bind "$GIT_FUZZY_STATUS_RESET_KEY:execute-silent(git fuzzy helper status_reset {+2..})+down+$RELOAD" \ + --bind "$GIT_FUZZY_STATUS_DISCARD_KEY:execute-silent(git fuzzy helper status_discard {2..})+$RELOAD" } gf_status_interpreter() { CONTENT="$(cat -)" HEAD="$(echo "$CONTENT" | head -n1)" TAIL="$(echo "$CONTENT" | tail -n +2)" - if [ "$HEAD" = 'alt-e' ]; then + if [ "$HEAD" = "$GIT_FUZZY_STATUS_EDIT_KEY" ]; then eval "git fuzzy helper status_edit $(echo "$TAIL" | cut -c4- | join_lines_quoted)" - elif [ "$HEAD" = 'alt-c' ]; then + elif [ "$HEAD" = "$GIT_FUZZY_STATUS_COMMIT_KEY" ]; then eval "git fuzzy helper status_commit" else echo "$TAIL" | cut -c4-