From 4ad73c71542c0e923919ae60545926c5218b11ef Mon Sep 17 00:00:00 2001 From: Dmytro Meleshko Date: Fri, 12 Feb 2021 21:00:01 +0200 Subject: [PATCH] [zsh] improve path.zsh loading speed by not calling the entire rustc The effects of this were very significant on my good ol' winchester. --- zsh/path.zsh | 35 +++++++++++++++++++++++++---------- 1 file changed, 25 insertions(+), 10 deletions(-) diff --git a/zsh/path.zsh b/zsh/path.zsh index 16e85bd..d6a9bda 100644 --- a/zsh/path.zsh +++ b/zsh/path.zsh @@ -68,17 +68,32 @@ export GOPATH=~/go path_prepend path "$GOPATH/bin" # Rust -path_prepend path ~/.cargo/bin -# check if the Rust toolchain was installed via rustup -if rustup_home="$(rustup show home 2> /dev/null)" && - rust_sysroot="$(rustc --print sysroot 2> /dev/null)" && - [[ -d "$rustup_home" && -d "$rust_sysroot" && "$rust_sysroot" == "$rustup_home"/* ]] -then - # add paths of the selected Rust toolchain - path_prepend fpath "$rust_sysroot/share/zsh/site-functions" - path_prepend manpath "$rust_sysroot/share/man" +if [[ -f ~/.rustup/settings.toml ]]; then + # Make a low-effort attempt at quickly extracting the selected Rust toolchain + # from rustup's settings. The TOML file is obviously assumed to be well-formed + # and syntactically correct because virtually always it's manipulated with the + # use of rustup's CLI. Also a shortcut is taken: strings aren't unescaped + # because Rust toolchain names don't need escaping in strings. + # See also . + if rust_toolchain="$( + awk ' + match($0, /^default_toolchain = "(.+)"$/, matches) { + print matches[1]; + exit; + } + /^\[.+\]$/ { + exit; + } + ' ~/.rustup/settings.toml + )" && [[ -n "$rust_toolchain" ]]; then + rust_sysroot=~/.rustup/toolchains/"$rust_toolchain" + path_prepend path "$rust_sysroot"/bin + path_prepend fpath "$rust_sysroot"/zsh/site-functions + path_prepend manpath "$rust_sysroot"/share/man + fi + unset rust_toolchain rust_sysroot fi -unset rustup_home rust_sysroot +path_prepend path ~/.cargo/bin # add my binaries and completions path_prepend path "${ZSH_DOTFILES:h}/scripts"