|
1 # Copyright (c) 2002-2009 Nokia Corporation and/or its subsidiary(-ies). |
|
2 # All rights reserved. |
|
3 # This component and the accompanying materials are made available |
|
4 # under the terms of the License "Eclipse Public License v1.0" |
|
5 # which accompanies this distribution, and is available |
|
6 # at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
7 # |
|
8 # Initial Contributors: |
|
9 # Nokia Corporation - initial contribution. |
|
10 # |
|
11 # Contributors: |
|
12 # |
|
13 # Description: |
|
14 # |
|
15 # |
|
16 # Description: |
|
17 # TableFormatter/Text.pm |
|
18 # |
|
19 |
|
20 package TableFormatter::Auto; |
|
21 |
|
22 use constant MAX_WIDTH => 75; |
|
23 use constant MAX_ROWS => 10; |
|
24 |
|
25 use strict; |
|
26 use TableFormatter; |
|
27 use TableFormatter::Text; |
|
28 use vars qw/@ISA/; |
|
29 |
|
30 @ISA = qw(TableFormatter); |
|
31 |
|
32 sub New { |
|
33 my $class = shift; |
|
34 my $inidata = shift; |
|
35 my $args = shift; |
|
36 |
|
37 my ($complex, $complexargs) = $args =~ m/^(\w+)\s*(.*)/; |
|
38 my $self = bless {}, (ref $class || $class); |
|
39 |
|
40 $self->CreateFormatters($complex, $inidata, $complexargs); |
|
41 |
|
42 return $self; |
|
43 } |
|
44 |
|
45 sub PrintTable { |
|
46 my $self = shift; |
|
47 my $data = shift; |
|
48 my $doHeading = shift; |
|
49 |
|
50 my $usecomplex = $self->UseComplexFormatter($data); |
|
51 |
|
52 my $formatter = ($usecomplex?$self->{complex}:$self->{simple}); |
|
53 return $formatter->PrintTable($data, $doHeading); |
|
54 } |
|
55 |
|
56 ## Private |
|
57 |
|
58 sub CreateFormatters { |
|
59 my $self = shift; |
|
60 my $complex = shift; |
|
61 my $inidata = shift; |
|
62 my $complexargs = shift; |
|
63 |
|
64 $self->{complex} ||= TableFormatter::CreateFormatter($complex, $inidata, $complexargs); |
|
65 $self->{simple} ||= TableFormatter::Text->New($inidata); |
|
66 } |
|
67 |
|
68 sub UseComplexFormatter { |
|
69 my $self = shift; |
|
70 my $data = shift; |
|
71 |
|
72 # Currently {maxrows} and {maxwidth} are unused |
|
73 my $maxrows = $self->{maxrows} || MAX_ROWS; |
|
74 my $maxwidth = $self->{maxwidth} || MAX_WIDTH; |
|
75 |
|
76 return 1 if (@$data > $maxrows); |
|
77 return 1 if ($self->TotalWidth($data) > $maxwidth); |
|
78 return 0; |
|
79 } |
|
80 |
|
81 sub TotalWidth { |
|
82 my $self = shift; |
|
83 my $data = shift; |
|
84 |
|
85 my $widths = $self->FindColWidths($data); |
|
86 my $total = 0; |
|
87 $total += $_ foreach (@$widths); |
|
88 return $total; |
|
89 } |
|
90 |
|
91 1; |
|
92 |
|
93 __END__ |
|
94 |
|
95 =head1 NAME |
|
96 |
|
97 TableFormatter/Text.pm - Formats tables in text |
|
98 |
|
99 =head1 INTERFACE |
|
100 |
|
101 =head2 New |
|
102 |
|
103 Creates a formatter. |
|
104 |
|
105 =head2 PrintTable |
|
106 |
|
107 Prints the table. Two arguments: firstly, a 2D array of the data. Secondly, a Boolean specifying whether the first row is a header row. |
|
108 |
|
109 =head1 KNOWN BUGS |
|
110 |
|
111 None. |
|
112 |
|
113 =head1 COPYRIGHT |
|
114 |
|
115 Copyright (c) 2002-2009 Nokia Corporation and/or its subsidiary(-ies). |
|
116 All rights reserved. |
|
117 This component and the accompanying materials are made available |
|
118 under the terms of the License "Eclipse Public License v1.0" |
|
119 which accompanies this distribution, and is available |
|
120 at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
121 |
|
122 Initial Contributors: |
|
123 Nokia Corporation - initial contribution. |
|
124 |
|
125 Contributors: |
|
126 |
|
127 Description: |
|
128 |
|
129 |
|
130 =cut |