our
$VERSION
=
"1.06"
;
use
5.014;
use
open
IO
=>
'utf8'
,
':std'
;
our
$DEFAULT_UNEXPAND
;
Getopt::EX::Hashed->configure(
DEFAULT
=> [
is
=>
'rw'
]);
has
unexpand
=>
' u ! '
,
default
=>
$DEFAULT_UNEXPAND
;
has
all
=>
' a '
,
default
=> 1;
has
zap
=>
' z '
;
has
minimum
=>
' x :1 '
;
has
ambiguous
=>
' =s '
,
any
=> [
qw(wide narrow)
];
has
tabstop
=>
' t =i '
,
min
=> 1;
has
tabhead
=>
' =s '
;
has
tabspace
=>
' =s '
;
has
tabstyle
=>
' ts :s '
;
has
help
=>
' h '
;
has
version
=>
' v '
;
has
[
qw(+minimum +tabstop +tabhead +tabspace +tabstyle)
] =>
sub
{
my
(
$name
,
$val
) = (
"$_[0]"
,
$_
[1]);
if
(
$name
eq
'tabstyle'
and
$val
eq
''
) {
list_tabstyle();
exit
;
}
$_
->
$name
=
$val
;
Text::ANSI::Tabs->configure(
$name
=>
$val
);
};
has
'+help'
=>
sub
{
pod2usage
-verbose
=> 99,
-sections
=> [
qw(SYNOPSIS VERSION)
];
};
has
'+version'
=>
sub
{
print
"Version: $VERSION\n"
;
exit
;
};
has
ARGV
=>
default
=> [];
has
'<>'
=>
sub
{
if
(
$_
[0] =~ /^-([0-9]+)$/x) {
$_
->tabstop = $1 or
die
"$_[0]: invalid tabstop\n"
;
Text::ANSI::Tabs->configure(
tabstop
=> $1);
}
else
{
if
(
$_
[0] =~ /^-{1,2}+(.+)/) {
warn
"Unknown option: $1\n"
;
pod2usage();
}
push
@{
$_
->ARGV},
$_
[0];
}
};
}
no
Getopt::EX::Hashed;
sub
run {
my
$app
=
shift
;
local
@ARGV
=
map
{ utf8::is_utf8(
$_
) ?
$_
: decode(
'utf8'
,
$_
) }
@_
;
ExConfigure
BASECLASS
=> [ __PACKAGE__,
'Getopt::EX'
];
Configure
qw(bundling pass_through)
;
$app
->getopt || pod2usage();
@ARGV
= @{
$app
->ARGV};
my
$action
=
$app
->unexpand ? \
&ansi_unexpand
: \
&ansi_expand
;
local
$/ =
$app
->zap ?
undef
: $/;
while
(<>) {
print
$action
->(
$_
);
}
return
0;
}
sub
list_tabstyle {
my
%style
=
%Text::ANSI::Fold::TABSTYLE
;
my
$max
= max
map
length
,
keys
%style
;
for
my
$name
(
sort
keys
%style
) {
my
(
$head
,
$space
) = @{
$style
{
$name
}};
my
$tab
=
$head
.
$space
x 7;
printf
"%*s %s\n"
,
$max
,
$name
,
$tab
x 3;
}
}
1;