scripts/bash_completion.pl
author František Kučera <franta-hg@frantovo.cz>
Sun, 16 Aug 2015 01:40:44 +0200
branchv_0
changeset 221 e38910065d55
parent 220 0bc544b38cfa
child 222 5ffeb18b6f85
permissions -rwxr-xr-x
bash-completion: --formatter-property choices: first working version
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@221
    66
	local formatter_dir='.$formatterPropertiesDir.'/$formatter
franta-hg@221
    67
	local property_choices_file=$formatter_dir/$property/choices;
franta-hg@221
    68
	if [ -f $property_choices_file ]; then
franta-hg@221
    69
		cat $property_choices_file;
franta-hg@221
    70
	fi
franta-hg@220
    71
}
franta-hg@220
    72
franta-hg@220
    73
_sql_dk_bash_completion() {
franta-hg@220
    74
	local cur prev formatter
franta-hg@81
    75
franta-hg@81
    76
	COMPREPLY=()
franta-hg@81
    77
	cur=${COMP_WORDS[COMP_CWORD]}
franta-hg@81
    78
	prev=${COMP_WORDS[COMP_CWORD-1]}
franta-hg@81
    79
franta-hg@81
    80
	case "$prev" in
franta-hg@159
    81
	--db | --test-connection | --list-jdbc-properties)
franta-hg@82
    82
		if [ -f '.$databasesFile.' ]; then
franta-hg@82
    83
			COMPREPLY=( $( compgen -W " $( cat '.$databasesFile.' ) " -- $cur ) )
franta-hg@82
    84
			return 0
franta-hg@82
    85
		fi
franta-hg@81
    86
		;;
franta-hg@209
    87
	--formatter | --list-formatter-properties)
franta-hg@82
    88
		if [ -f '.$formattersFile.' ]; then
franta-hg@82
    89
			COMPREPLY=( $( compgen -W " $( cat '.$formattersFile.' ) " -- $cur ) )
franta-hg@82
    90
		else
franta-hg@82
    91
			COMPREPLY=( $( compgen -W "
franta-hg@81
    92
';
franta-hg@81
    93
franta-hg@81
    94
while (<>) {
franta-hg@81
    95
	if (/"(.*?)".*? \/\/\s*bash-completion:formatter/) {
franta-hg@82
    96
		print "				$1\n";
franta-hg@81
    97
	}
franta-hg@81
    98
	last if (/\/\/\s*bash-completion:options/);
franta-hg@81
    99
}
franta-hg@81
   100
franta-hg@81
   101
franta-hg@82
   102
print '				" -- $cur ) );
franta-hg@82
   103
		fi
franta-hg@81
   104
		return 0
franta-hg@81
   105
		;;
franta-hg@220
   106
	--formatter-property)
franta-hg@220
   107
		formatter=$( _sql_dk_bash_completion_find_formatter "${COMP_WORDS[@]}" );
franta-hg@220
   108
		COMPREPLY=( $( compgen -W "$(_sql_dk_bash_completion_formatter_property )" -- $cur ) ); 
franta-hg@220
   109
		return 0;
franta-hg@220
   110
		;;
franta-hg@81
   111
	esac;
franta-hg@220
   112
	
franta-hg@220
   113
	if [ "x${COMP_WORDS[COMP_CWORD-2]}" == "x--formatter-property" ]; then
franta-hg@221
   114
		formatter=$( _sql_dk_bash_completion_find_formatter "${COMP_WORDS[@]}" );
franta-hg@220
   115
		COMPREPLY=( $( compgen -W "$(_sql_dk_bash_completion_formatter_property_choice )" -- $cur ) ); 
franta-hg@220
   116
		return 0;
franta-hg@220
   117
	fi
franta-hg@81
   118
franta-hg@81
   119
	COMPREPLY=( $( compgen -W "
franta-hg@81
   120
';
franta-hg@81
   121
franta-hg@81
   122
while (<>) {
franta-hg@81
   123
	if (/"(.*?)".*? \/\/\s*bash-completion:option/) {
franta-hg@81
   124
		print "			$1\n";
franta-hg@81
   125
	}
franta-hg@81
   126
}
franta-hg@81
   127
franta-hg@81
   128
print '		" -- $cur ) )
franta-hg@81
   129
	return 0
franta-hg@81
   130
franta-hg@81
   131
}
franta-hg@81
   132
franta-hg@220
   133
complete -F _sql_dk_bash_completion sql-dk
franta-hg@81
   134
';