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