bash-completion: --formatter-property choices: first working version v_0
authorFrantišek Kučera <franta-hg@frantovo.cz>
Sun, 16 Aug 2015 01:40:44 +0200
branchv_0
changeset 221 e38910065d55
parent 220 0bc544b38cfa
child 222 5ffeb18b6f85
bash-completion: --formatter-property choices: first working version
java/sql-dk/src/info/globalcode/sql/dk/CLIStarter.java
scripts/bash_completion.pl
--- a/java/sql-dk/src/info/globalcode/sql/dk/CLIStarter.java	Sun Aug 16 01:25:01 2015 +0200
+++ b/java/sql-dk/src/info/globalcode/sql/dk/CLIStarter.java	Sun Aug 16 01:40:44 2015 +0200
@@ -239,7 +239,7 @@
 		}
 	}
 
-	private void writeBashCompletionHelperFileForFormatterProperties(File formattersDir) throws ClassNotFoundException {
+	private void writeBashCompletionHelperFileForFormatterProperties(File formattersDir) throws ClassNotFoundException, FileNotFoundException {
 		if (Constants.CONFIG_FILE.lastModified() > formattersDir.lastModified()) {
 			// TODO: delete old directory
 			formattersDir.mkdir();
@@ -250,12 +250,20 @@
 				Class<Formatter> formatterClass = (Class<Formatter>) Class.forName(fd.getClassName());
 				List<Class<? extends Formatter>> hierarchy = Functions.getClassHierarchy(formatterClass, Formatter.class);
 				Collections.reverse(hierarchy);
-				hierarchy.stream().forEach((c) -> {
+				for (Class<? extends Formatter> c : hierarchy) {
 					for (PropertyDeclaration p : Functions.getPropertyDeclarations(c)) {
 						File propertyDir = new File(formatterDir, p.name());
 						propertyDir.mkdir();
+						File choicesFile = new File(propertyDir, "choices");
+						try (PrintWriter fw = new PrintWriter(choicesFile)) {
+							// TODO: refactor, move
+							if (p.type() == Boolean.class) {
+								fw.println("true");
+								fw.println("false");
+							}
+						}
 					}
-				});
+				}
 			}
 			log.log(Level.FINE, "Bash completion helper files was written in: {0}", formattersDir);
 		} else {
--- a/scripts/bash_completion.pl	Sun Aug 16 01:25:01 2015 +0200
+++ b/scripts/bash_completion.pl	Sun Aug 16 01:40:44 2015 +0200
@@ -63,10 +63,11 @@
 
 _sql_dk_bash_completion_formatter_property_choice() {
 	local property="${COMP_WORDS[COMP_CWORD-1]}";
-	echo true;
-	echo false;
-	echo "TODO_$property";
-	# TODO: find choices for the property  
+	local formatter_dir='.$formatterPropertiesDir.'/$formatter
+	local property_choices_file=$formatter_dir/$property/choices;
+	if [ -f $property_choices_file ]; then
+		cat $property_choices_file;
+	fi
 }
 
 _sql_dk_bash_completion() {
@@ -110,6 +111,7 @@
 	esac;
 	
 	if [ "x${COMP_WORDS[COMP_CWORD-2]}" == "x--formatter-property" ]; then
+		formatter=$( _sql_dk_bash_completion_find_formatter "${COMP_WORDS[@]}" );
 		COMPREPLY=( $( compgen -W "$(_sql_dk_bash_completion_formatter_property_choice )" -- $cur ) ); 
 		return 0;
 	fi