#!/usr/bin/env perl

use strict;
use warnings;
use utf8;

use App::ansifold qw($VERSION);

=encoding utf-8

=head1 NAME

ansifold - fold command handling ANSI terminal sequences

=head1 SYNOPSIS

ansifold [ options ]

    --width=80, -w80         Folding width
    --boundary=word, -s      Fold on word boundary
    --padding                Padding to margin space
    --padchar=_              Padding character
    --ambiguous=narrow|wide  Unicode ambiguous character handling
    --paragraph, -p          Print extra newline
    --truncate               Truncate folded text

=head1 DESCRIPTION

B<ansifold> is almost B<fold> compatible command utilizing
L<Text::ANSI::Fold> module, which enables to handle ANSI terminal
sequences and Unicode multibyte characters properly.

=head1 LICENSE

Copyright (C) Kazumasa Utashiro.

This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.

=head1 AUTHOR

Kazumasa Utashiro

=cut

use open IO => 'utf8';
binmode STDIN,  ":encoding(utf8)";
binmode STDOUT, ":encoding(utf8)";
binmode STDERR, ":encoding(utf8)";

use Pod::Usage;
use Text::ANSI::Fold;

my %opt = (
    width => 80,
    );
my @optargs = (
    "width|w=i"   => \$opt{width},
    "boundary=s"  => \$opt{boundary},
    "padding!"    => \$opt{padding},
    "padchar=s"   => \$opt{padchar},
    "ambiguous=s" => \$opt{ambiguous},
    "paragraph|p" => \$opt{paragraph},
    "truncate!"   => \$opt{truncate},
    "s" => sub {
	$opt{boundary} = 'word';
    },
    "version|v" => sub {
	print "Version: $VERSION\n";
	exit;
    },
    );

use Getopt::Long;
Getopt::Long::Configure("bundling");
GetOptions(@optargs) || pod2usage();

if ($opt{truncate}) {
    $opt{width} = [ $opt{width} ];
}

my $fold = new Text::ANSI::Fold do {
    map  { $_ => $opt{$_} }
    grep { defined $opt{$_} }
    qw(width boundary padding padchar ambiguous);
};

while (<>) {
    my $chomped = chomp;
    print join "\n", $fold->text($_)->chops;
    print "\n" if $chomped;
    print "\n" if $opt{paragraph};
}
