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 2200bc544b38cfa
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
     1.1 --- a/java/sql-dk/src/info/globalcode/sql/dk/CLIStarter.java	Sat Aug 15 16:15:30 2015 +0200
     1.2 +++ b/java/sql-dk/src/info/globalcode/sql/dk/CLIStarter.java	Sun Aug 16 01:25:01 2015 +0200
     1.3 @@ -29,6 +29,7 @@
     1.4  import info.globalcode.sql.dk.configuration.FormatterDefinition;
     1.5  import info.globalcode.sql.dk.configuration.Loader;
     1.6  import info.globalcode.sql.dk.configuration.NameIdentified;
     1.7 +import info.globalcode.sql.dk.configuration.PropertyDeclaration;
     1.8  import info.globalcode.sql.dk.formatting.Formatter;
     1.9  import info.globalcode.sql.dk.formatting.FormatterContext;
    1.10  import info.globalcode.sql.dk.formatting.FormatterException;
    1.11 @@ -41,6 +42,8 @@
    1.12  import java.io.PrintWriter;
    1.13  import java.sql.SQLException;
    1.14  import java.util.Collection;
    1.15 +import java.util.Collections;
    1.16 +import java.util.List;
    1.17  import java.util.logging.Level;
    1.18  import java.util.logging.LogRecord;
    1.19  import java.util.logging.Logger;
    1.20 @@ -215,6 +218,7 @@
    1.21  				dir.mkdir();
    1.22  				writeBashCompletionHelperFile(configuration.getDatabases(), new File(dir, "databases"));
    1.23  				writeBashCompletionHelperFile(configuration.getAllFormatters(), new File(dir, "formatters"));
    1.24 +				writeBashCompletionHelperFileForFormatterProperties(new File(dir, "formatter-properties"));
    1.25  			} catch (Exception e) {
    1.26  				log.log(Level.WARNING, "Unable to generate Bash completion helper files", e);
    1.27  			}
    1.28 @@ -234,4 +238,29 @@
    1.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});
    1.30  		}
    1.31  	}
    1.32 +
    1.33 +	private void writeBashCompletionHelperFileForFormatterProperties(File formattersDir) throws ClassNotFoundException {
    1.34 +		if (Constants.CONFIG_FILE.lastModified() > formattersDir.lastModified()) {
    1.35 +			// TODO: delete old directory
    1.36 +			formattersDir.mkdir();
    1.37 +			for (FormatterDefinition fd : configuration.getAllFormatters()) {
    1.38 +				File formatterDir = new File(formattersDir, fd.getName());
    1.39 +				formatterDir.mkdir();
    1.40 +
    1.41 +				Class<Formatter> formatterClass = (Class<Formatter>) Class.forName(fd.getClassName());
    1.42 +				List<Class<? extends Formatter>> hierarchy = Functions.getClassHierarchy(formatterClass, Formatter.class);
    1.43 +				Collections.reverse(hierarchy);
    1.44 +				hierarchy.stream().forEach((c) -> {
    1.45 +					for (PropertyDeclaration p : Functions.getPropertyDeclarations(c)) {
    1.46 +						File propertyDir = new File(formatterDir, p.name());
    1.47 +						propertyDir.mkdir();
    1.48 +					}
    1.49 +				});
    1.50 +			}
    1.51 +			log.log(Level.FINE, "Bash completion helper files was written in: {0}", formattersDir);
    1.52 +		} else {
    1.53 +			log.log(Level.FINER, "Not writing Bash completion helper directory: {0} because configuration {1} has not been changed", new Object[]{formattersDir, Constants.CONFIG_FILE});
    1.54 +		}
    1.55 +
    1.56 +	}
    1.57  }
     2.1 --- a/java/sql-dk/src/info/globalcode/sql/dk/Functions.java	Sat Aug 15 16:15:30 2015 +0200
     2.2 +++ b/java/sql-dk/src/info/globalcode/sql/dk/Functions.java	Sun Aug 16 01:25:01 2015 +0200
     2.3 @@ -18,6 +18,9 @@
     2.4  package info.globalcode.sql.dk;
     2.5  
     2.6  import info.globalcode.sql.dk.configuration.NameIdentified;
     2.7 +import info.globalcode.sql.dk.configuration.PropertyDeclaration;
     2.8 +import info.globalcode.sql.dk.configuration.PropertyDeclarations;
     2.9 +import info.globalcode.sql.dk.formatting.Formatter;
    2.10  import java.io.BufferedReader;
    2.11  import java.io.File;
    2.12  import java.io.IOException;
    2.13 @@ -189,6 +192,17 @@
    2.14  		return hierarchy;
    2.15  	}
    2.16  
    2.17 +	public static PropertyDeclaration[] getPropertyDeclarations(Class<? extends Formatter> formatterClass) {
    2.18 +		PropertyDeclarations properties = formatterClass.getAnnotation(PropertyDeclarations.class);
    2.19 +
    2.20 +		if (properties == null) {
    2.21 +			PropertyDeclaration p = formatterClass.getAnnotation(PropertyDeclaration.class);
    2.22 +			return p == null ? new PropertyDeclaration[]{} : new PropertyDeclaration[]{p};
    2.23 +		} else {
    2.24 +			return properties.value();
    2.25 +		}
    2.26 +	}
    2.27 +
    2.28  	/**
    2.29  	 * TODO: support background or styles and move to ColorfulPrintWriter
    2.30  	 *
     3.1 --- a/java/sql-dk/src/info/globalcode/sql/dk/InfoLister.java	Sat Aug 15 16:15:30 2015 +0200
     3.2 +++ b/java/sql-dk/src/info/globalcode/sql/dk/InfoLister.java	Sun Aug 16 01:25:01 2015 +0200
     3.3 @@ -188,17 +188,6 @@
     3.4  		}
     3.5  	}
     3.6  
     3.7 -	private PropertyDeclaration[] getPropertyDeclarations(Class<? extends Formatter> formatterClass) {
     3.8 -		PropertyDeclarations properties = formatterClass.getAnnotation(PropertyDeclarations.class);
     3.9 -
    3.10 -		if (properties == null) {
    3.11 -			PropertyDeclaration p = formatterClass.getAnnotation(PropertyDeclaration.class);
    3.12 -			return p == null ? new PropertyDeclaration[]{} : new PropertyDeclaration[]{p};
    3.13 -		} else {
    3.14 -			return properties.value();
    3.15 -		}
    3.16 -	}
    3.17 -
    3.18  	private void listFormatterProperties(String formatterName) throws FormatterException, ConfigurationException {
    3.19  		FormatterDefinition fd = configurationProvider.getConfiguration().getFormatter(formatterName);
    3.20  		try {
    3.21 @@ -223,7 +212,7 @@
    3.22  			List<Class<? extends Formatter>> hierarchy = Functions.getClassHierarchy(formatterClass, Formatter.class);
    3.23  			Collections.reverse(hierarchy);
    3.24  			hierarchy.stream().forEach((c) -> {
    3.25 -				for (PropertyDeclaration p : getPropertyDeclarations(c)) {
    3.26 +				for (PropertyDeclaration p : Functions.getPropertyDeclarations(c)) {
    3.27  					data.put(p.name(), propertyDeclarationToRow(p, c, printDeclaredIn));
    3.28  				}
    3.29  			});
     4.1 --- a/scripts/bash_completion.pl	Sat Aug 15 16:15:30 2015 +0200
     4.2 +++ b/scripts/bash_completion.pl	Sun Aug 16 01:25:01 2015 +0200
     4.3 @@ -39,11 +39,38 @@
     4.4  
     4.5  my $databasesFile  = "$configDir/bash-completion/databases";
     4.6  my $formattersFile = "$configDir/bash-completion/formatters";
     4.7 +my $formatterPropertiesDir = "$configDir/bash-completion/formatter-properties";
     4.8  
     4.9  print '#have sql-dk &&
    4.10 -_sql_dk()
    4.11 -{
    4.12 -	local cur prev
    4.13 +_sql_dk_bash_completion_find_formatter() {
    4.14 +	local previous
    4.15 +	for token in "$@"; do
    4.16 +		if [ "x$previous" == "x--formatter" ]; then
    4.17 +			echo -n "$token";
    4.18 +		fi
    4.19 +		previous="$token";
    4.20 +	done
    4.21 +}
    4.22 +
    4.23 +_sql_dk_bash_completion_formatter_property() {
    4.24 +	if [ -n "$formatter" ]; then # TODO: this does not match formatter name in apostrophes or quotes
    4.25 +		local formatter_dir='.$formatterPropertiesDir.'/$formatter
    4.26 +		if [ -d  $formatter_dir ]; then
    4.27 +			ls -1 $formatter_dir;
    4.28 +		fi
    4.29 +	fi
    4.30 +}
    4.31 +
    4.32 +_sql_dk_bash_completion_formatter_property_choice() {
    4.33 +	local property="${COMP_WORDS[COMP_CWORD-1]}";
    4.34 +	echo true;
    4.35 +	echo false;
    4.36 +	echo "TODO_$property";
    4.37 +	# TODO: find choices for the property  
    4.38 +}
    4.39 +
    4.40 +_sql_dk_bash_completion() {
    4.41 +	local cur prev formatter
    4.42  
    4.43  	COMPREPLY=()
    4.44  	cur=${COMP_WORDS[COMP_CWORD]}
    4.45 @@ -75,7 +102,17 @@
    4.46  		fi
    4.47  		return 0
    4.48  		;;
    4.49 +	--formatter-property)
    4.50 +		formatter=$( _sql_dk_bash_completion_find_formatter "${COMP_WORDS[@]}" );
    4.51 +		COMPREPLY=( $( compgen -W "$(_sql_dk_bash_completion_formatter_property )" -- $cur ) ); 
    4.52 +		return 0;
    4.53 +		;;
    4.54  	esac;
    4.55 +	
    4.56 +	if [ "x${COMP_WORDS[COMP_CWORD-2]}" == "x--formatter-property" ]; then
    4.57 +		COMPREPLY=( $( compgen -W "$(_sql_dk_bash_completion_formatter_property_choice )" -- $cur ) ); 
    4.58 +		return 0;
    4.59 +	fi
    4.60  
    4.61  	COMPREPLY=( $( compgen -W "
    4.62  ';
    4.63 @@ -91,5 +128,5 @@
    4.64  
    4.65  }
    4.66  
    4.67 -complete -F _sql_dk sql-dk
    4.68 +complete -F _sql_dk_bash_completion sql-dk
    4.69  ';