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" PF_cFlags="--description --help --version"
AllTimeFolders= AllTimeFolders=
__getAllTime(){ __getAllTime(){
files=( $(ls) ) # Initialize empty array for time folders
deleteFiles=(settings caseSetup cleanThisCase VTK runThisCase stl postprocess postProcess) local time_folders=()
declare -A delk
for del in "${deleteFiles[@]}" ; do delk[$del]=1 ; done # Loop through all directories in current folder
# Tag items to remove, based on for dir in */; do
for k in "${!files[@]}" ; do # Remove trailing slash
[ "${delk[${files[$k]}]-}" ] && unset 'files[k]' dir=${dir%/}
done
# Compaction # Check if directory name is a valid floating point number
COMPREPLY=("${files[@]}") # This pattern matches integers and floating point numbers
AllTimeFolders="${files[@]}" 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(){ __getFields(){
__getAllTime __getAllTime
local -A unique_files=() 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
# Build exclude dictionary for faster lookups
for file in "${exclude_files[@]}"; do
exclude_dict["$file"]=1
done
for dir in $AllTimeFolders; do for dir in $AllTimeFolders; do
# Check if the directory exists # Skip if not a directory
if [ ! -d "$dir" ]; then [ ! -d "$dir" ] && continue
continue # Skip to the next directory
fi # Find all files in this time directory
while IFS= read -r filename; do
files_in_dir=$(find "$dir" -maxdepth 1 -type f -printf '%f\n' | sort -u) # Skip empty lines and excluded files
[ -z "$filename" ] || [ "${exclude_dict[$filename]+exists}" ] && continue
# Add filenames to the associative array (duplicates will be overwritten)
while IFS= read -r filename; do # Add to unique files
unique_files["$filename"]=1 # Just the key is important, value can be anything unique_files["$filename"]=1
done <<< "$files_in_dir" done < <(find "$dir" -maxdepth 1 -type f -printf '%f\n')
done
done
COMPREPLY=("${!unique_files[@]}") # Set completion reply to the unique filenames
AllTimeFolders= COMPREPLY=(${!unique_files[@]})
# Clear global variable
AllTimeFolders=
} }
_pFlowToVTK(){ _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 __getAllTime
elif [ "$3" == "--fields" ]; then elif [ "$prev" == "--fields" ] || [ $is_field -eq 1 ]; then
__getFields # 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 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 fi
} }
complete -F _pFlowToVTK pFlowToVTK complete -F _pFlowToVTK pFlowToVTK