Skip to content

Commit

Permalink
feat(workdir): support CWD outside of Git repos
Browse files Browse the repository at this point in the history
# Conflicts:
#	git-prompt-kit.zsh.zwc
  • Loading branch information
olets committed Jan 17, 2022
1 parent 878348f commit 8de45aa
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 39 deletions.
20 changes: 10 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,16 +133,14 @@ Name | Type | Description | Default

Name | Type | Description | Default
---|---|---|---
`GIT_PROMPT_KIT_CWD_TRAILING_COUNT` | integer | The maximum number of trailing path components in the current working directory component. When in a Git repository, this is relative to the Git root
`GIT_PROMPT_KIT_DEFAULT_PUSH_REMOTE_NAME` | string | The default Git push remote | `upstream`
`GIT_PROMPT_KIT_DEFAULT_REMOTE_NAME` | string | The default Git remote | `origin`
`GIT_PROMPT_KIT_HIDDEN_HOSTS` | array | The hosts that will not be included in the prompt | `()`
`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_ROOT_TRAILING_COUNT` | integer | The maximum number of trailing path components in the Git root directory component * | `1`
`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_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 *

\* Zero means the whole path, -1 means no trailing components. See https://zsh.sourceforge.io/Doc/Release/Prompt-Expansion.html

### Layout options

Expand All @@ -163,7 +161,7 @@ Name | Type | Description | Default
`GIT_PROMPT_KIT_SYMBOL_ASSUME_UNCHANGED` | string | Follows the Git assume-unchanged segment | ``
`GIT_PROMPT_KIT_SYMBOL_BEHIND` | string | Precedes the Git commits-behind segment | `-`
`GIT_PROMPT_KIT_SYMBOL_BRANCH` | string | Precedes the Git branch | none
`GIT_PROMPT_KIT_SYMBOL_CHAR_NORMAL` | string | Character shown at end of prompt for normal users | `%%` **
`GIT_PROMPT_KIT_SYMBOL_CHAR_NORMAL` | string | Character shown at end of prompt for normal users | `%%` *
`GIT_PROMPT_KIT_SYMBOL_CHAR_ROOT` | string | Character shown at end of prompt for root users | `#`
`GIT_PROMPT_KIT_SYMBOL_COMMIT` | string | Precedes the Git commit | none
`GIT_PROMPT_KIT_SYMBOL_CONFLICTED` | string | Follows the Git conflicted files segment | `UU`
Expand All @@ -180,7 +178,7 @@ Name | Type | Description | Default
`GIT_PROMPT_KIT_SYMBOL_TAG` | string | Precedes the Git tag | `@`
`GIT_PROMPT_KIT_SYMBOL_UNTRACKED` | string | Follows Git untracked file segment | `??`

\** `%%` expands as `%` in the zsh prompt.
\* `%%` expands as `%` in the zsh prompt.

## Components

Expand Down Expand Up @@ -213,26 +211,27 @@ Name | Type | Description
---|---|---
`GIT_PROMPT_KIT_ACTION` | prompt string | Git: current action (e.g. "rebase")
`GIT_PROMPT_KIT_AHEAD` | prompt string | Git: commits ahead of the upstream
`GIT_PROMPT_KIT_PUSH_AHEAD` | prompt string | Git: commits ahead of the push remote
`GIT_PROMPT_KIT_ASSUMED_UNCHANGED` | prompt string | Git: assume-unchanged files count
`GIT_PROMPT_KIT_BEHIND` | prompt string | Git: commits behind the upstream
`GIT_PROMPT_KIT_PUSH_BEHIND` | prompt string | Git: commits behind the push remote
`GIT_PROMPT_KIT_CHAR` | prompt string | Prompt character
`GIT_PROMPT_KIT_CONFLICTED` | prompt string | Git: conflicted files count
`GIT_PROMPT_KIT_CWD` | prompt string | Current working directory with trailing directories. If in a Git repo, relative to the Git root.
`GIT_PROMPT_KIT_DELETED_STAGED` | prompt string | Git: staged deleted files count
`GIT_PROMPT_KIT_DELETED` | prompt string | Git: unstaged deleted files count
`GIT_PROMPT_KIT_HEAD` | prompt string | Git: HEAD (branch or commit)
`GIT_PROMPT_KIT_MODIFIED_STAGED` | prompt string | Git: staged modified files count
`GIT_PROMPT_KIT_MODIFIED` | prompt string | Git: unstaged modified files count
`GIT_PROMPT_KIT_NEW` | prompt string | Git: (staged) new files count
`GIT_PROMPT_KIT_PUSH_AHEAD` | prompt string | Git: commits ahead of the push remote
`GIT_PROMPT_KIT_PUSH_BEHIND` | prompt string | Git: commits behind the push remote
`GIT_PROMPT_KIT_PUSH` | prompt string | Git: push remote if not the default
`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_ROOT` | prompt string | Git root directory, underlined, with trailing directories.
`GIT_PROMPT_KIT_SKIP_WORKTREE` | prompt string | Git: skip-worktree files count
`GIT_PROMPT_KIT_STASHES` | prompt string | Git: stash count
`GIT_PROMPT_KIT_TAG` | prompt string | Git: up to one tag at HEAD
`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 All @@ -241,6 +240,7 @@ Name | Type | Description
`GIT_PROMPT_KIT_REF` | prompt string | `GIT_PROMPT_KIT_HEAD`, `GIT_PROMPT_KIT_AHEAD`, `GIT_PROMPT_KIT_BEHIND`, `GIT_PROMPT_KIT_REMOTE`, `GIT_PROMPT_KIT_PUSH`, and `GIT_PROMPT_KIT_TAG`
`GIT_PROMPT_KIT_STATUS_EXTENDED` | prompt string | `GIT_PROMPT_KIT_STASHES`, `GIT_PROMPT_KIT_ASSUMED_UNCHANGED`, and `GIT_PROMPT_KIT_SKIP_WORKTREE`
`GIT_PROMPT_KIT_STATUS` | prompt string | `GIT_PROMPT_KIT_UNTRACKED`, `GIT_PROMPT_KIT_CONFLICTED`, `GIT_PROMPT_KIT_DELETED`, `GIT_PROMPT_KIT_MODIFIED`, `GIT_PROMPT_KIT_NEW`, `GIT_PROMPT_KIT_DELETED_STAGED`, and `GIT_PROMPT_KIT_MODIFIED_STAGED`
`GIT_PROMPT_KIT_WORKDIR` | prompt string | `GIT_PROMPT_KIT_ROOT` and `GIT_PROMPT_KIT_CWD`

### Other components

Expand Down
68 changes: 39 additions & 29 deletions git-prompt-kit.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,8 @@ typeset -g GIT_PROMPT_KIT_GITSTATUSD_INSTANCE_NAME=${GIT_PROMPT_KIT_GITSTATUSD_I
# Content options
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}
GIT_PROMPT_KIT_CWD_TRAILING_COUNT=${GIT_PROMPT_KIT_CWD_TRAILING_COUNT-1}
GIT_PROMPT_KIT_ROOT_TRAILING_COUNT=${GIT_PROMPT_KIT_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 @@ -201,7 +200,7 @@ _git_prompt_kit_update_git() {
typeset -g GIT_PROMPT_KIT_STATUS_EXTENDED=
typeset -g GIT_PROMPT_KIT_TAG=
typeset -g GIT_PROMPT_KIT_UNTRACKED=
typeset -g GIT_PROMPT_KIT_WORKDIR=
typeset -g GIT_PROMPT_KIT_ROOT=

(( GIT_PROMPT_KIT_LINEBREAK_AFTER_GIT_REF )) || typeset -g GIT_PROMPT_KIT_LINEBREAK_AFTER_GIT_REF=
(( GIT_PROMPT_KIT_NO_LINEBREAK_BEFORE_GIT_REF )) || typeset -g GIT_PROMPT_KIT_NO_LINEBREAK_BEFORE_GIT_REF=
Expand All @@ -214,7 +213,6 @@ _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
Expand Down Expand Up @@ -253,33 +251,16 @@ _git_prompt_kit_update_git() {

# Git directory

GIT_PROMPT_KIT_WORKDIR+="%F{$GIT_PROMPT_KIT_COLOR_WORKDIR}"

GIT_PROMPT_KIT_ROOT+="%F{$GIT_PROMPT_KIT_COLOR_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

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
if (( GIT_PROMPT_KIT_ROOT_TRAILING_COUNT + 1 >= ${#root_path_components} )); then
GIT_PROMPT_KIT_ROOT+=${(j./.)root_path_components[0,-2]}
else
GIT_PROMPT_KIT_ROOT+=${(j./.)root_path_components[$(( -1 - GIT_PROMPT_KIT_ROOT_TRAILING_COUNT )),-2]}
fi

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

# Git tree status: stashes

Expand Down Expand Up @@ -561,10 +542,13 @@ _git_prompt_kit_update_nongit() {
emulate -L zsh

typeset -g GIT_PROMPT_KIT_CHAR=
typeset -g GIT_PROMPT_KIT_CWD=
typeset -g GIT_PROMPT_KIT_USERHOST=
typeset -g GIT_PROMPT_KIT_WORKDIR=

local host=${(%):-%m}
local user=${(%):-%n}
local -a cwd_path_components
local -i show_host
local -i show_user

Expand All @@ -586,6 +570,32 @@ _git_prompt_kit_update_nongit() {
(( show_user )) && GIT_PROMPT_KIT_USERHOST+="%F{$GIT_PROMPT_KIT_COLOR_USER}%n%f"
(( show_host )) && GIT_PROMPT_KIT_USERHOST+="%F{$GIT_PROMPT_KIT_COLOR_HOST}${GIT_PROMPT_KIT_SYMBOL_HOST}%m%f"
fi

GIT_PROMPT_KIT_CWD="%F{$GIT_PROMPT_KIT_COLOR_WORKDIR}%$(( GIT_PROMPT_KIT_CWD_TRAILING_COUNT + 1 ))~%f"

if [[ -n $GIT_PROMPT_KIT_ROOT ]]; then
GIT_PROMPT_KIT_CWD=

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

GIT_PROMPT_KIT_CWD+="%F{$GIT_PROMPT_KIT_COLOR_WORKDIR}"

if (( GIT_PROMPT_KIT_CWD_TRAILING_COUNT + 1 >= ${#cwd_path_components} )); then
GIT_PROMPT_KIT_CWD+=${(j./.)cwd_path_components[0,-1]}
else
GIT_PROMPT_KIT_CWD+=.../${(j./.)cwd_path_components[$(( -1 - GIT_PROMPT_KIT_CWD_TRAILING_COUNT )),-1]}
fi

GIT_PROMPT_KIT_CWD+="%F{$GIT_PROMPT_KIT_COLOR_INACTIVE}"
fi
fi

GIT_PROMPT_KIT_WORKDIR+=$GIT_PROMPT_KIT_ROOT
if [[ -n $GIT_PROMPT_KIT_ROOT && -n $GIT_PROMPT_KIT_CWD ]]; then
GIT_PROMPT_KIT_WORKDIR+="%F{$GIT_PROMPT_KIT_COLOR_WORKDIR}/%F{$GIT_PROMPT_KIT_COLOR_INACTIVE}"
fi
GIT_PROMPT_KIT_WORKDIR+=$GIT_PROMPT_KIT_CWD
}

_git_prompt_kit_no_color() {
Expand Down
Binary file modified git-prompt-kit.zsh.zwc
Binary file not shown.

0 comments on commit 8de45aa

Please sign in to comment.