1
0
Fork 0
mirror of https://github.com/romkatv/powerlevel10k.git synced 2024-11-24 05:00:07 +00:00

make rust_version segment compatible with the new rustup toolchain file (#2413)

This commit is contained in:
Roman Perepelitsa 2023-10-23 10:09:41 +02:00
parent 862440ae11
commit c7fa7d6748

View file

@ -3186,70 +3186,57 @@ instant_prompt_root_indicator() { prompt_root_indicator; }
################################################################
# Segment to display Rust version number
prompt_rust_version() {
unset P9K_RUST_VERSION
if (( _POWERLEVEL9K_RUST_VERSION_PROJECT_ONLY )); then
_p9k_upglob Cargo.toml -. && return
fi
local rustc=$commands[rustc] toolchain deps=()
if (( $+commands[ldd] )); then
if ! _p9k_cache_stat_get $0_so $rustc; then
local line so
for line in "${(@f)$(ldd $rustc 2>/dev/null)}"; do
[[ $line == (#b)[[:space:]]#librustc_driver[^[:space:]]#.so' => '(*)' (0x'[[:xdigit:]]#')' ]] || continue
so=$match[1]
break
done
_p9k_cache_stat_set "$so"
fi
deps+=$_p9k__cache_val[1]
fi
if (( $+commands[rustup] )); then
local rustup=$commands[rustup]
local rustup_home=${RUSTUP_HOME:-~/.rustup}
local cfg=($rustup_home/settings.toml(.N))
deps+=($cfg $rustup_home/update-hashes/*(.N))
if [[ -z ${toolchain::=$RUSTUP_TOOLCHAIN} ]]; then
if ! _p9k_cache_stat_get $0_overrides $rustup $cfg; then
local lines=(${(f)"$(rustup override list 2>/dev/null)"})
if [[ $lines[1] == "no overrides" ]]; then
_p9k_cache_stat_set
else
local MATCH
local keys=(${(@)${lines%%[[:space:]]#[^[:space:]]#}/(#m)*/${(b)MATCH}/})
local vals=(${(@)lines/(#m)*/$MATCH[(I)/] ${MATCH##*[[:space:]]}})
_p9k_cache_stat_set ${keys:^vals}
fi
fi
local -A overrides=($_p9k__cache_val)
_p9k_upglob rust-toolchain -.
local dir=$_p9k__parent_dirs[$?]
local -i n m=${dir[(I)/]}
local pair
for pair in ${overrides[(K)$_p9k__cwd/]}; do
n=${pair%% *}
(( n <= m )) && continue
m=n
toolchain=${pair#* }
done
if [[ -z $toolchain && -n $dir ]]; then
_p9k_read_word $dir/rust-toolchain
toolchain=$_p9k__ret
fi
fi
fi
if ! _p9k_cache_stat_get $0_v$toolchain $rustc $deps; then
_p9k_cache_stat_set "$($rustc --version 2>/dev/null)"
fi
local v=${${_p9k__cache_val[1]#rustc }%% *}
[[ -n $v ]] || return
typeset -g P9K_RUST_VERSION=$_p9k__cache_val[1]
_p9k_prompt_segment "$0" "darkorange" "$_p9k_color1" 'RUST_ICON' 0 '' "${v//\%/%%}"
local -i len=$#_p9k__prompt _p9k__has_upglob
_p9k_prompt_segment $0 darkorange $_p9k_color1 RUST_ICON 1 '$P9K_RUST_VERSION' '${P9K_RUST_VERSION//\%/%%}'
(( _p9k__has_upglob )) || typeset -g "_p9k__segment_val_${_p9k__prompt_side}[_p9k__segment_index]"=$_p9k__prompt[len+1,-1]
}
_p9k_prompt_rust_version_init() {
function _p9k_prompt_rust_version_init() {
_p9k__async_segments_compute+='_p9k_rust_version_prefetch'
typeset -g "_p9k__segment_cond_${_p9k__prompt_side}[_p9k__segment_index]"='$commands[rustc]'
}
_p9k_rust_version_prefetch() {
local rustc=$commands[rustc]
if [[ -z $rustc ]] ||
{ (( _POWERLEVEL9K_RUST_VERSION_PROJECT_ONLY )) && _p9k_upglob Cargo.toml -. }; then
unset P9K_RUST_VERSION
return
fi
_p9k_worker_invoke rust_version \
"_p9k_prompt_rust_version_compute ${(q)P9K_RUST_VERSION} ${(q)rustc} ${(q)_p9k__cwd_a}"
}
_p9k_prompt_rust_version_compute() {
_p9k_worker_async \
"_p9k_prompt_rust_version_async ${(q)1} ${(q)2} ${(q)3}" \
_p9k_prompt_rust_version_sync
}
_p9k_prompt_rust_version_async() {
typeset -g P9K_RUST_VERSION=$1
local rustc=$2 cwd=$3 v
if pushd -q -- $cwd; then
{
v=${${"$($rustc --version)"#rustc }%% *} || v=
} always {
popd -q
}
fi
[[ $v != $P9K_RUST_VERSION ]] || return
typeset -g P9K_RUST_VERSION=$v
_p9k_print_params P9K_RUST_VERSION
echo -E - 'reset=1'
}
_p9k_prompt_rust_version_sync() {
if [[ -n $REPLY ]]; then
eval $REPLY
_p9k_worker_reply $REPLY
fi
}
# RSpec test ratio
prompt_rspec_stats() {
if [[ -d app && -d spec ]]; then