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
Feature request: add segment for rtx #2212
Comments
I managed to create an implementation by copy-pasting the asdf functions, deleting what seemed to be unnecessary, and piggybacking on the ASDF p10k settings. function prompt_rtx() {
emulate -L zsh -o extended_glob
typeset -ga _p9k_rtx_meta_files
typeset -ga _p9k_rtx_meta_non_files
typeset -g _p9k_rtx_meta_sig
# plugin => installed_version_pattern
# example: (ruby '2.7.0|2.6.3|system' lua 'system' chubaka '1.0.0|system')
typeset -gA _p9k_rtx_plugins
# dir => mtime ':' ${(pj:\0:)files}
typeset -gA _p9k_rtx_dir2files
# :file => mtime ':' ${(pj:\0:)tool_versions}
# plugin:file => mtime ':' version
typeset -gA _p9k_rtx_file2versions
function _p9k_rtx_check_meta() {
[[ -n $_p9k_rtx_meta_sig ]] || return
[[ -z $^_p9k_rtx_meta_non_files(#qN) ]] || return
local -a stat
if (( $#_p9k_rtx_meta_files )); then
zstat -A stat +mtime -- $_p9k_rtx_meta_files 2>/dev/null || return
fi
[[ $_p9k_rtx_meta_sig == $RTX_CONFIG_FILE$'\0'$RTX_DATA_DIR$'\0'${(pj:\0:)stat} ]] || return
}
function _p9k_rtx_init_meta() {
local last_sig=$_p9k_rtx_meta_sig
{
local -a files
_p9k_rtx_plugins=()
local cfg=${RTX_CONFIG_FILE:-~/.config/rtx/config.toml}
files+=$cfg
local root=${RTX_DATA_DIR:-~/.local/share/rtx}
files+=$root/plugins
if [[ -d $root/plugins ]]; then
local plugin
for plugin in $root/plugins/[^[:space:]]##(/N); do
files+=$root/installs/${plugin:t}
local -aU installed=($root/installs/${plugin:t}/[^[:space:]]##(/N:t) system)
_p9k_rtx_plugins[${plugin:t}]=${(j:|:)${(@b)installed}}
done
fi
_p9k_rtx_meta_files=($^files(N))
_p9k_rtx_meta_non_files=(${files:|_p9k_rtx_meta_files})
local -a stat
if (( $#_p9k_rtx_meta_files )); then
zstat -A stat +mtime -- $_p9k_rtx_meta_files 2>/dev/null || return
fi
_p9k_rtx_meta_sig=$RTX_CONFIG_FILE$'\0'$RTX_DATA_DIR$'\0'${(pj:\0:)stat}
_p9k_rtx_dir2files=()
_p9k_rtx_file2versions=()
} always {
if (( $? == 0 )); then
_p9k__state_dump_scheduled=1
return
fi
[[ -n $last_sig ]] && _p9k__state_dump_scheduled=1
_p9k_rtx_meta_files=()
_p9k_rtx_meta_non_files=()
_p9k_rtx_meta_sig=
_p9k_rtx_plugins=()
_p9k_rtx_dir2files=()
_p9k_rtx_file2versions=()
}
}
# Usage: _p9k_rtx_parse_version_file <file>
#
# Mutates `versions` on success.
function _p9k_rtx_parse_version_file() {
local file=$1
local -a stat
zstat -A stat +mtime $file 2>/dev/null || return
local cached=$_p9k_rtx_file2versions[:$file]
if [[ $cached == $stat[1]:* ]]; then
local file_versions=(${(0)${cached#*:}})
else
local file_versions=()
{ local lines=(${(@)${(@)${(f)"$(<$file)"}%$'\r'}/\#*}) } 2>/dev/null
local line
for line in $lines; do
local words=($=line)
(( $#words > 1 )) || continue
local installed=$_p9k_rtx_plugins[$words[1]]
[[ -n $installed ]] || continue
file_versions+=($words[1] ${${words:1}[(r)$installed]:-$words[2]})
done
_p9k_rtx_file2versions[:$file]=$stat[1]:${(pj:\0:)file_versions}
_p9k__state_dump_scheduled=1
fi
local plugin version
for plugin version in $file_versions; do
: ${versions[$plugin]=$version}
done
return 0
}
_p9k_rtx_check_meta || _p9k_rtx_init_meta || return
local -A versions
local -a stat
local -i has_global
local dirs=($_p9k__parent_dirs)
local mtimes=($_p9k__parent_mtimes)
if [[ $dirs[-1] != ~ ]]; then
zstat -A stat +mtime ~ 2>/dev/null || return
dirs+=(~)
mtimes+=($stat[1])
fi
local elem
for elem in ${(@)${:-{1..$#dirs}}/(#m)*/${${:-$MATCH:$_p9k_rtx_dir2files[$dirs[MATCH]]}#$MATCH:$mtimes[MATCH]:}}; do
if [[ $elem == *:* ]]; then
local dir=$dirs[${elem%%:*}]
zstat -A stat +mtime $dir 2>/dev/null || return
local files=($dir/.tool-versions(N))
_p9k_rtx_dir2files[$dir]=$stat[1]:${(pj:\0:)files}
else
local files=(${(0)elem})
fi
if [[ ${files[1]:h} == ~ ]]; then
has_global=1
local -A local_versions=(${(kv)versions})
versions=()
fi
local file=
for file in $files; do
_p9k_rtx_parse_version_file $file || return
done
done
if (( ! has_global )); then
has_global=1
local -A local_versions=(${(kv)versions})
versions=()
fi
if [[ -r $RTX_DEFAULT_TOOL_VERSIONS_FILENAME ]]; then
_p9k_rtx_parse_version_file $RTX_DEFAULT_TOOL_VERSIONS_FILENAME || return
fi
local plugin
for plugin in ${(k)_p9k_rtx_plugins}; do
local upper=${${(U)plugin//-/_}//İ/I}
if (( $+parameters[_POWERLEVEL9K_ASDF_${upper}_SOURCES] )); then
local sources=(${(P)${:-_POWERLEVEL9K_ASDF_${upper}_SOURCES}})
else
local sources=($_POWERLEVEL9K_ASDF_SOURCES)
fi
local version="${(P)${:-RTX_${upper}_VERSION}}"
if [[ -n $version ]]; then
(( $sources[(I)shell] )) || continue
else
version=$local_versions[$plugin]
if [[ -n $version ]]; then
(( $sources[(I)local] )) || continue
else
version=$versions[$plugin]
[[ -n $version ]] || continue
(( $sources[(I)global] )) || continue
fi
fi
if [[ $version == $versions[$plugin] ]]; then
if (( $+parameters[_POWERLEVEL9K_ASDF_${upper}_PROMPT_ALWAYS_SHOW] )); then
(( _POWERLEVEL9K_ASDF_${upper}_PROMPT_ALWAYS_SHOW )) || continue
else
(( _POWERLEVEL9K_ASDF_PROMPT_ALWAYS_SHOW )) || continue
fi
fi
if [[ $version == system ]]; then
if (( $+parameters[_POWERLEVEL9K_ASDF_${upper}_SHOW_SYSTEM] )); then
(( _POWERLEVEL9K_ASDF_${upper}_SHOW_SYSTEM )) || continue
else
(( _POWERLEVEL9K_ASDF_SHOW_SYSTEM )) || continue
fi
fi
_p9k_get_icon prompt_asdf_$upper ${upper}_ICON $plugin
_p9k_prompt_segment prompt_asdf_$upper green $_p9k_color1 $'\1'$_p9k__ret 0 '' ${version//\%/%%}
done
}
POWERLEVEL9K_RIGHT_PROMPT_ELEMENTS[(r)asdf]=(asdf rtx) I'm sure it could be done much better, I don't really understand half of what is going on up there. But it might be a good start, and it does the job for me (for now). Also, this doesn't work for |
Rtx is quite fast, so it may also be reasonable to use its output rather than parsing config files (it's definitely easier 🙂). I'm currently using this for an rtx segment: function prompt_rtx() {
local plugins=("${(@f)$(rtx current)}")
local plugin
for plugin in ${(k)plugins}; do
local parts=("${(@s/ /)plugin}")
local upper=${(U)parts[1]}
local version=${parts[2]}
_p9k_get_icon prompt_rtx_$upper ${upper}_ICON $plugin
_p9k_prompt_segment prompt_rtx_$upper green $_p9k_color1 $'\1'$_p9k__ret 0 '' ${version//\%/%%}
done
}
typeset -g POWERLEVEL9K_RTX_FOREGROUND=$CYAN
typeset -g POWERLEVEL9K_RTX_RUBY_FOREGROUND=$RED
... |
@jason0x43 Thanks for sharing! Seems this should promising, but I’m not sure where to include this or if there is more to it. I've tried but |
I included the typeset -g POWERLEVEL9K_RIGHT_PROMPT_ELEMENTS=(
# =========================[ Line #1 ]=========================
status # exit code of the last command
command_execution_time # duration of the last command
background_jobs # presence of background jobs
direnv # direnv status (https://direnv.net/)
npm # custom npm registry host
asdf # asdf version manager (https://github.com/asdf-vm/asdf)
rtx # rtx version manager (https://github.com/jdxcode/rtx)
virtualenv # python virtual environment (https://docs.python.org/3/library/venv.html)
...
Very dense. |
Thanks @jason0x43! I forgot about I found that until I removed the
~/.zshrc# …
export ZSH="/Users/travis/.oh-my-zsh"
ZSH_THEME=powerlevel10k/powerlevel10k
plugins=(git rtx)
source $ZSH/oh-my-zsh.sh
[[ -f ~/.p10k.zsh ]] && source ~/.p10k.zsh
# … ~/.oh-my-zsh/custom/plugins/rtx/rtx.plugin.zsh# Oh My Zsh plugin for rtx
#
# https://github.com/jdxcode/rtx
# https://github.com/ohmyzsh/ohmyzsh/tree/master/plugins/asdf
# https://github.com/ohmyzsh/ohmyzsh/tree/master/plugins/direnv
# [Feature request: add segment for rtx](https://github.com/romkatv/powerlevel10k/issues/2212)
#
# TODO: Instant Prompt
# - https://github.com/romkatv/powerlevel10k/issues/702#issuecomment-626222730
# - https://github.com/romkatv/powerlevel10k#how-do-i-initialize-direnv-when-using-instant-prompt
#
# Don't continue if rtx is not found
command -v rtx &>/dev/null || return
source <(rtx activate zsh)
function prompt_rtx() {
local plugin_versions=("${(@f)$(rtx current 2>/dev/null)}")
local plugin_version
for plugin_version in $plugin_versions; do
local parts=("${(@s/ /)plugin_version}")
local plugin=${parts[1]}
local version=${parts[2]}
local upper=${(U)plugin}
_p9k_get_icon prompt_asdf_$upper ${upper}_ICON $plugin
_p9k_prompt_segment prompt_asdf_$upper green $_p9k_color1 $'\1'$_p9k__ret 0 '' ${version//\%/%%}
done
}
_p9k_prompt_rtx_init() {
typeset -g "_p9k__segment_cond_${_p9k__prompt_side}[_p9k__segment_index]"='${commands[rtx]:-${${+functions[rtx]}:#0}}'
} ~/.p10k.zsh# …
# The list of segments shown on the right. Fill it with less important segments.
# Right prompt on the last prompt line (where you are typing your commands) gets
# automatically hidden when the input line reaches it. Right prompt above the
# last prompt line gets hidden if it would overlap with left prompt.
typeset -g POWERLEVEL9K_RIGHT_PROMPT_ELEMENTS=(
# =========================[ Line #1 ]=========================
status # exit code of the last command
command_execution_time # duration of the last command
background_jobs # presence of background jobs
direnv # direnv status (https://direnv.net/)
#asdf # asdf version manager (https://github.com/asdf-vm/asdf)
rtx # rtx version manager (https://github.com/jdxcode/rtx)
virtualenv # python virtual environment (https://docs.python.org/3/library/venv.html)
# …
# Instant prompt mode.
#
# - off: Disable instant prompt. Choose this if you've tried instant prompt and found
# it incompatible with your zsh configuration files.
# - quiet: Enable instant prompt and don't print warnings when detecting console output
# during zsh initialization. Choose this if you've read and understood
# https://github.com/romkatv/powerlevel10k/blob/master/README.md#instant-prompt.
# - verbose: Enable instant prompt and print a warning when detecting console output during
# zsh initialization. Choose this if you've never tried instant prompt, haven't
# seen the warning, or if you are unsure what this all means.
typeset -g POWERLEVEL9K_INSTANT_PROMPT=quiet
# … |
Keep in mind that all powerlevel10k variables and functions that start with an underscore are internal. If you use them, your prompt can do anything when you update powerlevel10k. It might delete all your files or turn mouse on fire. There are no guarantees whatsoever. |
Sorry about that -- I had actually fixed that locally and forgotten to update my post.
For some reason I entirely forgot about A better version: function prompt_rtx() {
local plugins=("${(@f)$(rtx current)}")
local plugin
for plugin in ${(k)plugins}; do
local parts=("${(@s/ /)plugin}")
local tool=${(U)parts[1]}
local version=${parts[2]}
p10k segment -r -i "${tool}_ICON" -s $tool -t "$version"
done
}
|
OK, here’s where I am now. Thank you!
https://gist.github.com/travismiller/7a22b3a1227a9d0ac3cc2d2b33921abd # Powerlevel10k prompt segments for rtx
#
# https://github.com/romkatv/powerlevel10k
# https://github.com/jdxcode/rtx
# [Feature request: add segment for rtx](https://github.com/romkatv/powerlevel10k/issues/2212)
#
# Usage in ~/.zshrc:
#
# # To customize prompt, run `p10k configure` or edit ~/.p10k.zsh.
# [[ -f ~/.p10k.zsh ]] && source ~/.p10k.zsh
# [[ -f ~/.p10k.rtx.zsh ]] && source ~/.p10k.rtx.zsh
#
() {
function prompt_rtx() {
local plugins=("${(@f)$(rtx current 2>/dev/null)}")
local plugin
for plugin in ${(k)plugins}; do
local parts=("${(@s/ /)plugin}")
local tool=${(U)parts[1]}
local version=${parts[2]}
p10k segment -r -i "${tool}_ICON" -s $tool -t "$version"
done
}
typeset -g POWERLEVEL9K_RTX_FOREGROUND=66
typeset -g POWERLEVEL9K_RTX_DOTNET_CORE_FOREGROUND=134
typeset -g POWERLEVEL9K_RTX_ELIXIR_FOREGROUND=129
typeset -g POWERLEVEL9K_RTX_ERLANG_FOREGROUND=125
typeset -g POWERLEVEL9K_RTX_FLUTTER_FOREGROUND=38
typeset -g POWERLEVEL9K_RTX_GOLANG_FOREGROUND=37
typeset -g POWERLEVEL9K_RTX_HASKELL_FOREGROUND=172
typeset -g POWERLEVEL9K_RTX_JAVA_FOREGROUND=32
typeset -g POWERLEVEL9K_RTX_JULIA_FOREGROUND=70
typeset -g POWERLEVEL9K_RTX_LUA_FOREGROUND=32
typeset -g POWERLEVEL9K_RTX_NODEJS_FOREGROUND=70
typeset -g POWERLEVEL9K_RTX_PERL_FOREGROUND=67
typeset -g POWERLEVEL9K_RTX_PHP_FOREGROUND=99
typeset -g POWERLEVEL9K_RTX_POSTGRES_FOREGROUND=31
typeset -g POWERLEVEL9K_RTX_PYTHON_FOREGROUND=37
typeset -g POWERLEVEL9K_RTX_RUBY_FOREGROUND=168
typeset -g POWERLEVEL9K_RTX_RUST_FOREGROUND=37
# Substitute the default asdf prompt element
typeset -g POWERLEVEL9K_RIGHT_PROMPT_ELEMENTS=("${POWERLEVEL9K_RIGHT_PROMPT_ELEMENTS[@]/asdf/rtx}")
} |
Thanks for this. I used your code as a base and made a small change so that it excludes tools defined in ~/.tool-versions, since I only care if a tool version differs from the global config. local plugins=("${(@f)$(rtx ls --current 2>/dev/null | awk '$3!="~/.tool-versions" {print $1, $2}')}") For example, if the output of
It will only show ruby in the prompt
|
@landonepps I modified your code for people that prefer to use
So we have to account for that extra string. Here is the code: local plugins=("${(@f)$(rtx ls --current 2>/dev/null | awk '!/\(symlink\)/ && $3!="~/.config/rtx/config.toml" {print $1, $2}')}") |
Thanks a lot for this :) # Powerlevel10k prompt segments for rtx
#
# https://github.com/romkatv/powerlevel10k
# https://github.com/jdxcode/rtx
# [Feature request: add segment for rtx](https://github.com/romkatv/powerlevel10k/issues/2212)
#
# Usage in ~/.zshrc:
#
# # To customize prompt, run `p10k configure` or edit ~/.p10k.zsh.
# [[ -f ~/.p10k.zsh ]] && source ~/.p10k.zsh
# [[ -f ~/.p10k.rtx.zsh ]] && source ~/.p10k.rtx.zsh
#
() {
function prompt_rtx() {
local plugins=("${(@f)$(rtx ls --current 2>/dev/null | awk '!/\(symlink\)/ && $3!="~/.tool-versions" && $3!="~/.config/rtx/config.toml" {print $1, $2}')}")
local plugin
for plugin in ${(k)plugins}; do
local parts=("${(@s/ /)plugin}")
local tool=${(U)parts[1]}
local version=${parts[2]}
p10k segment -r -i "${tool}_ICON" -s $tool -t "$version"
done
}
typeset -g POWERLEVEL9K_RTX_FOREGROUND=6
typeset -g POWERLEVEL9K_RTX_RUBY_FOREGROUND=1
typeset -g POWERLEVEL9K_RTX_PYTHON_FOREGROUND=6
typeset -g POWERLEVEL9K_RTX_GOLANG_FOREGROUND=6
typeset -g POWERLEVEL9K_RTX_NODEJS_FOREGROUND=2
typeset -g POWERLEVEL9K_RTX_RUST_FOREGROUND=4
typeset -g POWERLEVEL9K_RTX_DOTNET_CORE_FOREGROUND=5
typeset -g POWERLEVEL9K_RTX_FLUTTER_FOREGROUND=4
typeset -g POWERLEVEL9K_RTX_LUA_FOREGROUND=4
typeset -g POWERLEVEL9K_RTX_JAVA_FOREGROUND=4
typeset -g POWERLEVEL9K_RTX_PERL_FOREGROUND=6
typeset -g POWERLEVEL9K_RTX_ERLANG_FOREGROUND=1
typeset -g POWERLEVEL9K_RTX_ELIXIR_FOREGROUND=5
typeset -g POWERLEVEL9K_RTX_POSTGRES_FOREGROUND=6
typeset -g POWERLEVEL9K_RTX_PHP_FOREGROUND=5
typeset -g POWERLEVEL9K_RTX_HASKELL_FOREGROUND=3
typeset -g POWERLEVEL9K_RTX_JULIA_FOREGROUND=2
# Substitute the default asdf prompt element
typeset -g POWERLEVEL9K_RIGHT_PROMPT_ELEMENTS=("${POWERLEVEL9K_RIGHT_PROMPT_ELEMENTS[@]/asdf/rtx}")
} |
Thanks a lot this looks great, for people who want to use background colors in their prompts, you can add this typeset -g POWERLEVEL9K_RTX_RUBY_BACKGROUND=196 Result will look like so: My full p10k.rtx.zsh if you are interested: https://github.com/2KAbhishek/dots2k/blob/main/config/shell/p10k.rtx.zsh |
Not sure about you guys, but I'm thinking of getting rid of this. So every time I enter a command, there is a noticeable delay before the prompt shows up again fully. This little delay really annoys me over time, especially in situations where I want to type in multiple commands in quick succession. Any thoughts? |
|
I have updated the script to use # Powerlevel10k prompt segments for mise
# [Feature request: add segment for mise](https://github.com/romkatv/powerlevel10k/issues/2212)
# Usage in ~/.zshrc:
# [[ -f ~/.config/shell/p10k.mise.zsh ]] && source ~/.config/shell/p10k.mise.zsh
() {
function prompt_mise() {
local plugins=("${(@f)$(mise ls --current 2>/dev/null | awk '!/\(symlink\)/ && $3!="~/.tool-versions" && $3!="~/.config/mise/config.toml" {print $1, $2}')}")
local plugin
for plugin in ${(k)plugins}; do
local parts=("${(@s/ /)plugin}")
local tool=${(U)parts[1]}
local version=${parts[2]}
p10k segment -r -i "${tool}_ICON" -s $tool -t "$version"
done
}
# Colors
typeset -g POWERLEVEL9K_MISE_BACKGROUND=1
typeset -g POWERLEVEL9K_MISE_DOTNET_CORE_BACKGROUND=5
typeset -g POWERLEVEL9K_MISE_ELIXIR_BACKGROUND=5
typeset -g POWERLEVEL9K_MISE_ERLANG_BACKGROUND=1
typeset -g POWERLEVEL9K_MISE_FLUTTER_BACKGROUND=4
typeset -g POWERLEVEL9K_MISE_GOLANG_BACKGROUND=4
typeset -g POWERLEVEL9K_MISE_HASKELL_BACKGROUND=3
typeset -g POWERLEVEL9K_MISE_JAVA_BACKGROUND=7
typeset -g POWERLEVEL9K_MISE_JULIA_BACKGROUND=2
typeset -g POWERLEVEL9K_MISE_LUA_BACKGROUND=4
typeset -g POWERLEVEL9K_MISE_NODEJS_BACKGROUND=2
typeset -g POWERLEVEL9K_MISE_PERL_BACKGROUND=4
typeset -g POWERLEVEL9K_MISE_PHP_BACKGROUND=5
typeset -g POWERLEVEL9K_MISE_POSTGRES_BACKGROUND=6
typeset -g POWERLEVEL9K_MISE_PYTHON_BACKGROUND=4
typeset -g POWERLEVEL9K_MISE_RUBY_BACKGROUND=1
typeset -g POWERLEVEL9K_MISE_RUST_BACKGROUND=208
# Substitute the default asdf prompt element
typeset -g POWERLEVEL9K_RIGHT_PROMPT_ELEMENTS=("${POWERLEVEL9K_RIGHT_PROMPT_ELEMENTS[@]/asdf/mise}")
} Link to file: https://github.com/2KAbhishek/dots2k/blob/main/config/shell/p10k.mise.zsh |
getting error: |
@2KAbhishek thanks for the updated script for mise! What is the purpose of the From an example project of mine: $ mise ls --current
Plugin Version Config Source Requested
node 18.16.0 ~/…/.node-version 18.16.0
ruby 3.2.2 ~/…/.ruby-version 3.2.2 |
@travismiller I'm not sure under what condition it shows up, this is something I got from another response in this issue |
rtx is a drop-in replacement for asdf, serving identical purposes. It is compatible with asdf in many respects, including the backend and the CLI. It would be great if p10k had a segment for rtx as well as asdf.
The text was updated successfully, but these errors were encountered: