bash-completion: --formatter-property names v_0
authorFrantišek Kučera <franta-hg@frantovo.cz>
Sun, 16 Aug 2015 01:25:01 +0200
branchv_0
changeset 220 0bc544b38cfa
parent 219 3b1733fb3793
child 221 e38910065d55
bash-completion: --formatter-property names
java/sql-dk/src/info/globalcode/sql/dk/CLIStarter.java
java/sql-dk/src/info/globalcode/sql/dk/Functions.java
java/sql-dk/src/info/globalcode/sql/dk/InfoLister.java
scripts/bash_completion.pl
--- a/java/sql-dk/src/info/globalcode/sql/dk/CLIStarter.java	Sat Aug 15 16:15:30 2015 +0200
+++ b/java/sql-dk/src/info/globalcode/sql/dk/CLIStarter.java	Sun Aug 16 01:25:01 2015 +0200
@@ -29,6 +29,7 @@
 import info.globalcode.sql.dk.configuration.FormatterDefinition;
 import info.globalcode.sql.dk.configuration.Loader;
 import info.globalcode.sql.dk.configuration.NameIdentified;
+import info.globalcode.sql.dk.configuration.PropertyDeclaration;
 import info.globalcode.sql.dk.formatting.Formatter;
 import info.globalcode.sql.dk.formatting.FormatterContext;
 import info.globalcode.sql.dk.formatting.FormatterException;
@@ -41,6 +42,8 @@
 import java.io.PrintWriter;
 import java.sql.SQLException;
 import java.util.Collection;
+import java.util.Collections;
+import java.util.List;
 import java.util.logging.Level;
 import java.util.logging.LogRecord;
 import java.util.logging.Logger;
@@ -215,6 +218,7 @@
 				dir.mkdir();
 				writeBashCompletionHelperFile(configuration.getDatabases(), new File(dir, "databases"));
 				writeBashCompletionHelperFile(configuration.getAllFormatters(), new File(dir, "formatters"));
+				writeBashCompletionHelperFileForFormatterProperties(new File(dir, "formatter-properties"));
 			} catch (Exception e) {
 				log.log(Level.WARNING, "Unable to generate Bash completion helper files", e);
 			}
@@ -234,4 +238,29 @@
 			log.log(Level.FINER, "Not writing Bash completion helper file: {0} because configuration {1} has not been changed", new Object[]{target, Constants.CONFIG_FILE});
 		}
 	}
+
+	private void writeBashCompletionHelperFileForFormatterProperties(File formattersDir) throws ClassNotFoundException {
+		if (Constants.CONFIG_FILE.lastModified() > formattersDir.lastModified()) {
+			// TODO: delete old directory
+			formattersDir.mkdir();
+			for (FormatterDefinition fd : configuration.getAllFormatters()) {
+				File formatterDir = new File(formattersDir, fd.getName());
+				formatterDir.mkdir();
+
+				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 (PropertyDeclaration p : Functions.getPropertyDeclarations(c)) {
+						File propertyDir = new File(formatterDir, p.name());
+						propertyDir.mkdir();
+					}
+				});
+			}
+			log.log(Level.FINE, "Bash completion helper files was written in: {0}", formattersDir);
+		} else {
+			log.log(Level.FINER, "Not writing Bash completion helper directory: {0} because configuration {1} has not been changed", new Object[]{formattersDir, Constants.CONFIG_FILE});
+		}
+
+	}
 }
--- a/java/sql-dk/src/info/globalcode/sql/dk/Functions.java	Sat Aug 15 16:15:30 2015 +0200
+++ b/java/sql-dk/src/info/globalcode/sql/dk/Functions.java	Sun Aug 16 01:25:01 2015 +0200
@@ -18,6 +18,9 @@
 package info.globalcode.sql.dk;
 
 import info.globalcode.sql.dk.configuration.NameIdentified;
+import info.globalcode.sql.dk.configuration.PropertyDeclaration;
+import info.globalcode.sql.dk.configuration.PropertyDeclarations;
+import info.globalcode.sql.dk.formatting.Formatter;
 import java.io.BufferedReader;
 import java.io.File;
 import java.io.IOException;
@@ -189,6 +192,17 @@
 		return hierarchy;
 	}
 
+	public static PropertyDeclaration[] getPropertyDeclarations(Class<? extends Formatter> formatterClass) {
+		PropertyDeclarations properties = formatterClass.getAnnotation(PropertyDeclarations.class);
+
+		if (properties == null) {
+			PropertyDeclaration p = formatterClass.getAnnotation(PropertyDeclaration.class);
+			return p == null ? new PropertyDeclaration[]{} : new PropertyDeclaration[]{p};
+		} else {
+			return properties.value();
+		}
+	}
+
 	/**
 	 * TODO: support background or styles and move to ColorfulPrintWriter
 	 *
--- a/java/sql-dk/src/info/globalcode/sql/dk/InfoLister.java	Sat Aug 15 16:15:30 2015 +0200
+++ b/java/sql-dk/src/info/globalcode/sql/dk/InfoLister.java	Sun Aug 16 01:25:01 2015 +0200
@@ -188,17 +188,6 @@
 		}
 	}
 
-	private PropertyDeclaration[] getPropertyDeclarations(Class<? extends Formatter> formatterClass) {
-		PropertyDeclarations properties = formatterClass.getAnnotation(PropertyDeclarations.class);
-
-		if (properties == null) {
-			PropertyDeclaration p = formatterClass.getAnnotation(PropertyDeclaration.class);
-			return p == null ? new PropertyDeclaration[]{} : new PropertyDeclaration[]{p};
-		} else {
-			return properties.value();
-		}
-	}
-
 	private void listFormatterProperties(String formatterName) throws FormatterException, ConfigurationException {
 		FormatterDefinition fd = configurationProvider.getConfiguration().getFormatter(formatterName);
 		try {
@@ -223,7 +212,7 @@
 			List<Class<? extends Formatter>> hierarchy = Functions.getClassHierarchy(formatterClass, Formatter.class);
 			Collections.reverse(hierarchy);
 			hierarchy.stream().forEach((c) -> {
-				for (PropertyDeclaration p : getPropertyDeclarations(c)) {
+				for (PropertyDeclaration p : Functions.getPropertyDeclarations(c)) {
 					data.put(p.name(), propertyDeclarationToRow(p, c, printDeclaredIn));
 				}
 			});
--- a/scripts/bash_completion.pl	Sat Aug 15 16:15:30 2015 +0200
+++ b/scripts/bash_completion.pl	Sun Aug 16 01:25:01 2015 +0200
@@ -39,11 +39,38 @@
 
 my $databasesFile  = "$configDir/bash-completion/databases";
 my $formattersFile = "$configDir/bash-completion/formatters";
+my $formatterPropertiesDir = "$configDir/bash-completion/formatter-properties";
 
 print '#have sql-dk &&
-_sql_dk()
-{
-	local cur prev
+_sql_dk_bash_completion_find_formatter() {
+	local previous
+	for token in "$@"; do
+		if [ "x$previous" == "x--formatter" ]; then
+			echo -n "$token";
+		fi
+		previous="$token";
+	done
+}
+
+_sql_dk_bash_completion_formatter_property() {
+	if [ -n "$formatter" ]; then # TODO: this does not match formatter name in apostrophes or quotes
+		local formatter_dir='.$formatterPropertiesDir.'/$formatter
+		if [ -d  $formatter_dir ]; then
+			ls -1 $formatter_dir;
+		fi
+	fi
+}
+
+_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  
+}
+
+_sql_dk_bash_completion() {
+	local cur prev formatter
 
 	COMPREPLY=()
 	cur=${COMP_WORDS[COMP_CWORD]}
@@ -75,7 +102,17 @@
 		fi
 		return 0
 		;;
+	--formatter-property)
+		formatter=$( _sql_dk_bash_completion_find_formatter "${COMP_WORDS[@]}" );
+		COMPREPLY=( $( compgen -W "$(_sql_dk_bash_completion_formatter_property )" -- $cur ) ); 
+		return 0;
+		;;
 	esac;
+	
+	if [ "x${COMP_WORDS[COMP_CWORD-2]}" == "x--formatter-property" ]; then
+		COMPREPLY=( $( compgen -W "$(_sql_dk_bash_completion_formatter_property_choice )" -- $cur ) ); 
+		return 0;
+	fi
 
 	COMPREPLY=( $( compgen -W "
 ';
@@ -91,5 +128,5 @@
 
 }
 
-complete -F _sql_dk sql-dk
+complete -F _sql_dk_bash_completion sql-dk
 ';