scripts/bash_completion.pl
author František Kučera <franta-hg@frantovo.cz>
Sun, 16 Aug 2015 01:25:01 +0200
branchv_0
changeset 220 0bc544b38cfa
parent 209 8dfe037b3274
child 221 e38910065d55
permissions -rwxr-xr-x
bash-completion: --formatter-property names
     1 #!/usr/bin/perl
     2 
     3 # SQL-DK
     4 # Copyright © 2013 František Kučera (frantovo.cz)
     5 # 
     6 # This program is free software: you can redistribute it and/or modify
     7 # it under the terms of the GNU General Public License as published by
     8 # the Free Software Foundation, either version 3 of the License, or
     9 # (at your option) any later version.
    10 # 
    11 # This program is distributed in the hope that it will be useful,
    12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
    13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    14 # GNU General Public License for more details.
    15 # 
    16 # You should have received a copy of the GNU General Public License
    17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
    18 
    19 
    20 # Parses Java source code from STDIN and generates script for BASH completion
    21 # Input (in this order):
    22 #	info/globalcode/sql/dk/Constants.java
    23 #	info/globalcode/sql/dk/formatting/*
    24 #	info/globalcode/sql/dk/CLIParser.java
    25 
    26 # TODO: support database/formatter names with spaces
    27 
    28 use strict;
    29 use warnings;
    30 
    31 my $configDir = "~";
    32 
    33 while (<>) {
    34 	if (/"(.*?)".*? \/\/\s*bash-completion:dir/) {
    35 		$configDir .= "/$1";
    36 		last;
    37 	}
    38 }
    39 
    40 my $databasesFile  = "$configDir/bash-completion/databases";
    41 my $formattersFile = "$configDir/bash-completion/formatters";
    42 my $formatterPropertiesDir = "$configDir/bash-completion/formatter-properties";
    43 
    44 print '#have sql-dk &&
    45 _sql_dk_bash_completion_find_formatter() {
    46 	local previous
    47 	for token in "$@"; do
    48 		if [ "x$previous" == "x--formatter" ]; then
    49 			echo -n "$token";
    50 		fi
    51 		previous="$token";
    52 	done
    53 }
    54 
    55 _sql_dk_bash_completion_formatter_property() {
    56 	if [ -n "$formatter" ]; then # TODO: this does not match formatter name in apostrophes or quotes
    57 		local formatter_dir='.$formatterPropertiesDir.'/$formatter
    58 		if [ -d  $formatter_dir ]; then
    59 			ls -1 $formatter_dir;
    60 		fi
    61 	fi
    62 }
    63 
    64 _sql_dk_bash_completion_formatter_property_choice() {
    65 	local property="${COMP_WORDS[COMP_CWORD-1]}";
    66 	echo true;
    67 	echo false;
    68 	echo "TODO_$property";
    69 	# TODO: find choices for the property  
    70 }
    71 
    72 _sql_dk_bash_completion() {
    73 	local cur prev formatter
    74 
    75 	COMPREPLY=()
    76 	cur=${COMP_WORDS[COMP_CWORD]}
    77 	prev=${COMP_WORDS[COMP_CWORD-1]}
    78 
    79 	case "$prev" in
    80 	--db | --test-connection | --list-jdbc-properties)
    81 		if [ -f '.$databasesFile.' ]; then
    82 			COMPREPLY=( $( compgen -W " $( cat '.$databasesFile.' ) " -- $cur ) )
    83 			return 0
    84 		fi
    85 		;;
    86 	--formatter | --list-formatter-properties)
    87 		if [ -f '.$formattersFile.' ]; then
    88 			COMPREPLY=( $( compgen -W " $( cat '.$formattersFile.' ) " -- $cur ) )
    89 		else
    90 			COMPREPLY=( $( compgen -W "
    91 ';
    92 
    93 while (<>) {
    94 	if (/"(.*?)".*? \/\/\s*bash-completion:formatter/) {
    95 		print "				$1\n";
    96 	}
    97 	last if (/\/\/\s*bash-completion:options/);
    98 }
    99 
   100 
   101 print '				" -- $cur ) );
   102 		fi
   103 		return 0
   104 		;;
   105 	--formatter-property)
   106 		formatter=$( _sql_dk_bash_completion_find_formatter "${COMP_WORDS[@]}" );
   107 		COMPREPLY=( $( compgen -W "$(_sql_dk_bash_completion_formatter_property )" -- $cur ) ); 
   108 		return 0;
   109 		;;
   110 	esac;
   111 	
   112 	if [ "x${COMP_WORDS[COMP_CWORD-2]}" == "x--formatter-property" ]; then
   113 		COMPREPLY=( $( compgen -W "$(_sql_dk_bash_completion_formatter_property_choice )" -- $cur ) ); 
   114 		return 0;
   115 	fi
   116 
   117 	COMPREPLY=( $( compgen -W "
   118 ';
   119 
   120 while (<>) {
   121 	if (/"(.*?)".*? \/\/\s*bash-completion:option/) {
   122 		print "			$1\n";
   123 	}
   124 }
   125 
   126 print '		" -- $cur ) )
   127 	return 0
   128 
   129 }
   130 
   131 complete -F _sql_dk_bash_completion sql-dk
   132 ';