[Po4a-commits] po4a/lib/Locale/Po4a Common.pm,1.9,1.10

Martin Quinson po4a-devel@lists.alioth.debian.org
Tue, 28 Jun 2005 19:37:37 +0000


Update of /cvsroot/po4a/po4a/lib/Locale/Po4a
In directory haydn:/tmp/cvs-serv9951/lib/Locale/Po4a

Modified Files:
	Common.pm 
Log Message:
Make the Text::WrapI18N and Term::ReadKey dependencies optional [FG]

Index: Common.pm
===================================================================
RCS file: /cvsroot/po4a/po4a/lib/Locale/Po4a/Common.pm,v
retrieving revision 1.9
retrieving revision 1.10
diff -u -d -r1.9 -r1.10
--- Common.pm	23 Jun 2005 15:42:37 -0000	1.9
+++ Common.pm	28 Jun 2005 19:37:34 -0000	1.10
@@ -29,23 +29,30 @@
 use 5.006;
 use strict;
 use warnings;
-use Text::WrapI18N qw(wrap $columns);
-use Term::ReadKey;
 
-sub setcolumns {
-    my ($col,$h,$cp,$hp);
-    eval {
-	($col,$h,$cp,$hp) = Term::ReadKey::GetTerminalSize(); 
-    };
-    if ($@) {
-       # GetTerminalSize failed. Maybe a terminal-less build or such strange condition. Let's play safe.
-       $col = 76;
+sub setcolumns() {
+    my $col=$ENV{COLUMNS};
+    if (!defined $col)
+    {
+        my @term=eval "use Term::ReadKey; Term::ReadKey::GetTerminalSize()";
+        $col=$term[0] if (!$@);
+        # If GetTerminalSize() failed we will fallback to a safe default.
+        # This can happen if Term::ReadKey is not available
+        # or this is a terminal-less build or such strange condition.
     }
-    $columns = $ENV{'COLUMNS'} || $col || 76;
-#    print "set col to $columns\n";
+    $col=76 if (!defined $col);
+    eval "use Text::WrapI18N qw(\$columns); \$columns = $col";
 }
 
-sub min {
+sub wrapi18n($$$) {
+    my ($header1, $header2, $msg) = @_;
+    setcolumns();
+    my $wrapped=eval "use Text::WrapI18N qw(wrap); wrap(\$header1, \$header2, \$msg)";
+    $wrapped="$header1$msg" if ($@);
+    return $wrapped;
+}
+
+sub min($$) {
     return $_[0] < $_[1] ? $_[0] : $_[1];
 }
 
@@ -123,12 +130,11 @@
 
 =cut
 
-sub wrap_msg {
+sub wrap_msg($@) {
     my $msg = shift;
     my @args = @_;
-    
-    setcolumns();
-    return wrap("", "", sprintf($msg, @args))."\n";
+
+    return wrapi18n("", "", sprintf($msg, @args))."\n";
 }
 
 =item wrap_mod($$@)
@@ -138,14 +144,13 @@
 
 =cut
 
-sub wrap_mod {
+sub wrap_mod($$@) {
     my ($mod, $msg) = (shift, shift);
     my @args = @_;
 
-    setcolumns();
     $mod .= ": ";
     my $spaces = " " x min(length($mod), 15);
-    return wrap($mod, $spaces, sprintf($msg, @args))."\n";
+    return wrapi18n($mod, $spaces, sprintf($msg, @args))."\n";
 }
 
 =item wrap_ref_mod($$$@)
@@ -156,11 +161,10 @@
 
 =cut
 
-sub wrap_ref_mod {
+sub wrap_ref_mod($$$@) {
     my ($ref, $mod, $msg) = (shift, shift, shift);
     my @args = @_;
 
-    setcolumns();
     if (!$mod) {
 	# If we don't get a module name, show the message like wrap_mod does
 	return wrap_mod($ref, $msg, @args);
@@ -168,7 +172,7 @@
 	$ref .= ": ";
 	my $spaces = " " x min(length($ref), 15);
 	$msg = "$ref($mod)\n$msg";
-	return wrap("", $spaces, sprintf($msg, @args))."\n";
+	return wrapi18n("", $spaces, sprintf($msg, @args))."\n";
     }
 }