Powerlevel10k: [feature] Show node/ruby/python/etc version via asdf

Created on 11 Sep 2019  ·  26Comments  ·  Source: romkatv/powerlevel10k

Hey,
I found out about https://github.com/starship/starship#-features today, and I wanted to check if powerlevel10k could display my package versions the same way:

image

Turns out it can 🙌 , with support for pyenv, nodeenv, rbenv, etc.
Now, I replaced all of these package managers with asdf.

Would it be possible to add support for asdf? I'm no dev, but happy to take a look if I can get some pointers.

Most helpful comment

Do you think it'd be possible to have an option to display a version only when the current version differs from the global version (which can be considered as "default")?

This is done. If you update powerlevel10k, restart zsh and run p10k configure, you'll have the following block in ~/.p10k.zsh:

# There are three parameters that can be used to hide tools. If at least one of them decides
# to hide a tool, that tool gets hidden. POWERLEVEL9K_ASDF_SHOW_SYSTEM=false hides "system". To
# see the difference between POWERLEVEL9K_ASDF_SOURCES and POWERLEVEL9K_ASDF_PROMPT_ALWAYS_SHOW
# consider the effect of the following commands:
#
#   asdf local  python 3.8.1
#   asdf global python 3.8.1
#
# After running both commands the current python version is 3.8.1 and its source is "local" as
# it takes precedence over "global". If POWERLEVEL9K_ASDF_PROMPT_ALWAYS_SHOW is set to false,
# it'll hide python version in this case because 3.8.1 is the same as the global version.
# POWERLEVEL9K_ASDF_SOURCES will hide python version only if the value of this parameter doesn't
# contain "local".

# Hide tool versions that don't come from one of these sources.
#
# Available sources:
#
# - shell   `asdf current` says "set by ASDF_${TOOL}_VERSION environment variable"
# - local   `asdf current` says "set by /some/not/home/directory/file"
# - global  `asdf current` says "set by /home/username/file"
#
# Note: If this parameter is set to (shell local global), it won't hide tools.
# Tip:  Override this parameter for ${TOOL} with POWERLEVEL9K_ASDF_${TOOL}_SOURCES.
typeset -g POWERLEVEL9K_ASDF_SOURCES=(shell local global)

# If set to false, hide tool versions that are the same as global.
#
# Note: The name of this parameter doesn't reflect its meaning at all.
# Note: If this parameter is set to true, it won't hide tools.
# Tip:  Override this parameter for ${TOOL} with POWERLEVEL9K_ASDF_${TOOL}_PROMPT_ALWAYS_SHOW.
typeset -g POWERLEVEL9K_ASDF_PROMPT_ALWAYS_SHOW=false

# If set to false, hide tool versions that are equal to "system".
#
# Note: If this parameter is set to true, it won't hide tools.
# Tip: Override this parameter for ${TOOL} with POWERLEVEL9K_ASDF_${TOOL}_SHOW_SYSTEM.
typeset -g POWERLEVEL9K_ASDF_SHOW_SYSTEM=true

The names of these parameters are truly awful. I chose them for compatibility with the existing *env segments (parameter names for those segments were inherited from powerlevel9k). The meaning of these parameters is pretty good though.

All 26 comments

Note: I use the legacy_version_file = yes of asdf, which means I have files like .ruby-version.

Because of that I thought it could already work with the rbenv segment of powerline10k — but no, so I guess the rbenv CLI is actually expected.

Thanks for filing the feature request. Sounds useful. I'll implement it some time later.

@romkatv if can make this feature async that will be great too. Thanks!

@sinetoami Hopefully I won't have to make it async. My first preference would be to make it fast, the same as the current support for nvm and rvm. If I fail to find a way to add fast support for asdf, then async it will be.

Hi, any progress on this one? This is the only missing piece for me. Other than that everything works perfect! Thanks

No progress so far, no ETA.

Just bumping this feature request- would be super useful for me as well.

Also @romkatv is there a theme for powerlevel10k that looks kind of similar to starship by any chance? I'm enjoying starship but running into some issues with the prompt speed.

Thanks!

Just bumping this feature request- would be super useful for me as well.

Still no ETA. It's not very difficult to implement but I'm busy with other things.

Also @romkatv is there a theme for powerlevel10k that looks kind of similar to starship by any chance?

Type p10k configure and select Lean style when asked.

@romkatv that worked great- thank you!

one other nice feature I found in starship is shortening the dir in the prompt to just the name of a git repository when inside of a repo. is there any way to do that?

I don't know how exactly starship shortens but there are a couple things in p10k that are similar.

One option is to open ~/.p10k.zsh, search for POWERLEVEL9K_DIR_MAX_LENGTH and set it to 0. Read comments on the parameters around that spot to understand how directory shortening works and what this change entails. You can also see this demo and read the description in it. This shortening algorithm is really good. I understand that you are used to something else but there is a good chance that you'll prefer this one if you use it. If not, let me know what is missing in your opinion.

Another option is to set POWERLEVEL9K_DIR_MAX_LENGTH=truncate_with_package_name. This will shorten your directory when any of the parent directories has package.json. I'm not fond of this parameter.

I've added asdf prompt segment to powerlevel10k. Please give it a try and let me know whether it works for you.

The easiest way to enable asdf is to update powerlevel10k, restart zsh and run p10k configure. You can also manually add it to your config. Search for asdf in https://github.com/romkatv/powerlevel10k/blob/master/config/p10k-lean.zsh for an example.

I don't know how exactly starship shortens but there are a couple things in p10k that are similar.

I now know how starship shortens directories. The equivalent parameter for powerlevel10k is POWERLEVEL9K_DIR_TRUNCATE_BEFORE_MARKER=true. This assumes that you haven't changed other parameters related to directory shortening in ~/.p10k.zsh.

Awesome, thanks @romkatv!
asdf segment works well—at least for ruby and nodejs versions.

Do you think it'd be possible to have an option to display a version only when the current version differs from the global version (which can be considered as "default")?

@julienma Certainly. What do you consider global version when using asdf?

ASDF Global versions are defined in ~/.tool-versions. For example, mine is:

python 3.8.1 2.7.17
ruby 2.5.7
nodejs 13.5.0

First of all, thanks a lot for adding asdf support to this awesome prompt!

The feature seems to work great and it's fast. I haven't checked if it updates when switching versions though because I currently have only one version per tool installed.

I also like the idea of not showing default versions like @AlJohri suggested. They could be shown when in a directory which meets certain conditions:

  • a .tool-versions file or legacy version file in the current dir or a parent which is not $HOME
  • a package.json, mix.exs etc. in the current dir or any parent
  • when switching to a version different than the default one defined in ~/.tool-versions

The segment currently shows in 2 colors: bright cyan for tools with an icon and dimmed for tools with a text label.

I have found that nerdfont-complete has support for more icons, so I checked by adding some to icons.zsh:

        ERLANG_ICON                    '\ue7b1'               # 
        ELIXIR_ICON                    '\ue62d'               # 
        POSTGRES_ICON                  '\ue76e'               # 

Now the icons show up instead of text labels, but they are still dimmed and not bright cyan. What's going on here?

The feature seems to work great and it's fast. I haven't checked if it updates when switching versions though because I currently have only one version per tool installed.

Powerlevel10k should be instant in all circumstances. If it's ever not instant, please open an issue.

To close the loop on https://github.com/romkatv/powerlevel10k/issues/211#issuecomment-533883684: I've implemented asdf as synchronous prompt. This is a good thing. It means your prompt always shows up-to-date information. Async implies that prompt doesn't block (good) and that presented information is stale (bad). In powerlevel10k you get the good without the bad. The only downside of synchronous prompt segments is that they are much more difficult to implement (a lot of work for me).

The segment currently shows in 2 colors: bright cyan for tools with an icon and dimmed for tools with a text label.

Take a look at your ~/.p10k.zsh. You'll see something like this:

###############[ asdf: asdf version manager (https://github.com/asdf-vm/asdf) ]###############
# Default asdf color. Only used to display tools for which there is no color override (see below).
typeset -g POWERLEVEL9K_ASDF_FOREGROUND=66

# Ruby version from asdf.
typeset -g POWERLEVEL9K_ASDF_RUBY_FOREGROUND=168
# typeset -g POWERLEVEL9K_ASDF_RUBY_VISUAL_IDENTIFIER_EXPANSION='⭐'

# Python version from asdf.
typeset -g POWERLEVEL9K_ASDF_PYTHON_FOREGROUND=37
# typeset -g POWERLEVEL9K_ASDF_PYTHON_VISUAL_IDENTIFIER_EXPANSION='⭐'

# ...

If a tool version is displayed in a dimmed color (that's color 66), it means there is no custom override for it. You can override the color and icon by adding the following parameters to ~/.p10k.zsh:

# Elixir version from asdf.
typeset -g POWERLEVEL9K_ASDF_ELIXIR_FOREGROUND=129
typeset -g POWERLEVEL9K_ASDF_ELIXIR_VISUAL_IDENTIFIER_EXPANSION=$'\uE62D'

You can use the following command to help you choose the right color:

for i in {0..255}; do print -Pn "%K{$i} %k%F{$i}${(l:3::0:)i}%f " ${${(M)$((i%8)):#7}:+$'\n'}; done

I've just added overrides for erlang, elixir and postgres. If you update powerlevel10k and run p10k configure, you'll have them.

Awesome, thanks!

Do you think it'd be possible to have an option to display a version only when the current version differs from the global version (which can be considered as "default")?

This is done. If you update powerlevel10k, restart zsh and run p10k configure, you'll have the following block in ~/.p10k.zsh:

# There are three parameters that can be used to hide tools. If at least one of them decides
# to hide a tool, that tool gets hidden. POWERLEVEL9K_ASDF_SHOW_SYSTEM=false hides "system". To
# see the difference between POWERLEVEL9K_ASDF_SOURCES and POWERLEVEL9K_ASDF_PROMPT_ALWAYS_SHOW
# consider the effect of the following commands:
#
#   asdf local  python 3.8.1
#   asdf global python 3.8.1
#
# After running both commands the current python version is 3.8.1 and its source is "local" as
# it takes precedence over "global". If POWERLEVEL9K_ASDF_PROMPT_ALWAYS_SHOW is set to false,
# it'll hide python version in this case because 3.8.1 is the same as the global version.
# POWERLEVEL9K_ASDF_SOURCES will hide python version only if the value of this parameter doesn't
# contain "local".

# Hide tool versions that don't come from one of these sources.
#
# Available sources:
#
# - shell   `asdf current` says "set by ASDF_${TOOL}_VERSION environment variable"
# - local   `asdf current` says "set by /some/not/home/directory/file"
# - global  `asdf current` says "set by /home/username/file"
#
# Note: If this parameter is set to (shell local global), it won't hide tools.
# Tip:  Override this parameter for ${TOOL} with POWERLEVEL9K_ASDF_${TOOL}_SOURCES.
typeset -g POWERLEVEL9K_ASDF_SOURCES=(shell local global)

# If set to false, hide tool versions that are the same as global.
#
# Note: The name of this parameter doesn't reflect its meaning at all.
# Note: If this parameter is set to true, it won't hide tools.
# Tip:  Override this parameter for ${TOOL} with POWERLEVEL9K_ASDF_${TOOL}_PROMPT_ALWAYS_SHOW.
typeset -g POWERLEVEL9K_ASDF_PROMPT_ALWAYS_SHOW=false

# If set to false, hide tool versions that are equal to "system".
#
# Note: If this parameter is set to true, it won't hide tools.
# Tip: Override this parameter for ${TOOL} with POWERLEVEL9K_ASDF_${TOOL}_SHOW_SYSTEM.
typeset -g POWERLEVEL9K_ASDF_SHOW_SYSTEM=true

The names of these parameters are truly awful. I chose them for compatibility with the existing *env segments (parameter names for those segments were inherited from powerlevel9k). The meaning of these parameters is pretty good though.

Thanks a lot for asdf support. I've added it by p10k configure.

It works smoothly for most of my projects but one repo has an error, causing a blank status bar.

prompt_asdf:22: bad set of key/value pairs for associative array

The repository has ruby and adr-tools.
asdf is configured with legacy_version_file = yes.

Can you help take a look at this?

@tiev Can you reproduce this with the newest version of powerlevel10k? Please try updating it and restarting zsh (source ~/.zshrc is not enough).

It works now. Thank you @romkatv

@tiev Thanks for verifying. The error was likely caused by a bug in powerlevel10k that I've fixed in https://github.com/romkatv/powerlevel10k/commit/540f8956692f34ff51e05664b5c768ce5f4c0e31.

I think so. I've updated and gotten the last 3 commits, including the one you mention.

Thanks, everyone, for testing and providing feedback. Closing as done.

Do you think it'd be possible to have an option to display a version only when the current version differs from the global version (which can be considered as "default")?

This is done.

Man, you're awesome @romkatv.
Works perfectly. Thanks!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

romkatv picture romkatv  ·  5Comments

georgijd picture georgijd  ·  3Comments

debo picture debo  ·  5Comments

Mystic8b picture Mystic8b  ·  4Comments

bwoodruff picture bwoodruff  ·  6Comments