From 6d817d97b866d43c677a9f231c7cdea4ebe17b10 Mon Sep 17 00:00:00 2001 From: Hans Larsen Date: Fri, 21 Jan 2022 11:02:44 -0800 Subject: [PATCH] Optimization and allow find_file to receive regex Renamed the find_file function to find_file_up_re to better describe the behaviour. --- functions/fish_prompt.fish | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/functions/fish_prompt.fish b/functions/fish_prompt.fish index 2ba4252..93ed92d 100644 --- a/functions/fish_prompt.fish +++ b/functions/fish_prompt.fish @@ -895,7 +895,7 @@ function __bobthefish_prompt_desk -S -d 'Display current desk environment' set_color normal end -function __bobthefish_prompt_find_file -S -d 'Find a file by going up the parent directories' +function __bobthefish_prompt_find_file_up_re -S -d 'Find file(s) (as a regex), going up the parent directories' set -l dir "$argv[1]" set -l file "$argv[2]" @@ -903,12 +903,12 @@ function __bobthefish_prompt_find_file -S -d 'Find a file by going up the parent return 1 end - while [ "$dir" != '/' ] - if [ -f "$dir/$file" ] + while [ "$dir" ] + if string match -q -r "$dir/($file)\$" -- $dir/* return end - set dir (dirname "$dir") + set dir (__bobthefish_dirname "$dir") end return 1 end @@ -920,12 +920,11 @@ function __bobthefish_prompt_node -S -d 'Display current node version' set should_show 1 else if [ "$theme_display_node" = 'rc' ] begin - __bobthefish_prompt_find_file "$PWD" .nvmrc - or __bobthefish_prompt_find_file "$PWD" .node-version + __bobthefish_prompt_find_file_up_re "$PWD" '\.nvmrc|\.node-version' end and set should_show 1 else if [ "$theme_display_node" = 'package' ] - __bobthefish_prompt_find_file "$PWD" package.json + __bobthefish_prompt_find_file_up_re "$PWD" 'package\.json' and set should_show 1 end