App-ansifold

 view release on metacpan or  search on metacpan

lib/App/ansifold.pm  view on Meta::CPAN

    has smart     => ' s !   ' ;
    has expand    => ' x :-1 ' , default => $DEFAULT_EXPAND;
    has tabstop   => '   =i  ' , min => 1;
    has tabhead   => '   =s  ' ;
    has tabspace  => '   =s  ' ;
    has tabstyle  => '   =s  ' ;
    has discard   => '   =s@ ' , default => [];
    has colrm     => '       ' , default => $DEFAULT_COLRM;
    has help      => ' h     ' ;
    has version   => ' v     ' ;

    has '+boundary'  => any => [ qw(none word space) ];
    has '+ambiguous' => any => [ qw(wide narrow) ] ;

    has '+help' => sub {
	pod2usage
	    -verbose  => 99,
	    -sections => [ qw(SYNOPSIS VERSION) ];
    };

    has '+version' => sub {
	print "Version: $VERSION\n";
	exit;
    };

    has '+nonewline' => sub {
	$_->separate = "";
    };

    has '+linebreak' =>
	default => LINEBREAK_NONE,
	action => sub {
	    my($name, $value) = @_;
	    $_->$name = do {
		local $_ = $value;
		my $v = LINEBREAK_NONE;
		$v   |= LINEBREAK_ALL    if /all/i;
		$v   |= LINEBREAK_RUNIN  if /runin/i;
		$v   |= LINEBREAK_RUNOUT if /runout/i;
		$v;
	    };
	};

    has '+smart' => sub {
	my $smart = $_->{$_[0]} = $_[1];
	($_->boundary, $_->linebreak) = do {
	    if ($smart) {
		('word', LINEBREAK_ALL);
	    } else {
		('none', LINEBREAK_NONE);
	    }
	};
    };

    has width_index => default => [];

} no Getopt::EX::Hashed;

sub run {
    my $app = shift;
    local @ARGV = @_;
    $app->options->params->doit;
    return 0;
}

sub options {
    my $app = shift;

    for (@ARGV) {
	$_ = decode 'utf8', $_ unless utf8::is_utf8($_);
    }

    use Getopt::EX::Long qw(:DEFAULT ExConfigure Configure);
    ExConfigure BASECLASS => [ __PACKAGE__, 'Getopt::EX' ];
    Configure "bundling";
    $app->getopt || pod2usage();

    if ($app->colrm) {
	$app->separate = '';
	@{$app->width} = do {
	    unless (@ARGV > 0 and $ARGV[0] =~ /^\d+$/) {
		"-1";
	    } else {
		my $start = shift(@ARGV) - 1;
		if (@ARGV > 0 and $ARGV[0] =~ /^\d+$/) {
		    my $end = shift(@ARGV);
		    sprintf '%d,-%d,-1', $start, $end - $start;
		} else {
		    sprintf '%d,', $start;
		}
	    }
	}
    }

    if ($app->expand > 0) {
	$app->tabstop = $app->expand;
    }

    use charnames ':loose';
    for (@{$app}{qw(tabhead tabspace)}) {
	defined && length > 1 or next;
	$_ = charnames::string_vianame($_) || die "$_: invalid name\n";
    }

    return $app;
}

sub params {
    my $app = shift;

    use Getopt::EX::Numbers;
    my $numbers = Getopt::EX::Numbers->new;

    my @width = do {
	map {
	    if    (/^$/)      { 0  }			# empty
	    elsif (/^-?\d+$/) { $_ }			# number
	    elsif (/^(-?[-\d:]+) (?:\{(\d+)\})? $/x) {	# a:b:c:d{e}
		($numbers->parse($1)->sequence) x ($2 // 1);
	    }
	    elsif (/^(term|tty)$/) {



( run in 1.368 second using v1.01-cache-2.11-cpan-a9496e3eb41 )