Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to Adding a new prompt element #169

Closed
securisec opened this issue Aug 14, 2019 · 16 comments
Closed

How to Adding a new prompt element #169

securisec opened this issue Aug 14, 2019 · 16 comments

Comments

@securisec
Copy link

Typically I like to add a prompt element for my powerlevel9k theme to show if i am in a directory that has a .envrc file in it. I am trying to replicate the same behavior in powerlevel10k, but am not getting the same output. The code i am using is:

  • p10k.zsh
prompt_direnv(){
  if [ ! -z "$DIRENV_DIR" ]; then
    _p9k_prompt_segment "$0" "$2" "black" "green" "" 'DIRENV_ICON'
  fi
}
  • icons.zsh
DIRENV_ICON '\uFA98'

under all the icon configuration

  • ~/.p10k.zsh
typeset -g POWERLEVEL9K_LEFT_PROMPT_ELEMENTS=(direnv)

A silimar configuration to this works for me in powerlevel9k, but cant seem to get any output in p10k... any suggestions on how to show an icon in p10k when a .envrc file is present?

@securisec securisec changed the title Adding a new prompt element How to Adding a new prompt element Aug 14, 2019
@romkatv
Copy link
Owner

romkatv commented Aug 14, 2019

_p9k_prompt_segment is an internal API. Don't use it.

Open ~/.p10k.zsh and search for "prompt_example". Does it help?

@securisec
Copy link
Author

Ah yes, that was perfect!

function prompt_direnv(){
    if [ ! -z "$DIRENV_DIR" ]; then
      p10k segment -f 208 -i '🔥' -t ''
    fi
  }

Thanks!

@romkatv
Copy link
Owner

romkatv commented Aug 14, 2019

👍

@romkatv
Copy link
Owner

romkatv commented Aug 14, 2019

(You can remote -t '' argument. Empty text is the default.)

@securisec
Copy link
Author

even better!

@securisec
Copy link
Author

There is one slight odd behavior though.. When i am in a dir with either virtualenv or something like that, it doesnt show the indicator sometimes until i press enter. is this as designed?

@romkatv
Copy link
Owner

romkatv commented Aug 14, 2019

If DIRENV_DIR is set in a precmd hook that was registered after p10k, you'll can get this behavior. Try changing your prompt_direnv as follows:

function prompt_direnv() {
  p10k segment -f 208 -i '🔥' -c '$DIRENV_DIR'
}

(By the way, I don't think this icon can be rendered in different colors, so you can remove -f 208.)

From p10k help segment:

  -c        condition; if empty after parameter expansion and process substitution, the
            segment is hidden;

So we instruct p10k to show 🔥 only when $DIRENV_DIR is non-empty.

@securisec
Copy link
Author

This is perfect! Thanks for the tips!

@doronbehar
Copy link

Hey! Just came around this issue when searching for a way to define my own segment and the explanations and examples here were very useful. Perhaps we should add these to the WiKi? I would have added it my self already but I think it's disabled.

@romkatv
Copy link
Owner

romkatv commented Oct 25, 2019

@doronbehar

There is indeed no wiki and no real documentation. The proxy for documentation is ~/.p10k.zsh produced by the configuration wizard (p10k configure). You can also read config templates directly from the repo: https://github.com/romkatv/powerlevel10k/blob/master/config/p10k-classic.zsh. Here's what it has:

typeset -g POWERLEVEL9K_RIGHT_PROMPT_ELEMENTS=(
  ...
  # example               # example user-defined segment (see prompt_example function below)
)

...

# Example of a user-defined prompt segment. Function prompt_example will be called on every
# prompt if `example` prompt segment is added to POWERLEVEL9K_LEFT_PROMPT_ELEMENTS or
# POWERLEVEL9K_RIGHT_PROMPT_ELEMENTS. It displays an icon and orange text greeting the user.
#
# Type `p10k help segment` for documentation and a more sophisticated example.
function prompt_example() {
  p10k segment -f 208 -i '' -t 'hello, %n'
}

# User-defined prompt segments can be customized the same way as built-in segments.
typeset -g POWERLEVEL9K_EXAMPLE_FOREGROUND=208

Eventually I'd like to have real docs but I'd rather not start a wiki.

P.S.

What segment have you defined? Could you share the code?

@doronbehar
Copy link

Thanks for replying so quickly! I read the comments in my ~/.p10k.zsh and understood most of them. I also use direnv and I added the exact same prompt element as @securisec did. I intend to write a bit more complex prompt element that will indicate how many new emails I've got in my maildirs and I wanted to share it somehow.

Somewhat related to documentation, it wasn't completely obvious right from the start that ~/.p10k has so many useful comments that will help me configure it. If I hadn't encountered this issue, I wouldn't have searched for prompt_example. Besides that, I had to find other issues related to vi_mode that led me into adding it to my config. This wasn't trivial as well to figure out - even the issues themselves haven't indicated I should just add the string vi_mode in e.g POWERLEVEL9K_RIGHT_PROMPT_ELEMENTS - I mean, you know what I mean, it could be vi_mode / vimode etc. how am I supposed to guess without git grep vi | grep mode in the repo?

p10k is great but it's a shame it's only configuration documentation is available with git grep inside the repo and old issues...

@doronbehar
Copy link

doronbehar commented Oct 25, 2019

Note that I'm not a migrator from p9k and I don't even know what that is so if it has documentation, it might be useful to link to it somewhere.

EDIT: considering the fact p10k seems to be compatible with it.

Here are issues that helped me configure vi-mode indication:

@romkatv
Copy link
Owner

romkatv commented Oct 25, 2019

Thanks for replying so quickly! I read the comments in my ~/.p10k.zsh and understood most of them. I also use direnv and I added the exact same prompt element as @securisec did.

I've opened #291 to add direnv segment to p10k.

I intend to write a bit more complex prompt element that will indicate how many new emails I've got in my maildirs and I wanted to share it somehow.

You can share it like any other piece of zsh code. If you show it to me, I might offer tips for making it better and/or faster, or even add it to p10k so that other users will have easier time to discover it and you won't have to maintain it.

Somewhat related to documentation, it wasn't completely obvious right from the start that ~/.p10k has so many useful comments that will help me configure it.

If you use the configuration wizard, you'll have this in your ~/.zshrc.

# To customize prompt, run `p10k configure` or edit ~/.p10k.zsh.

Since you wanted to customize your prompt, and you knew that p10k configure doesn't offer what you want, you could try the second mentioned option. That's how it's supposed to work in theory but I hear that it didn't actually work in practice.

I've added the following sentence to the Configuration section on the homepage:

You can further customize your prompt by editing ~/.p10k.zsh.

Hopefully it'll induce more users to open this file.

Besides that, I had to find other issues related to vi_mode that led me into adding it to my config.

The reason vi_mode isn't mentioned in ~/.p10k.zsh is that it's not useful. prompt_char is strictly better. It can do everything that vi_mode does and more.

@romkatv
Copy link
Owner

romkatv commented Oct 27, 2019

@securisec @doronbehar

Heads up: I've implemented direnv prompt segment in Powerlevel10k. Since it has the same name as your user-defined segment, it may or may not override it. I recommend that you do the following:

  1. Update powerlevel10k.
  2. Remove prompt_direnv function that you had earlier.
  3. Restart zsh and verify that you still see direnv segment but now it has a different icon (▼) and perhaps color (it's now yellow).
  4. Optional: Define necessary style-related parameters to get the look you want:
typeset -g POWERLEVEL9K_DIRENV_FOREGROUND=4
typeset -g POWERLEVEL9K_DIRENV_BACKGROUND=7
typeset -g POWERLEVEL9K_DIRENV_VISUAL_IDENTIFIER_EXPANSION='🔥'

Note: p10k configure includes direnv in the list of enabled segments by default.

@securisec
Copy link
Author

securisec commented Oct 27, 2019 via email

@Ameausoone
Copy link

👌

# Example of a user-defined prompt segment. Function prompt_example will be called on every

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants