Skip to content

Commit

Permalink
feat(workdir): root dir, trailing components; cwd, trailing components
Browse files Browse the repository at this point in the history
# Conflicts:
#	git-prompt-kit.zsh
#	git-prompt-kit.zsh.zwc
  • Loading branch information
olets committed Jan 17, 2022
1 parent 1e720a0 commit 878348f
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 14 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,10 @@ Name | Type | Description | Default
`GIT_PROMPT_KIT_HIDDEN_USERS` | array | The users that will not be included in the prompt | `()`
`GIT_PROMPT_KIT_LOCAL` | string | Shown if the checked-out branch has no upstream | `local`
`GIT_PROMPT_KIT_SHOW_EXTENDED_STATUS` | number | Show the stash, assume-unchanged, and skip-worktree counts (YES if non-zero, NO if zero) | `1`
`GIT_PROMPT_KIT_WORKDIR_DEPTH` | integer | The number of path components shown in the working directory component
`GIT_PROMPT_KIT_WORKDIR_CWD_TRAILING_COUNT` | integer | The number of path components trailing the Git root directory in the working directory component *
`GIT_PROMPT_KIT_WORKDIR_ROOT_TRAILING_COUNT` | integer | When in a subdirectory of the Git repo, the number of path components trailing the current working directory in the working directory component *

\* For the special sequences supported in zsh prompts see http://zsh.sourceforge.net/Doc/Release/Prompt-Expansion.html
\* Zero means the whole path, -1 means no trailing components. See https://zsh.sourceforge.io/Doc/Release/Prompt-Expansion.html

### Layout options

Expand Down Expand Up @@ -231,6 +232,7 @@ Name | Type | Description
`GIT_PROMPT_KIT_UNTRACKED` | prompt string | Git: untracked (not staged) files count
`GIT_PROMPT_KIT_REMOTE` | prompt string | Git: "local" if no upstream; upstream branch if the name differs from the local branch; upstream remote and branch if the remote is not the default
`GIT_PROMPT_KIT_USERHOST` | prompt string | User (if not configured as hidden) and host (if not configured as hidden)
`GIT_PROMPT_KIT_WORKDIR` | prompt string | Git root directory with trailing directories. If in a subdirectory, the current working directory with trailing directories.

### Molecule components

Expand Down
39 changes: 27 additions & 12 deletions git-prompt-kit.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ typeset -g GIT_PROMPT_KIT_GITSTATUSD_INSTANCE_NAME=${GIT_PROMPT_KIT_GITSTATUSD_I
GIT_PROMPT_KIT_DEFAULT_PUSH_REMOTE_NAME=${GIT_PROMPT_KIT_DEFAULT_PUSH_REMOTE_NAME-upstream}
GIT_PROMPT_KIT_DEFAULT_REMOTE_NAME=${GIT_PROMPT_KIT_DEFAULT_REMOTE_NAME-origin}
GIT_PROMPT_KIT_WORKDIR_DEPTH=${GIT_PROMPT_KIT_WORKDIR_DEPTH-2}
GIT_PROMPT_KIT_WORKDIR_CWD_TRAILING_COUNT=${GIT_PROMPT_KIT_WORKDIR_CWD_TRAILING_COUNT-1}
GIT_PROMPT_KIT_WORKDIR_ROOT_TRAILING_COUNT=${GIT_PROMPT_KIT_WORKDIR_ROOT_TRAILING_COUNT-1}
#
! [[ -v GIT_PROMPT_KIT_HIDDEN_HOSTS ]] && typeset -a GIT_PROMPT_KIT_HIDDEN_HOSTS=()
! [[ -v GIT_PROMPT_KIT_HIDDEN_USERS ]] && typeset -a GIT_PROMPT_KIT_HIDDEN_USERS=()
Expand Down Expand Up @@ -126,7 +128,6 @@ _git_prompt_kit_configs=(
GIT_PROMPT_KIT_SYMBOL_STASH
GIT_PROMPT_KIT_SYMBOL_TAG
GIT_PROMPT_KIT_SYMBOL_UNTRACKED
GIT_PROMPT_KIT_WORKDIR_DEPTH
)

typeset -ga _git_prompt_kit_colors
Expand Down Expand Up @@ -213,6 +214,8 @@ _git_prompt_kit_update_git() {
local action_status=
local ref_status=
local tree_status=
local -a cwd_path_components
local -a root_path_components
local -i added_staged_count
local -i show_ahead
local -i show_behind
Expand All @@ -221,7 +224,6 @@ _git_prompt_kit_update_git() {
local -i show_push_remote
local -i show_remote
local -i show_remote_branch
local -i subpath_depth
local -i triangular_workflow
local -i unstaged_count

Expand Down Expand Up @@ -251,19 +253,32 @@ _git_prompt_kit_update_git() {

# Git directory

subpath_depth=${#${(s./.)${PWD#$VCS_STATUS_WORKDIR}}}
GIT_PROMPT_KIT_WORKDIR+="%F{$GIT_PROMPT_KIT_COLOR_WORKDIR}"
# If PWD is deep enough inside the repo that the repo name does not show in the trimmed PWD,
# prefix the trimmed PWD with the "<repo name>/".
if (( $subpath_depth >= $GIT_PROMPT_KIT_WORKDIR_DEPTH )); then
GIT_PROMPT_KIT_WORKDIR+="${VCS_STATUS_WORKDIR##*/}/"

root_path_components=( ${(s./.)VCS_STATUS_WORKDIR} )

if (( GIT_PROMPT_KIT_WORKDIR_ROOT_TRAILING_COUNT == 0 )) || (( GIT_PROMPT_KIT_WORKDIR_ROOT_TRAILING_COUNT == -1 )); then
GIT_PROMPT_KIT_WORKDIR+=${(j./.)root_path_components[$GIT_PROMPT_KIT_WORKDIR_ROOT_TRAILING_COUNT,-2]}
else
GIT_PROMPT_KIT_WORKDIR+=${(j./.)root_path_components[$(( -1 - GIT_PROMPT_KIT_WORKDIR_ROOT_TRAILING_COUNT )),-2]}
fi
# If directories are ellided between the repo root and the trimmed PWD,
# add ".../" after the repo name
if (( $subpath_depth > $GIT_PROMPT_KIT_WORKDIR_DEPTH )); then
GIT_PROMPT_KIT_WORKDIR+=".../"

GIT_PROMPT_KIT_WORKDIR+="/%U${root_path_components[-1]}%u"

if [[ $VCS_STATUS_WORKDIR != $PWD ]]; then
cwd_path_components=( ${(s./.)PWD} )

GIT_PROMPT_KIT_WORKDIR+=/

if (( GIT_PROMPT_KIT_WORKDIR_CWD_TRAILING_COUNT >= ${#cwd_path_components} - ${#root_path_components} - 1 )); then
GIT_PROMPT_KIT_WORKDIR+=${(j./.)cwd_path_components[$(( ${#root_path_components} - ${#cwd_path_components} )),-1]}
elif (( GIT_PROMPT_KIT_WORKDIR_CWD_TRAILING_COUNT == 0 )) || (( GIT_PROMPT_KIT_WORKDIR_CWD_TRAILING_COUNT == -1 )); then
GIT_PROMPT_KIT_WORKDIR+=../${(j./.)cwd_path_components[$(( GIT_PROMPT_KIT_WORKDIR_CWD_TRAILING_COUNT )),-1]}
else
GIT_PROMPT_KIT_WORKDIR+=../${(j./.)cwd_path_components[$(( -1 - GIT_PROMPT_KIT_WORKDIR_CWD_TRAILING_COUNT )),-1]}
fi
fi
GIT_PROMPT_KIT_WORKDIR+="%$GIT_PROMPT_KIT_WORKDIR_DEPTH~"

GIT_PROMPT_KIT_WORKDIR+="%F{$GIT_PROMPT_KIT_COLOR_INACTIVE}"

# Git tree status: stashes
Expand Down

0 comments on commit 878348f

Please sign in to comment.