Text::SimpleTable's now go as wide as $ENV{COLUMNS} (jhannah)
Jay Hannah [Thu, 22 Jan 2009 01:53:47 +0000 (01:53 +0000)]
Patch written by Oleg Kostyuk <cub.uanic@gmail.com>

Also nuked Stats.pm report() -- my patch to 5.80/trunk a couple
weeks back added tests and everything over there.

Changes
lib/Catalyst.pm
lib/Catalyst/DispatchType/Chained.pm
lib/Catalyst/DispatchType/Path.pm
lib/Catalyst/DispatchType/Regex.pm
lib/Catalyst/Dispatcher.pm
lib/Catalyst/Stats.pm
lib/Catalyst/Utils.pm

diff --git a/Changes b/Changes
index f431d57..53d3d18 100644 (file)
--- a/Changes
+++ b/Changes
@@ -1,6 +1,8 @@
 # This file documents the revision history for Perl extension Catalyst.
 
 5.71000   2009-01-19 17:50:00
+        - Text::SimpleTable's go as wide as $ENV{COLUMNS} (jhannah)
+          Patch written by Oleg Kostyuk <cub.uanic@gmail.com>
         - backport go doc patch
         - added ru/ua translations to error page
         - backport stripping build_requires
index 0b75068..5294064 100644 (file)
@@ -956,7 +956,8 @@ EOF
         my @plugins = map { "$_  " . ( $_->VERSION || '' ) } $class->registered_plugins;
 
         if (@plugins) {
-            my $t = Text::SimpleTable->new(74);
+            my $column_width = Catalyst::Utils::term_width() - 6;
+            my $t = Text::SimpleTable->new($column_width);
             $t->row($_) for @plugins;
             $class->log->debug( "Loaded plugins:\n" . $t->draw . "\n" );
         }
@@ -988,7 +989,8 @@ EOF
     $class->setup_components;
 
     if ( $class->debug ) {
-        my $t = Text::SimpleTable->new( [ 63, 'Class' ], [ 8, 'Type' ] );
+        my $column_width = Catalyst::Utils::term_width() - 8 - 9;
+        my $t = Text::SimpleTable->new( [ $column_width, 'Class' ], [ 8, 'Type' ] );
         for my $comp ( sort keys %{ $class->components } ) {
             my $type = ref $class->components->{$comp} ? 'instance' : 'class';
             $t->row( $comp, $type );
@@ -2589,6 +2591,8 @@ obra: Jesse Vincent
 
 omega: Andreas Marienborg
 
+Oleg Kostyuk <cub.uanic@gmail.com>
+
 phaylon: Robert Sedlacek <phaylon@dunkelheit.at>
 
 sky: Arthur Bergman
index 4862310..dccd3c9 100644 (file)
@@ -4,6 +4,7 @@ use strict;
 use base qw/Catalyst::DispatchType/;
 use Text::SimpleTable;
 use Catalyst::ActionChain;
+use Catalyst::Utils;
 use URI;
 
 # please don't perltidy this. hairy code within.
@@ -43,9 +44,10 @@ sub list {
 
     return unless $self->{endpoints};
 
+    my $column_width = Catalyst::Utils::term_width() - 35 - 9;
     my $paths = Text::SimpleTable->new(
-                    [ 35, 'Path Spec' ], [ 36, 'Private' ]
-                );
+       [ 35, 'Path Spec' ], [ 36, 'Private' ], [ $column_width, 'Private' ]
+    );
 
     ENDPOINT: foreach my $endpoint (
                   sort { $a->reverse cmp $b->reverse }
index 0b4e843..9135d70 100644 (file)
@@ -3,6 +3,7 @@ package Catalyst::DispatchType::Path;
 use strict;
 use base qw/Catalyst::DispatchType/;
 use Text::SimpleTable;
+use Catalyst::Utils;
 use URI;
 
 =head1 NAME
@@ -25,7 +26,10 @@ Debug output for Path dispatch points
 
 sub list {
     my ( $self, $c ) = @_;
-    my $paths = Text::SimpleTable->new( [ 35, 'Path' ], [ 36, 'Private' ] );
+    my $column_width = Catalyst::Utils::term_width() - 35 - 9;
+    my $paths = Text::SimpleTable->new( 
+       [ 35, 'Path' ], [ $column_width, 'Private' ]
+    );
     foreach my $path ( sort keys %{ $self->{paths} } ) {
         my $display_path = $path eq '/' ? $path : "/$path";
         foreach my $action ( @{ $self->{paths}->{$path} } ) {
index a2dd81b..d6d283d 100644 (file)
@@ -3,6 +3,7 @@ package Catalyst::DispatchType::Regex;
 use strict;
 use base qw/Catalyst::DispatchType::Path/;
 use Text::SimpleTable;
+use Catalyst::Utils;
 use Text::Balanced ();
 
 =head1 NAME
@@ -25,7 +26,8 @@ Output a table of all regex actions, and their private equivalent.
 
 sub list {
     my ( $self, $c ) = @_;
-    my $re = Text::SimpleTable->new( [ 35, 'Regex' ], [ 36, 'Private' ] );
+    my $column_width = Catalyst::Utils::term_width() - 35 - 9;
+    my $re = Text::SimpleTable->new( [ 35, 'Regex' ], [ $column_width, 'Private' ] );
     for my $regex ( @{ $self->{compiled} } ) {
         my $action = $regex->{action};
         $re->row( $regex->{path}, "/$action" );
index 9ebd2b9..2b81250 100644 (file)
@@ -8,6 +8,7 @@ use Catalyst::Action;
 use Catalyst::ActionContainer;
 use Catalyst::DispatchType::Default;
 use Catalyst::DispatchType::Index;
+use Catalyst::Utils;
 use Text::SimpleTable;
 use Tree::Simple;
 use Tree::Simple::Visitor::FindByPath;
@@ -571,10 +572,9 @@ sub setup_actions {
 
     return unless $c->debug;
 
+    my $column_width = Catalyst::Utils::term_width() - 20 - 36 - 12;
     my $privates = Text::SimpleTable->new(
-        [ 20, 'Private' ],
-        [ 36, 'Class' ],
-        [ 12, 'Method' ]
+        [ 20, 'Private' ], [ 36, 'Class' ], [ $column_width, 'Method' ]
     );
 
     my $has_private = 0;
index 95300e7..b22ad04 100644 (file)
@@ -4,6 +4,7 @@ use strict;
 use warnings;
 use Time::HiRes qw/gettimeofday tv_interval/;
 use Text::SimpleTable ();
+use Catalyst::Utils;
 use Tree::Simple qw/use_weak_refs/;
 use Tree::Simple::Visitor::FindByUID;
 
@@ -92,12 +93,8 @@ sub elapsed {
 sub report {
     my $self = shift;
 
-# close any remaining open nodes
-    for (my $i = $#{$self->{stack}}; $i > 0; $i--) {
-    $self->profile(end => $self->{stack}->[$i]->getNodeValue->{action});
-    }
-
-    my $t = Text::SimpleTable->new( [ 62, 'Action' ], [ 9, 'Time' ] );
+    my $column_width = Catalyst::Utils::term_width() - 9 - 13;
+    my $t = Text::SimpleTable->new( [ $column_width, 'Action' ], [ 9, 'Time' ] );
     my @results;
     $self->{tree}->traverse(
                 sub {
index d27fa9c..f3d8c12 100644 (file)
@@ -20,6 +20,8 @@ See L<Catalyst>.
 
 =head1 DESCRIPTION
 
+Catalyst Utilities. 
+
 =head1 METHODS
 
 =head2 appprefix($class)
@@ -331,6 +333,45 @@ sub env_value {
     return;
 }
 
+=head2 term_width
+
+Try to guess terminal width to use with formatting of debug output
+
+All you need to get this work, is:
+
+1) Install Term::Size::Any, or
+
+2) Export $COLUMNS from your shell. 
+
+(Warning to bash users: 'echo $COLUMNS' may be showing you the bash
+variable, not $ENV{COLUMNS}. 'export COLUMNS=$COLUMNS' and you should see 
+that 'env' now lists COLUMNS.)
+
+As last resort, default value of 80 chars will be used.
+
+=cut
+
+my $_term_width;
+
+sub term_width {
+    return $_term_width if $_term_width;
+
+    my $width = eval '
+        use Term::Size::Any;
+        my ($columns, $rows) = Term::Size::Any::chars;
+        return $columns;
+    ';
+
+    if ($@) {
+        $width = $ENV{COLUMNS}
+            if exists($ENV{COLUMNS})
+            && $ENV{COLUMNS} =~ m/^\d+$/;
+    }
+
+    $width = 80 unless ($width && $width >= 80);
+    return $_term_width = $width;
+}
+
 =head1 AUTHORS
 
 Catalyst Contributors, see Catalyst.pm