diff --git a/README.md b/README.md index 7e84317..8b9fe2a 100644 --- a/README.md +++ b/README.md @@ -215,13 +215,14 @@ Git Radar is highly customisable using a prompt format string. The 4 features above: remote commits, local commits, branch and file changes; are controlled by the prompt format string. -Feature | Control string ----------------|--------------- -Remote commits | `%{remote}` -Local commits | `%{local}` -Branch | `%{branch}` -File changes | `%{changes}` -Stashes | `%{stash}` +Feature | Control string +---------------------------|--------------- +Remote commits | `%{remote}` +Local commits | `%{local}` +Branch | `%{branch}` +File changes | `%{changes}` +Stashes | `%{stash}` +Relative path in the repo | `%{git_path}` You can create any prompt shape you prefer by exporting `GIT_RADAR_FORMAT` with your preferred shape. The control strings above will be replaced with the output diff --git a/radar-base.sh b/radar-base.sh index 517a6ce..65b90d0 100755 --- a/radar-base.sh +++ b/radar-base.sh @@ -543,6 +543,10 @@ stash_status() { fi } +git_path() { + printf '%s' "$(basename `git rev-parse --show-toplevel`)/$(git rev-parse --show-prefix)" +} + render_prompt() { output="$PROMPT_FORMAT" branch_sed="" @@ -550,7 +554,7 @@ render_prompt() { local_sed="" changes_sed="" stash_sed="" - + git_path_sed="" if_pre="%\{([^%{}]{1,}:){0,1}" if_post="(:[^%{}]{1,}){0,1}\}" @@ -597,11 +601,20 @@ render_prompt() { stash_sed="s/${sed_pre}stash${sed_post}//" fi fi + if [[ $PROMPT_FORMAT =~ ${if_pre}git_path${if_post} ]]; then + git_path_result="$(git_path | sed -e 's/[\/&]/\\&/g')" + if [[ -n "git_path_result" ]]; then + git_path_sed="s/${sed_pre}git_path${sed_post}/\2${git_path_result}\4/" + else + git_path_sed="s/${sed_pre}git_path${sed_post}//" + fi + fi printf '%b' "$output" | sed \ -e "$remote_sed" \ -e "$branch_sed" \ -e "$changes_sed" \ -e "$local_sed" \ - -e "$stash_sed" + -e "$stash_sed" \ + -e "$git_path_sed" }