在zsh提示符中更改当前工作目录的路径

时间:2016-09-25 00:32:54

标签: zsh zshrc

我尝试修改现有的zsh提示符以使用zsh 5.0和4.3,因为那些版本是我使用的系统。 如何让zsh脚本知道当前工作目录而不是文件所在的目录?

对于上下文,

这是脚本中的一个函数,用于检查我们当前是否在git目录中并在以下情况下添加到提示符中:

# Git status.
# Collect indicators, git branch and pring string.
spaceship_git_status() {
  [[ $SPACESHIP_GIT_SHOW == false ]] && return

  # Check if the current directory is in a Git repository.
  command git rev-parse --is-inside-work-tree &>/dev/null || return

  # Check if the current directory is in .git before running git checks.
  if [[ "$(git rev-parse --is-inside-git-dir 2> /dev/null)" == 'false' ]]; then
    # Ensure the index is up to date.
    git update-index --really-refresh -q &>/dev/null

    # String of indicators
    local indicators=''

    indicators+="$(spaceship_git_uncomitted)"
    indicators+="$(spaceship_git_unstaged)"
    indicators+="$(spaceship_git_untracked)"
    indicators+="$(spaceship_git_stashed)"
    indicators+="$(spaceship_git_unpushed_unpulled)"

    [ -n "${indicators}" ] && indicators=" [${indicators}]";

    echo -n " %Bon%b "
    echo -n "%{$fg_bold[magenta]%}"
    echo -n "$(git_current_branch)"
    echo -n "%{$reset_color%}"
    echo -n "%{$fg_bold[red]%}"
    echo -n "$indicators"
    echo -n "%{$reset_color%}"
  fi
}

但是,根据我的调试,该函数似乎始终认为它位于源脚本所在的目录中。换句话说,当我更改目录时,脚本继续引用脚本所在的目录。

此处调用spaceship_git_status函数:

# Build prompt line
spaceship_build_prompt() {
  spaceship_host
  spaceship_current_dir
  spaceship_git_status
  spaceship_nvm_status
  spaceship_ruby_version
  spaceship_venv_status
}

这是PROMPT环境变量:

# Compose PROMPT
PROMPT=''
[[ $SPACESHIP_PROMPT_ADD_NEWLINE == true ]] && PROMPT="$PROMPT$NEWLINE"
PROMPT="$PROMPT $(spaceship_build_prompt) "
[[ $SPACESHIP_PROMPT_SEPARATE_LINE == true ]] && PROMPT="$PROMPT$NEWLINE"
PROMPT="$PROMPT $(spaceship_return_status) "

我认为这是zsh版本的问题< 5.2因为在我的另一台计算机上使用5.2提示正常。

完整代码:https://github.com/denysdovhan/spaceship-zsh-theme/blob/master/spaceship.zsh

0 个答案:

没有答案
相关问题