From f5ba30b901bf4cec5921ddcce574762999fb26f0 Mon Sep 17 00:00:00 2001
From: HRN <hamid.r.norouzi@gmail.com>
Date: Mon, 3 Feb 2025 19:15:08 +0330
Subject: [PATCH] autoCompelte for time folders and field names

---
 cmake/autoComplete | 46 ++++++++++++++++++++++++++++++++++++++++++----
 1 file changed, 42 insertions(+), 4 deletions(-)

diff --git a/cmake/autoComplete b/cmake/autoComplete
index 57de5139..6c289ce5 100644
--- a/cmake/autoComplete
+++ b/cmake/autoComplete
@@ -1,17 +1,55 @@
 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[@]}"
+}
+
+__getFields(){
+	__getAllTime
+	local -A unique_files=()
+
+	for dir in $AllTimeFolders; do
+	  # Check if the directory exists
+	  if [ ! -d "$dir" ]; then
+		continue  # Skip to the next directory
+	  fi
+		  
+	  files_in_dir=$(find "$dir" -maxdepth 1 -type f -printf '%f\n' | sort -u)
+	  
+	# 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"
+	  
+	done
+	COMPREPLY=("${!unique_files[@]}")
+	AllTimeFolders=
+}
 
 _pFlowToVTK(){
-    if [ "$3" == "--time" ]; then
-      COMPREPLY=( $(ls) )
+    if [ "$3" == "--time" ]  ; then
+        __getAllTime
+    elif [ "$3" == "--fields" ]; then 
+    	__getFields
     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" -- "$2") )
     fi
 }
 complete -F _pFlowToVTK pFlowToVTK
 
 _postprocessPhasicFlow(){
     if [ "$3" == "--time" ]; then
-      COMPREPLY=( $(ls) )
+      __getAllTime
     else
       COMPREPLY=( $(compgen -W "$PF_cFlags --out-folder --time --zeroFolder" -- "$2") )
     fi