scripts/bash_completion.pl
author František Kučera <franta-hg@frantovo.cz>
Fri, 20 Dec 2019 12:55:01 +0100
branchv_0
changeset 251 97dbb1015d98
parent 250 aae5009bd0af
permissions -rwxr-xr-x
sqldk-relpipe convergence: integer type (relpipe already support signed integers)
     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, version 3 of the License.
     9 # 
    10 # This program is distributed in the hope that it will be useful,
    11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
    12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    13 # GNU General Public License for more details.
    14 # 
    15 # You should have received a copy of the GNU General Public License
    16 # along with this program. If not, see <http://www.gnu.org/licenses/>.
    17 
    18 
    19 # Parses Java source code from STDIN and generates script for BASH completion
    20 # Input (in this order):
    21 #	info/globalcode/sql/dk/Constants.java
    22 #	info/globalcode/sql/dk/formatting/*
    23 #	info/globalcode/sql/dk/CLIParser.java
    24 
    25 # TODO: support database/formatter names with spaces
    26 
    27 use strict;
    28 use warnings;
    29 
    30 my $configDir = "~";
    31 
    32 while (<>) {
    33 	if (/"(.*?)".*? \/\/\s*bash-completion:dir/) {
    34 		$configDir .= "/$1";
    35 		last;
    36 	}
    37 }
    38 
    39 my $databasesFile  = "$configDir/bash-completion/databases";
    40 my $formattersFile = "$configDir/bash-completion/formatters";
    41 my $defaultFormatterFile = "$configDir/bash-completion/default-formatter";
    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 			return 0;
    51 		fi
    52 		previous="$token";
    53 	done
    54 	
    55 	if [ -f '.$defaultFormatterFile.' ]; then
    56 		cat '.$defaultFormatterFile.'
    57 	fi
    58 }
    59 
    60 _sql_dk_bash_completion_formatter_property_name() {
    61 	if [ -n "$formatter" ]; then # TODO: this does not match formatter name in apostrophes or quotes
    62 		local formatter_dir='.$formatterPropertiesDir.'/$formatter
    63 		if [ -d  $formatter_dir ]; then
    64 			ls -1 $formatter_dir;
    65 		fi
    66 	fi
    67 }
    68 
    69 _sql_dk_bash_completion_formatter_property_choice() {
    70 	local property="${COMP_WORDS[COMP_CWORD-1]}";
    71 	local formatter_dir='.$formatterPropertiesDir.'/$formatter
    72 	local property_choices_file=$formatter_dir/$property/choices;
    73 	if [ -f $property_choices_file ]; then
    74 		cat $property_choices_file;
    75 	fi
    76 }
    77 
    78 _sql_dk_bash_completion() {
    79 	local cur prev formatter
    80 
    81 	COMPREPLY=()
    82 	cur=${COMP_WORDS[COMP_CWORD]}
    83 	prev=${COMP_WORDS[COMP_CWORD-1]}
    84 
    85 	case "$prev" in
    86 	--db | --test-connection | --list-jdbc-properties)
    87 		if [ -f '.$databasesFile.' ]; then
    88 			COMPREPLY=( $( compgen -W " $( cat '.$databasesFile.' ) " -- $cur ) )
    89 			return 0
    90 		fi
    91 		;;
    92 	--formatter | --list-formatter-properties)
    93 		if [ -f '.$formattersFile.' ]; then
    94 			COMPREPLY=( $( compgen -W " $( cat '.$formattersFile.' ) " -- $cur ) )
    95 		else
    96 			COMPREPLY=( $( compgen -W "
    97 ';
    98 
    99 while (<>) {
   100 	if (/"(.*?)".*? \/\/\s*bash-completion:formatter/) {
   101 		print "				$1\n";
   102 	}
   103 	last if (/\/\/\s*bash-completion:options/);
   104 }
   105 
   106 
   107 print '				" -- $cur ) );
   108 		fi
   109 		return 0
   110 		;;
   111 	--formatter-property)
   112 		formatter=$( _sql_dk_bash_completion_find_formatter "${COMP_WORDS[@]}" );
   113 		COMPREPLY=( $( compgen -W "$(_sql_dk_bash_completion_formatter_property_name )" -- $cur ) ); 
   114 		return 0;
   115 		;;
   116 	esac;
   117 	
   118 	if [ "x${COMP_WORDS[COMP_CWORD-2]}" == "x--formatter-property" ]; then
   119 		formatter=$( _sql_dk_bash_completion_find_formatter "${COMP_WORDS[@]}" );
   120 		COMPREPLY=( $( compgen -W "$(_sql_dk_bash_completion_formatter_property_choice )" -- $cur ) ); 
   121 		return 0;
   122 	fi
   123 
   124 	COMPREPLY=( $( compgen -W "
   125 ';
   126 
   127 while (<>) {
   128 	if (/"(.*?)".*? \/\/\s*bash-completion:option/) {
   129 		print "			$1\n";
   130 	}
   131 }
   132 
   133 print '		" -- $cur ) )
   134 	return 0
   135 
   136 }
   137 
   138 complete -F _sql_dk_bash_completion sql-dk
   139 ';