Improve output padding.

Take 'length' of numbers in account.

Change GetMaxLength() accordingly and use that
new information in FormatOutput().

Fixes #53.

Signed-off-by: Thomas Hochstein <thh@inter.net>
This commit is contained in:
Thomas Hochstein 2013-09-02 10:13:35 +02:00
parent d28168419e
commit b342fcf030
2 changed files with 35 additions and 22 deletions

View file

@ -382,16 +382,17 @@ sub OutputData {
### $FileTempl: file name template (--filetemplate): filetempl-YYYY-MM ### $FileTempl: file name template (--filetemplate): filetempl-YYYY-MM
### $DBQuery : database query handle with executed query, ### $DBQuery : database query handle with executed query,
### containing $Month, $Key, $Value ### containing $Month, $Key, $Value
### $PadGroup : padding length for key field (optional) for 'pretty' ### $PadField : padding length for key field (optional) for 'pretty'
### $PadValue : padding length for value field (optional) for 'pretty'
my ($Format, $Comments, $GroupBy, $Precision, $ValidKeys, $FileTempl, my ($Format, $Comments, $GroupBy, $Precision, $ValidKeys, $FileTempl,
$DBQuery, $PadGroup) = @_; $DBQuery, $PadField, $PadValue) = @_;
my %ValidKeys = %{$ValidKeys} if $ValidKeys; my %ValidKeys = %{$ValidKeys} if $ValidKeys;
my ($FileName, $Handle, $OUT); my ($FileName, $Handle, $OUT);
our $LastIteration; our $LastIteration;
# define output types # define output types
my %LegalOutput; my %LegalOutput;
@LegalOutput{('dump',,'list','pretty')} = (); @LegalOutput{('dump','list','pretty')} = ();
# bail out if format is unknown # bail out if format is unknown
&Bleat(2,"Unknown output type '$Format'!") if !exists($LegalOutput{$Format}); &Bleat(2,"Unknown output type '$Format'!") if !exists($LegalOutput{$Format});
@ -427,7 +428,7 @@ sub OutputData {
$Handle = $OUT; $Handle = $OUT;
}; };
print $Handle &FormatOutput($Format, $Comments, $Caption, $Key, $Value, print $Handle &FormatOutput($Format, $Comments, $Caption, $Key, $Value,
$Precision, $PadGroup); $Precision, $PadField, $PadValue);
$LastIteration = $Caption; $LastIteration = $Caption;
}; };
close $OUT if ($FileTempl); close $OUT if ($FileTempl);
@ -443,9 +444,11 @@ sub FormatOutput {
### $Key : newsgroup, client, ... or $Month, as above ### $Key : newsgroup, client, ... or $Month, as above
### $Value : number of postings with that attribute ### $Value : number of postings with that attribute
### $Precision: number of digits right of decimal point (0 or 2) ### $Precision: number of digits right of decimal point (0 or 2)
### $PadGroup : padding length for key field (optional) for 'pretty' ### $PadField : padding length for key field (optional) for 'pretty'
### $PadValue : padding length for value field (optional) for 'pretty'
### OUT: $Output: formatted output ### OUT: $Output: formatted output
my ($Format, $Comments, $Caption, $Key, $Value, $Precision, $PadGroup) = @_; my ($Format, $Comments, $Caption, $Key, $Value, $Precision, $PadField,
$PadValue) = @_;
my ($Output); my ($Output);
# keep last caption in mind # keep last caption in mind
our ($LastIteration); our ($LastIteration);
@ -462,8 +465,13 @@ sub FormatOutput {
# output as a table # output as a table
$Output = sprintf ("# ----- %s:\n",$Caption) $Output = sprintf ("# ----- %s:\n",$Caption)
if ($Comments and (!defined($LastIteration) or $Caption ne $LastIteration)); if ($Comments and (!defined($LastIteration) or $Caption ne $LastIteration));
$Output .= sprintf ($PadGroup ? sprintf("%%-%us %%10.*f\n",$PadGroup) : # increase $PadValue for numbers with decimal point
"%s %.*f\n",$Key,$Precision,$Value); $PadValue += $Precision+1 if $Precision;
# add padding if $PadField is set; $PadValue HAS to be set then
$Output .= sprintf ($PadField ?
sprintf("%%-%us%%s %%%u.*f\n",$PadField,$PadValue) :
"%s%s %.*f\n",$Key,$Comments ? ':' : '',
$Precision,$Value);
}; };
return $Output; return $Output;
}; };
@ -485,26 +493,30 @@ sub SQLHierarchies {
################################################################################ ################################################################################
sub GetMaxLength { sub GetMaxLength {
################################################################################ ################################################################################
### get length of longest field in future query result ### get length of longest fields in future query result
### IN : $DBHandle : database handel ### IN : $DBHandle : database handle
### $Table : table to query ### $Table : table to query
### $Field : field to check ### $Field : field (key!, i.e. month, newsgroup, ...) to check
### $Value : field (value!, i.e. postings) to check
### $WhereClause : WHERE clause ### $WhereClause : WHERE clause
### $HavingClause: HAVING clause ### $HavingClause: HAVING clause
### @BindVars : bind variables for WHERE clause ### @BindVars : bind variables for WHERE clause
### OUT: $Length: length of longest instnace of $Field ### OUT: $FieldLength : length of longest instance of $Field
my ($DBHandle,$Table,$Field,$WhereClause,$HavingClause,@BindVars) = @_; ### $ValueLength : length of longest instance of $Value
my $DBQuery = $DBHandle->prepare(sprintf("SELECT MAX(LENGTH(%s)) ". my ($DBHandle,$Table,$Field,$Value,$WhereClause,$HavingClause,@BindVars) = @_;
"FROM %s %s %s",$Field,$Table, my $DBQuery = $DBHandle->prepare(sprintf("SELECT MAX(LENGTH(%s)),".
$WhereClause,$HavingClause ? "MAX(%s) ".
"FROM %s %s %s",$Field,,$Value,
$Table,$WhereClause,$HavingClause ?
'GROUP BY newsgroup' . $HavingClause . 'GROUP BY newsgroup' . $HavingClause .
' ORDER BY LENGTH(newsgroup) '. ' ORDER BY LENGTH(newsgroup) '.
'DESC LIMIT 1': '')); 'DESC LIMIT 1': ''));
$DBQuery->execute(@BindVars) or &Bleat(1,sprintf("Can't get field length ". $DBQuery->execute(@BindVars) or &Bleat(1,sprintf("Can't get field length ".
"for '%s' from table '%s': ". "for '%s' from table '%s': ".
"$DBI::errstr",$Field,$Table)); "$DBI::errstr",$Field,$Table));
my ($Length) = $DBQuery->fetchrow_array; my ($FieldLength,$ValueMax) = $DBQuery->fetchrow_array;
return $Length; my $ValueLength = length($ValueMax) if ($ValueMax);
return ($FieldLength,$ValueLength);
}; };
################################################################################ ################################################################################

View file

@ -154,9 +154,10 @@ if ($OptReportType and $OptReportType ne 'default') {
### get length of longest newsgroup name delivered by query ### get length of longest newsgroup name delivered by query
### for formatting purposes ### for formatting purposes
my $Field = ($GroupBy eq 'month') ? 'newsgroup' : 'month'; my $Field = ($GroupBy eq 'month') ? 'newsgroup' : 'month';
my $MaxLength = &GetMaxLength($DBHandle,$Conf{'DBTableGrps'}, my ($MaxLength,$MaxValLength) = &GetMaxLength($DBHandle,$Conf{'DBTableGrps'},
$Field,$SQLWhereClause,$SQLHavingClause, $Field,'postings',$SQLWhereClause,
@SQLBindNewsgroups); $SQLHavingClause,
@SQLBindNewsgroups);
### build and execute SQL query ### build and execute SQL query
my ($DBQuery); my ($DBQuery);
@ -241,7 +242,7 @@ if ($OptCaptions && $OptComments) {
# output data # output data
&OutputData($OptFormat,$OptComments,$GroupBy,$Precision, &OutputData($OptFormat,$OptComments,$GroupBy,$Precision,
$OptCheckgroupsFile ? $ValidGroups : '', $OptCheckgroupsFile ? $ValidGroups : '',
$OptFileTemplate,$DBQuery,$MaxLength); $OptFileTemplate,$DBQuery,$MaxLength,$MaxValLength);
### close handles ### close handles
$DBHandle->disconnect; $DBHandle->disconnect;