What would be the best/appropriate algorithm for fuzzy search directory names? I would like to implement a bash completion which completes directory/file names using fuzzy search but it seems that the algorithm is dependent on the set of strings to match.

有帮助吗?

解决方案

Hmm... this is an interesting proposal. I would do it something like this:

First, parse the file path to get the text after the last slash

IFS='/' read -a filepath <<< '$string'
dirname=${filepath[${#filepath[@] - 1]}

Next, use find to get all the immediate subdirectories directories in the current path and add them to the bash completion option. You can use the =~ operator in place of fuzzy search, as described in this answer

for i in 'find . -type d -maxdepth 1'; do
  if [[ i =~ $dirname ]]; then
    //add to bash completion option, unsure how to do this part
  fi
done

However, note that =~ is a bash-only operator.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top