autoComplete improved for better time folder filtering and fields improved for better field filtering

This commit is contained in:
Hamidreza 2025-04-11 10:13:53 +03:30
parent 8e87333973
commit d136ac0262
1 changed files with 74 additions and 33 deletions

View File

@ -1,48 +1,89 @@
PF_cFlags="--description --help --version"
AllTimeFolders=
__getAllTime(){
files=( $(ls) )
deleteFiles=(settings caseSetup cleanThisCase VTK runThisCase stl postprocess postProcess)
declare -A delk
for del in "${deleteFiles[@]}" ; do delk[$del]=1 ; done
# Tag items to remove, based on
for k in "${!files[@]}" ; do
[ "${delk[${files[$k]}]-}" ] && unset 'files[k]'
done
# Compaction
COMPREPLY=("${files[@]}")
AllTimeFolders="${files[@]}"
# Initialize empty array for time folders
local time_folders=()
# Loop through all directories in current folder
for dir in */; do
# Remove trailing slash
dir=${dir%/}
# Check if directory name is a valid floating point number
# This pattern matches integers and floating point numbers
if [[ $dir =~ ^[0-9]+(\.[0-9]+)?$ ]]; then
time_folders+=("$dir")
fi
done
# Set completion reply to the time folders
COMPREPLY=("${time_folders[@]}")
AllTimeFolders="${time_folders[@]}"
}
__getFields(){
__getAllTime
local -A unique_files=()
__getAllTime
local -A unique_files=()
# Files to exclude from suggestions
local exclude_files=("shapeHash" "pStructure" "particleInsertion" "p" "alpha" "U" "Sp" "Su" "phi")
declare -A exclude_dict
for dir in $AllTimeFolders; do
# Check if the directory exists
if [ ! -d "$dir" ]; then
continue # Skip to the next directory
fi
# Build exclude dictionary for faster lookups
for file in "${exclude_files[@]}"; do
exclude_dict["$file"]=1
done
files_in_dir=$(find "$dir" -maxdepth 1 -type f -printf '%f\n' | sort -u)
for dir in $AllTimeFolders; do
# Skip if not a directory
[ ! -d "$dir" ] && continue
# Add filenames to the associative array (duplicates will be overwritten)
while IFS= read -r filename; do
unique_files["$filename"]=1 # Just the key is important, value can be anything
done <<< "$files_in_dir"
# Find all files in this time directory
while IFS= read -r filename; do
# Skip empty lines and excluded files
[ -z "$filename" ] || [ "${exclude_dict[$filename]+exists}" ] && continue
done
COMPREPLY=("${!unique_files[@]}")
AllTimeFolders=
# Add to unique files
unique_files["$filename"]=1
done < <(find "$dir" -maxdepth 1 -type f -printf '%f\n')
done
# Set completion reply to the unique filenames
COMPREPLY=(${!unique_files[@]})
# Clear global variable
AllTimeFolders=
}
_pFlowToVTK(){
if [ "$3" == "--time" ] ; then
local cur="${COMP_WORDS[COMP_CWORD]}"
local prev="${COMP_WORDS[COMP_CWORD-1]}"
# Check if we're completing a field
local is_field=0
for ((i=1; i<COMP_CWORD; i++)); do
if [[ "${COMP_WORDS[i]}" == "--fields" ]]; then
is_field=1
break
fi
done
if [ "$prev" == "--time" ]; then
__getAllTime
elif [ "$3" == "--fields" ]; then
__getFields
elif [ "$prev" == "--fields" ] || [ $is_field -eq 1 ]; then
# We're completing field names
__getFields
# Filter the results based on the current word prefix
if [ -n "$cur" ]; then
local filtered=()
for item in "${COMPREPLY[@]}"; do
if [[ "$item" == "$cur"* ]]; then
filtered+=("$item")
fi
done
COMPREPLY=("${filtered[@]}")
fi
else
COMPREPLY=( $(compgen -W "$PF_cFlags --binary --no-geometry --no-particles --out-folder --time --separate-surfaces --fields" -- "$2") )
COMPREPLY=( $(compgen -W "$PF_cFlags --binary --no-geometry --no-particles --out-folder --time --separate-surfaces --fields" -- "$cur") )
fi
}
complete -F _pFlowToVTK pFlowToVTK