Subject: | Allow control of tab size |
Currently, the documentation for Text::Diff::Table reads:
Assumes tab stops every 8 characters, as $DIETY [sic] intended.
It would be more useful to allow control over the tab size, especially
when wrapping text to fit a MAX WIDTH (RT #14468). The attached patch is
a quick hack to do that.
Subject: | Text_Diff_Table_patch_tabsize.txt |
--- old\Table.pm 2002-Aug-27 10:57:58
+++ new\Table.pm 2009-Mar-02 03:21:05
@@ -71,75 +71,75 @@
@ISA = qw( Text::Diff::Base Exporter );
-@EXPORT_OK = qw( expand_tabs );
+@EXPORT_OK = qw( expand_tabs tab_size );
$VERSION = 1.2;
@@ -77,86 +77,88 @@
use Carp;
-
+our $tab_size = 4;
my %escapes = map {
my $c =
- $_ eq '"' || $_ eq '$' ? qq{'$_'}
+ $_ eq '"' || $_ eq '$'
- : $_ eq "\\" ? qq{"\\\\"}
+ ? qq{'$_'}
+ : $_ eq "\\"
+ ? qq{"\\\\"}
- : qq{"$_"} ;
+ : qq{"$_"} ;
( ord eval $c => $_ )
} (
@@ -100,112 +102,113 @@
my $count=0;
$s =~ s{(\t)(\t*)|([^\t]+)}{
- if ( $1 ) {
+ if ( $1 ) {
- my $spaces = " " x ( 8 - $count % 8 + 8 * length $2 );
+ my $spaces = " " x ( $tab_size - $count % $tab_size + $tab_size * length $2 );
- $count = 0;
+ $count = 0;
- $spaces;
+ $spaces;
- }
- else {
+ } else {
- $count += length $3;
+ $count += length $3;
- $3;
+ $3;
- }
+ }
}ge;
@@ -124,145 +125,145 @@
{
- ## use utf8 if available. don't if not.
+ ## use utf8 if available. don't if not.
- my $escaper = <<'EOCODE' ;
+ my $escaper = <<'EOCODE' ;
- sub escape($) {
+ sub escape($) {
- use utf8;
+ use utf8;
- join "", map {
+ join "", map {
- $_ = ord;
+ $_ = ord;
- exists $escapes{$_}
+ exists $escapes{$_}
- ? $escapes{$_}
+ ? $escapes{$_}
- : sprintf( "\\x{%04x}", $_ ) ;
+ : sprintf( "\\x{%04x}", $_ ) ;
- } split //, shift ;
+ } split //, shift ;
- }
+ }
-
+ 1;
- 1;
EOCODE
- unless ( eval $escaper ) {
+ unless ( eval $escaper ) {
- $escaper =~ s/ *use *utf8 *;\n// or die "Can't drop use utf8;";
+ $escaper =~ s/ *use *utf8 *;\n// or die "Can't drop use utf8;";
- eval $escaper or die $@;
+ eval $escaper or die $@;
- }
+ }
}
@@ -186,206 +186,204 @@
: "*";
if ( $elt_type ne "*" ) {
- if ( $elt_type eq "=" || $A->[1] =~ /\S/ || $B->[1] =~ /\S/ ) {
+ if ( $elt_type eq "=" || $A->[1] =~ /\S/ || $B->[1] =~ /\S/ ) {
- $A->[1] = escape trim_trailing_line_ends expand_tabs $A->[1];
+ $A->[1] = escape trim_trailing_line_ends expand_tabs $A->[1];
- $B->[1] = escape trim_trailing_line_ends expand_tabs $B->[1];
+ $B->[1] = escape trim_trailing_line_ends expand_tabs $B->[1];
- }
- else {
+ } else {
- $A->[1] = escape $A->[1];
+ $A->[1] = escape $A->[1];
- $B->[1] = escape $B->[1];
+ $B->[1] = escape $B->[1];
- }
- }
+ }
- else {
+ } else {
## not using \z here for backcompat reasons.
$A->[1] =~ /^(\s*?)([^ \t].*?)?(\s*)(?![\n\r])$/s;
my ( $l_ws_A, $body_A, $t_ws_A ) = ( $1, $2, $3 );
- $body_A = "" unless defined $body_A;
+ $body_A = "" unless defined $body_A;
$B->[1] =~ /^(\s*?)([^ \t].*?)?(\s*)(?![\n\r])$/s;
my ( $l_ws_B, $body_B, $t_ws_B ) = ( $1, $2, $3 );
- $body_B = "" unless defined $body_B;
+ $body_B = "" unless defined $body_B;
my $added_escapes;
@@ -386,390 +384,388 @@
diffs.
-Assumes tab stops every 8 characters, as $DIETY intended.
+Set $Text::Diff::Table::tab_size to control tab stops (default 4).
Assumes all character codes >= 127 need to be escaped as hex codes, ie that the