Changeset 153

Show
Ignore:
Timestamp:
05/31/04 10:28:51 (4 years ago)
Author:
svn
Message:

manage now uses Text::Table for format_columns.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • manage/trunk/manage

    r150 r153  
    1515use IO::File; 
    1616use IO::Select; 
     17use Text::Table; 
    1718 
    1819  
     
    6566my @header = @{shift()}; 
    6667my @body = @{shift()}; 
    67 my ($width, $height) = GetTerminalSize(*STDIN); 
    68 my @width = (); 
    69 my $total = 0; 
    70  
    71         sub sizeof($) { 
    72         my $s = shift; 
    73  
    74                 $s =~ s/\e\[[^m]+m//g; 
    75         my @s = split(/\n/m, $s); 
    76         my $size = 0; 
    77  
    78                 for my $line (@s) { 
    79                         $size = length($line) if length($line) > $size; 
    80                 } 
    81                 return $size; 
    82         } 
    83  
    84  
    85         sub render_row($\@\@) { 
    86         my $width = shift; 
    87         my @row = @{$_[0]}; 
    88         my @width = @{$_[1]}; 
    89         my @out = (); 
    90         my $lines = 0; 
    91  
    92                 for (my $i = 1; $i < @row; ++$i) { 
    93                         $Text::Wrap::columns = $width[$i] + 1; 
    94                 my @lines = split(/\n/m, wrap("", "", $row[$i])); 
    95  
    96                         $out[$i] = \@lines; 
    97                         $lines = @lines if @lines > $lines; 
    98                 } 
    99          
    100         my $out = ""; 
    101                 for (my $i = 0; $i < $lines; ++$i) { 
    102                         for (my $j = 1; $j < @row; ++$j) { 
    103                         my $text = $out[$j][$i]; 
    104  
    105                                 $text = "" unless defined($text); 
    106                                 $out .= sprintf("%-$width[$j].$width[$j]s", $text); 
    107                         } 
    108                         $out .= "\n"; 
    109                 } 
    110                 chomp($out); 
    111                 $out; 
    112         } 
    113  
    114         # Find minimum column sizes 
    115         for (my $i = 1; $i < @header; ++$i) { 
    116                 $width[$i] = sizeof($header[$i]) + 1; 
    117  
    118                 for my $row (@body) { 
    119                 my $sz = sizeof($row->[$i]) + 1; 
    120  
    121                         $width[$i] = $sz if $sz > $width[$i]; 
    122                 } 
    123  
    124                 $total += $width[$i]; 
    125                 return if $BREAK; 
    126         } 
    127  
    128 my $scale = $width / $total; 
    129  
    130         # Scale widths up 
    131         for (my $i = 1; $i < @width; ++$i) { 
    132         my $new = floor($width[$i] * $scale); 
    133  
    134                 if ($width[$i] < 16 and $new < $width[$i]) { 
    135                 } else { 
    136                         $width[$i] = $new; 
    137                 } 
    138         } 
    139  
    140         # Actually display the table 
    141         print($header[0] . render_row($width, @header, @width) . "\e[0m\n"); 
    142         for my $row (@body) { 
    143  
    144                 last if $BREAK; 
    145                 print($row->[0] . render_row($width, @{$row}, @width) . "\e[0m\n"); 
    146         } 
     68my $hc = shift(@header); 
     69my @bc; 
     70my $text; 
     71my $table = new Text::Table(@header); 
     72        for my $r (@body) { 
     73                push(@bc, shift(@$r)); 
     74        } 
     75 
     76        $table->load(@body); 
     77        $text = $table->title(); 
     78        chomp($text); 
     79        print("$hc$text\e[0m\n"); 
     80        for (my $line = 0; $line < $table->body_height(); ++$line) { 
     81                $text = $table->body($line); 
     82                chomp($text); 
     83                print($bc[$line] . $text . "\e[0m\n"); 
     84        } 
     85#my ($width, $height) = GetTerminalSize(*STDIN); 
     86#my @width = (); 
     87#my $total = 0; 
     88
     89#       sub sizeof($) { 
     90#       my $s = shift; 
     91
     92#               $s =~ s/\e\[[^m]+m//g; 
     93#       my @s = split(/\n/m, $s); 
     94#       my $size = 0; 
     95
     96#               for my $line (@s) { 
     97#                       $size = length($line) if length($line) > $size; 
     98#               } 
     99#               return $size; 
     100#       } 
     101
     102
     103#       sub render_row($\@\@) { 
     104#       my $width = shift; 
     105#       my @row = @{$_[0]}; 
     106#       my @width = @{$_[1]}; 
     107#       my @out = (); 
     108#       my $lines = 0; 
     109
     110#               for (my $i = 1; $i < @row; ++$i) { 
     111#                       $Text::Wrap::columns = $width[$i] + 1; 
     112#               my @lines = split(/\n/m, wrap("", "", $row[$i])); 
     113
     114#                       $out[$i] = \@lines; 
     115#                       $lines = @lines if @lines > $lines; 
     116#               } 
     117#        
     118#       my $out = ""; 
     119#               for (my $i = 0; $i < $lines; ++$i) { 
     120#                       for (my $j = 1; $j < @row; ++$j) { 
     121#                       my $text = $out[$j][$i]; 
     122
     123#                               $text = "" unless defined($text); 
     124#                               $out .= sprintf("%-$width[$j].$width[$j]s", $text); 
     125#                       } 
     126#                       $out .= "\n"; 
     127#               } 
     128#               chomp($out); 
     129#               $out; 
     130#       } 
     131
     132#       # Find minimum column sizes 
     133#       for (my $i = 1; $i < @header; ++$i) { 
     134#               $width[$i] = sizeof($header[$i]) + 1; 
     135
     136#               for my $row (@body) { 
     137#               my $sz = sizeof($row->[$i]) + 1; 
     138
     139#                       $width[$i] = $sz if $sz > $width[$i]; 
     140#               } 
     141
     142#               $total += $width[$i]; 
     143#               return if $BREAK; 
     144#       } 
     145
     146#my $scale = $width / $total; 
     147
     148#       # Scale widths up 
     149#       for (my $i = 1; $i < @width; ++$i) { 
     150#       my $new = floor($width[$i] * $scale); 
     151
     152#               if ($width[$i] < 16 and $new < $width[$i]) { 
     153#               } else { 
     154#                       $width[$i] = $new; 
     155#               } 
     156#       } 
     157
     158#       # Actually display the table 
     159#       print($header[0] . render_row($width, @header, @width) . "\e[0m\n"); 
     160#       for my $row (@body) { 
     161
     162#               last if $BREAK; 
     163#               print($row->[0] . render_row($width, @{$row}, @width) . "\e[0m\n"); 
     164#       } 
    147165} 
    148166 
     
    157175entered exactly as shown. Words enclosed in \e[1m< >\e[22m are user defined. 
    158176", 
    159 #       'fw' => { 
    160 #               HELP => 'Firewall management.', 
    161 #               'log' => { 
    162 #                       HELP => 'Firewall log manipulation.',  
    163 #               }, 
    164 #               'watch' => { 
    165 #                       HELP => 'Put firewall into watch mode.', 
    166 #               }, 
    167 #       }, 
    168177        'set' => { 
    169178                HELP => 'Query/modify settings.', 
     
    214223 
    215224                                for my $key (sort keys %CONF) { 
    216                                         push(@rows, [$ALTCOLOURS[++$row % 2], $key, ${$CONF{$key}->{value}}, $CONF{$key}->{help}]); 
     225                                        push(@rows, [$ALTCOLOURS[++$row % 2], sprintf("%-12s", $key), sprintf("%-16s", ${$CONF{$key}->{value}}), $CONF{$key}->{help}]); 
    217226                                }    
    218227                                format_columns(