diff --git a/config/fish/functions/fish_right_prompt.fish b/config/fish/functions/fish_right_prompt.fish index a81d078..933caee 100644 --- a/config/fish/functions/fish_right_prompt.fish +++ b/config/fish/functions/fish_right_prompt.fish @@ -5,6 +5,6 @@ function fish_right_prompt if contains rbenv (functions) echo $red(rbenv version-name)$normal else if contains asdf (functions) - echo $red(fast-asdf-ruby-version | awk '$1 == "ruby" { print $2 }')$normal + echo $red(fast-asdf-version ruby)$normal end end diff --git a/local/bin/fast-asdf-ruby-version b/local/bin/fast-asdf-ruby-version deleted file mode 100755 index 1e7bee6..0000000 --- a/local/bin/fast-asdf-ruby-version +++ /dev/null @@ -1,31 +0,0 @@ -#!/bin/bash - -asdf_path="$PWD" -asdf_info= - -# Make sure asdf is currently active. -if [ "$ASDF_DIR" ]; then - - # Traverse up directories until we hit an asdf directory or filesystem root. - while [ -z "$asdf_info" -o $asdf_path != '' ]; do - if [ -f "$asdf_path/.tool-versions" ]; then - asdf_path="$asdf_path/.tool-versions" - break - elif [ -f "$asdf_path/.ruby-version" ]; then - asdf_path="$asdf_path/.ruby-version" - break - else - asdf_path="${asdf_path%/*}" - fi - done - - # Use array manipulations and IFS to replace newlines with '/'. - if [ -f "$asdf_path" ]; then - IFS=$'\n' - asdf_info=($(<"$asdf_path")) - IFS='/' - asdf_info="${asdf_info[*]}" - fi -fi - -echo $asdf_info diff --git a/local/bin/fast-asdf-version b/local/bin/fast-asdf-version new file mode 100755 index 0000000..e0cbd3a --- /dev/null +++ b/local/bin/fast-asdf-version @@ -0,0 +1,56 @@ +#!/bin/zsh +# +# Usage: fast-asdf-version [tool] +# +# For example, `fast-asdf-version ruby` or `fast-asdf-version nodejs`. +tool="$1" + +if [ -z "$tool" ]; then + echo "Usage: fast-asdf-version [tool]" + exit 1 +fi + +declare -A legacy_version_files +legacy_version_files=( + [crystal]=".crystal-version" + [elixir]=".exenv-version" + [ruby]=".ruby-version" + [nodejs]=".node-version" +) + +asdf_version_file= +asdf_version= + +# Make sure ASDF is active. +if [ "$ASDF_DIR" ]; then + # Traverse up directories until we find a .tool-versions file, legacy file, + # or hit the filesystem root. + while [[ -z "$asdf_version_file" ]]; do + if [[ -f "$PWD/.tool-versions" ]]; then + asdf_version_file="$PWD/.tool-versions" + elif [[ -f "$PWD/$legacy_version_files[$tool]" ]]; then + asdf_version_file="$PWD/$legacy_version_files[$tool]" + elif [[ "$PWD" == "/" ]]; then + # If we hit the filesystem root and there was no version file there, + # check the user's home directory as a last-ditch effort + if [[ -f "$HOME/.tool-versions" ]]; then + asdf_version_file="$HOME/.tool-versions" + elif [[ -f "$HOME/$legacy_version_files[$tool]" ]]; then + asdf_version_file="$HOME/$legacy_version_files[$tool]" + fi + + break + fi + + cd .. + done + + # Finally, parse whichever version file we found + if [[ "$asdf_version_file" == *.tool-versions ]]; then + asdf_version=$(cat "$asdf_version_file" | awk -v tool=$tool '$1 == tool { print $2 }') + else + asdf_version=$(cat $asdf_version_file) + fi +fi + +echo $asdf_version