Text::ANSI::Fold - Text folding with ANSI sequence and Asian wide characters.
use Text::ANSI::Fold qw(ansi_fold); ($folded, $remain) = ansi_fold($text, $width, [ option ]); use Text::ANSI::Fold; my $f = new Text::ANSI::Fold width => 80, boundary => 'word'; $f->configure(ambiguous => 'wide'); ($folded, $remain) = $f->fold($text);
Text::ANSI::Fold provides capability to fold a text into two strings by given width. Text can include ANSI terminal sequences. If the text is divided in the middle of ANSI-effect region, reset sequence is appended to folded text, and recover sequence is prepended to trimmed string.
This module also support Unicode Asian full-width and non-spacing combining characters properly.
Use exported ansi_fold function to fold original text, with number of visual columns you want to cut off the text. Width parameter have to be a number greater than zero.
($folded, $remain) = ansi_fold($text, $width);
It returns a pair of strings. First one is folded text, and second is cut-off text.
This function returns at least one character in any situation. If you provide Asian wide string and just one column as width, it trims off the first wide character even if it does not fit to given width.
Default parameter can be set by configure class method:
Text::ANSI::Fold->configure(width => 80, padding => 1);
Then you don't have to pass second argument.
($folded, $remain) = ansi_fold($text);
Because second argument is always taken as width, use undef when using default width with additional parameter:
($folded, $remain) = ansi_fold($text, undef, padding => 1);
You can create an object to hold parameters, which is effective during object life time. For example,
my $f = new Text::ANSI::Fold width => 80, boundary => 'word';
makes an object folding on word boundaries with 80 columns width. Then you can use this without parameters.
$f->fold($text);
Use configure method to update parameters:
$f->configure(padding => 1);
Additional parameter can be specified on each call, and they precede saved value.
$f->fold($text, width => 40);
Experimentally fold object can hold string inside.
$f->configure(text => "text");
And folded string can be taken by retrieve method.
while ((my $folded = $f->retrieve) ne '') { print $folded; print "\n" if $folded !~ /\n\z/; }
Option parameter can be specified as name-value list for ansi_fold function as well as new and configure method.
ansi_fold($text, $width, boundary => 'word', ...); Text::ANSI::Fold->configure(boundary => 'word'); my $f = new Text::ANSI::Fold boundary => 'word'; $f->configure(boundary => 'word');
Specify folding width.
boundary option currently takes only "word" as a valid value. In this case, text is folded on word boundary. This occurs only when enough space will be provided to hold the word on next call with same width.
If padding option is given with true value, margin space is filled up with space character. Next code fills spaces if the given text is shorter than 80.
ansi_fold($text, 80, padding => 1);
padchar option specifies character used to fill up the remainder of given width.
ansi_fold($text, 80, padding => 1, padchar => '_');
Tells how to treat Unicode East Asian ambiguous characters. Default is "narrow" which means single column. Set "wide" to tell the module to treat them as wide character.
Text::ANSI::Fold was originally implemented in sdif command for long time, which provide side-by-side view for diff output. It is necessary to process output from cdif command which highlight diff output using ANSI escape sequences.
These modules provide a rich set of functions to handle string contains ANSI color terminal sequences. In contrast, Text::ANSI::Fold provides simple folding mechanism with minimum overhead. Also sdif need to process other than SGR (Select Graphic Rendition) color sequence, and non-spacing combining characters, those are not supported by these modules.
Copyright (C) Kazumasa Utashiro.
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
To install Text::ANSI::Fold, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Text::ANSI::Fold
CPAN shell
perl -MCPAN -e shell install Text::ANSI::Fold
For more information on module installation, please visit the detailed CPAN module installation guide.