mirror of
https://github.com/robbyrussell/oh-my-zsh.git
synced 2026-12-02 21:33:04 +01:00
* Better placement of Fossil prompt info Instead of blindly tacking `$fossil_prompt_info` onto the end of the prompt string and making assumptions about whether `$PROMPT` or `$RPROMPT` are in use here, assume the user is using a theme that puts `$git_prompt_info` in there somewhere and inject our info immediately afterward. The theme creator decided that's a good place for Git prompt info, so that's a good place for Fossil prompt info, too. This also replaces some calls out to `grep` with internal Zsh string manipulation for less overhead. * fix(fossil): fall back to RPROMPT when the theme has no git_prompt_info Keeps today's behaviour for themes without git_prompt_info, and re-checks on each prompt in case the theme rebuilds its prompt. --------- Co-authored-by: Robby Russell <robby@planetargon.com> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
65 lines
2.1 KiB
Bash
65 lines
2.1 KiB
Bash
_FOSSIL_PROMPT=""
|
|
|
|
# Prefix at the very beginning of the prompt, before the branch name
|
|
ZSH_THEME_FOSSIL_PROMPT_PREFIX="%{$fg_bold[blue]%}fossil:(%{$fg_bold[red]%}"
|
|
|
|
# At the very end of the prompt
|
|
ZSH_THEME_FOSSIL_PROMPT_SUFFIX="%{$fg_bold[blue]%})"
|
|
|
|
# Text to display if the branch is dirty
|
|
ZSH_THEME_FOSSIL_PROMPT_DIRTY="%{$fg_bold[red]%}✖"
|
|
|
|
# Text to display if the branch is clean
|
|
ZSH_THEME_FOSSIL_PROMPT_CLEAN="%{$fg_bold[green]%}✔"
|
|
|
|
function fossil_prompt_info() {
|
|
local branch=$(fossil branch current 2>&1)
|
|
|
|
# if we're not in a fossil repo, don't show anything
|
|
! command grep -q "use --repo" <<< "$branch" || return
|
|
|
|
local changes=$(fossil changes)
|
|
local dirty="$ZSH_THEME_FOSSIL_PROMPT_CLEAN"
|
|
|
|
if [[ -n "$changes" ]]; then
|
|
dirty="$ZSH_THEME_FOSSIL_PROMPT_DIRTY"
|
|
fi
|
|
|
|
printf ' %s%s%s%s%s' \
|
|
"$ZSH_THEME_FOSSIL_PROMPT_PREFIX" \
|
|
"${branch:gs/%/%%}" \
|
|
"$dirty" \
|
|
"$ZSH_THEME_FOSSIL_PROMPT_SUFFIX" \
|
|
"%{$reset_color%}"
|
|
}
|
|
|
|
function _fossil_prompt () {
|
|
# Re-check each time, in case the theme rebuilds its prompt in precmd
|
|
if [ -z "$_FOSSIL_PROMPT" ] || [[ "$PROMPT$RPROMPT" != *fossil_prompt_info* ]]; then
|
|
# Use substitutions to work out what other plugins and themes have
|
|
# done ahead of us so we don't duplicate their work.
|
|
#
|
|
# If $fossil_prompt_info isn't in the prompt strings already but we
|
|
# can find $git_prompt_info, put it immediately afterward so that
|
|
# one or the other appears in the prompt. We're gambling that you
|
|
# aren't in a directory that's a checkout for both a Git repo and a
|
|
# Fossil repo.
|
|
#
|
|
# Otherwise, add it to the right prompt, as before.
|
|
if [ "${PROMPT/fossil_prompt_info//}" = "$PROMPT" ]; then
|
|
PROMPT="${PROMPT/git_prompt_info/git_prompt_info)\$(fossil_prompt_info}"
|
|
fi
|
|
if [ "${RPROMPT/fossil_prompt_info//}" = "$RPROMPT" ]; then
|
|
RPROMPT="${RPROMPT/git_prompt_info/git_prompt_info)\$(fossil_prompt_info}"
|
|
fi
|
|
if [[ "$PROMPT$RPROMPT" != *fossil_prompt_info* ]]; then
|
|
RPROMPT+='$(fossil_prompt_info)'
|
|
fi
|
|
|
|
_FOSSIL_PROMPT="1"
|
|
fi
|
|
}
|
|
|
|
autoload -U add-zsh-hook
|
|
add-zsh-hook precmd _fossil_prompt
|