romkatv / powerlevel10k

A Zsh theme
MIT License
46.05k stars 2.17k forks source link

[HOWTO] create 2 vcs configs #1105

Closed CircleCode closed 3 years ago

CircleCode commented 3 years ago

I'd like to have my current prompt to show full VCS info, and the transient one to show only branch@sha1 part.

Here is an extract of my config:

  typeset -g POWERLEVEL9K_LEFT_PROMPT_ELEMENTS=(
    # =========================[ Line #1 ]=========================
    command_execution_time
    vcs
    # =========================[ Line #2 ]=========================
    newline
    nvm
    dir
    # =========================[ Line #3 ]=========================
    newline
    status
    background_jobs
    prompt_char
  )

  typeset -g POWERLEVEL9K_TRANSIENT_PROMPT=off

function p10k-on-post-prompt() {
  p10k display '1|2|3/left_frame|3/left/background_jobs'=hide
}

function p10k-on-pre-prompt() {
  p10k display '1|2|3/left_frame|3/left/background_jobs'=show
}

I thought about adding something like vcs2 in third line, and define my_git_formatter2 with only the part I want to display. Then I could show and hide it in p10k-on-post-prompt and function p10k-on-pre-prompt

The thing is I don't know how to create this vcs2 segment (I know this is implemented through gitstatus which make it heavily asynchronous, and I expect there is an easy way to achieve this)

romkatv commented 3 years ago

You can do it like this:

  1. Update powerlevel10k to the latest version.
  2. Modify my_git_formatter to show less info in transient prompt:

    diff --git a/config/p10k-lean.zsh b/config/p10k-lean.zsh
    index ed6ce82..1e123a5 100644
    --- a/config/p10k-lean.zsh
    +++ b/config/p10k-lean.zsh
    @@ -391,6 +391,7 @@
          res+="${meta}:${clean}${(V)VCS_STATUS_REMOTE_BRANCH//\%/%%}"  # escape %
        fi
    
    +    if [[ $P9K_PROMPT != transient ]]; then
        # ⇣42 if behind the remote.
        (( VCS_STATUS_COMMITS_BEHIND )) && res+=" ${clean}⇣${VCS_STATUS_COMMITS_BEHIND}"
        # ⇡42 if ahead of the remote; no leading space if also behind the remote: ⇣42⇡42.
    @@ -421,6 +422,7 @@
        # in the repository config. The number of staged and untracked files may also be unknown
        # in this case.
        (( VCS_STATUS_HAS_UNSTAGED == -1 )) && res+=" ${modified}─"
    +    fi
    
        typeset -g my_git_format=$res
      }
  3. Add vcs to POWERLEVEL9K_LEFT_PROMPT_ELEMENTS before prompt_char.
  4. Modify prompt hooks like this:

    function p10k-on-post-prompt() {
      p10k display '1|2|3/left_frame|3/left/background_jobs'=hide '3/left/vcs'=show
    }
    
    function p10k-on-pre-prompt() {
      p10k display '1|2|3/left_frame|3/left/background_jobs'=show '3/left/vcs'=hide
    }
CircleCode commented 3 years ago

once you know about the $P9K_PROMPT, it seems so easy :-) Thanks for the tip, it works like a charm

Side question: with this config,the vcs segment is used twice. Is it computed twice, or is the computation done once, and only the display (with eventual variables interpolation) done twice?

romkatv commented 3 years ago

Side question: with this config,the vcs segment is used twice. Is it computed twice, or is the computation done once, and only the display (with eventual variables interpolation) done twice?

The status of the git repository is computed only once and then formatted twice. Formatting is fast, so this is fine.

There is still a bit of a performance hit when you use two vcs segments because Powerlevel10k has a few optimizations that it only uses when there is one vcs segment. On my machine I see prompt slowdown of 3-4 milliseconds when I add a second vcs segment. This is much lower than keyboard input latency, so it's virtually impossible to detect for a human.

CircleCode commented 3 years ago

thanks for your time, and the detailed explanations