—#!/usr/bin/env perl
use
strict;
use
warnings;
BEGIN {
if
($0 =~ /expand$/) {
## when executed as 'ansiexpand'
$App::ansifold::DEFAULT_WIDTH
= -1;
$App::ansifold::DEFAULT_EXPAND
= -1;
}
elsif
($0 =~ /colrm$/) {
## when executed as 'ansicolrm'
$App::ansifold::DEFAULT_SEPARATE
=
""
;
$App::ansifold::DEFAULT_COLRM
= 1;
}
elsif
($0 =~ /cut$/) {
## when executed as 'ansicut'
$App::ansifold::DEFAULT_SEPARATE
=
""
;
}
}
use
App::ansifold;
exit
App::ansifold->new->perform(
splice
@ARGV
);
__END__
=encoding utf-8
=head1 NAME
ansifold/ansicolrm/ansicut - fold/colrm/cut command handling ANSI terminal sequences
=head1 SYNOPSIS
ansifold [ options ]
-w# --width=# Folding width (default 72)
--boundary=word|space Fold on word boundary
--padding[=#] Padding to margin space
--padchar=_ Default padding character
--prefix=string Set prefix string (default empty)
--autoindent=pattern Set auto-indent pattern
--keepindent Preserve indent string
--ambiguous=narrow|wide Unicode ambiguous character handling
-p --paragraph Print extra newline
-r --refill Join paragraph into single line first
--separate=string Set the output separator string (default newline)
-n --nonewline Same as --separate ''
--lb=# --linebreak=mode Line-break mode (all, runin, runout, none)
--runin=# Run-in width (default 4)
--runout=# Run-out width (default 4)
--runlen=# Set run-in and run-out both
--splitwide[=#] Split in the middle of wide character
-s --smart Same as --boundary=word --linebreak=all
-x[#] --expand[=#] Expand tabs
--tabstop=n Tab-stop position (default 8)
--tabhead=char Tab-head character (default space)
--tabspace=char Tab-space character (default space)
--tabstyle=style Tab expansion style (shade, dot, symbol)
--colrm start [ end ] colrm(1) command compatible
-c# --cut list cut(1) command compatible
-h --help Show help message
-v --version Show version
ansicolrm [ options ]
ansicut -c list
=head1 VERSION
Version 1.32
=head1 DESCRIPTION
B<ansifold> is a L<fold(1)> compatible command utilizing
L<Text::ANSI::Fold> module, which enables to handle ANSI terminal
sequences.
B<ansicolrm> works like L<colrm(1)> command. This is an alias for
B<ansifold> command and works exactly same except option B<--colrm> is
enabled by default.
B<ansicut> works like L<cut(1)> command. This is an alias for
B<ansifold> command and works exactly same except default output
separator string is set as empty by default. Support only B<-c> (or
B<--cut>) option of the original L<cut(1)> command.
=head2 FOLD BY WIDTH
B<ansifold> folds lines in 72 column by default. Use option B<-w> to
change the folding width.
$ ansifold -w132
Single field is used repeatedly for the same line.
With option B<--padding>, remained columns are filled by padding
character, space by default, or specified by optional value like
C<--padding=_>. Default padding character can be set by B<--padchar>
option.
B<ansifold> handles Unicode multi-byte characters properly. Option
B<--ambiguous> takes I<wide> or I<narrow> and it specifies the visual
width of Unicode ambiguous characters.
If the last character is full-width and must be wrapped in the middle
of it, it is wrapped just before the character. If padding is
specified, then one padding character is inserted. If you really want
to keep the wrapping position, use the C<--stripwide> option.
=head2 TERMINAL WIDTH and CALCULATION
If the width argument begins with C<=>, it is interpreted as an RPN
(Reverse Polish Notation) expression with the terminal width as the
initial value. Therefore,
ansifold -w=
will wrap at the width of the terminal, and
ansifold -w=2/
will wrap at half the width of the terminal.
=head2 MULTIPLE WIDTH
Unlike the original fold(1) command, multiple numbers can be
specified.
$ LANG=C date | ansifold -w 3,1,3,1,2 | cat -n
1 Wed
2
3 Dec
4
5 19
With multiple fields, unmatched part is discarded as in the above
example. So you can truncate lines by putting comma at the end of
single field.
ansifold -w80,
Option C<-w80,> is equivalent to C<-w80,0>. Zero width is ignored
when seen as a final number, but not ignored otherwise.
If the data is shorter and there is no corresponding string for the
field, an empty string is returned. If the padding option is
specified, the field is padded to the given width.
=head2 NEGATIVE WIDTH
Negative number fields are discarded.
$ LANG=C date | ansifold -w 3,-1,3,-1,2
Wed
Dec
19
If the final width is negative, it is not discarded but takes all the
rest instead. So next commands do the same thing.
$ colrm 7 10
$ ansifold -nw 6,-4,-1
Option C<--width -1> does nothing effectively. Using it with
B<--expand> option implements ANSI/Unicode aware L<expand(1)> command.
$ ansifold --expand --width -1
This can be written as this.
$ ansifold -xw-1
=head2 NUMBERS
Number description is handled by L<Getopt::EX::Numbers> module, and
consists of C<start>, C<end>, C<step> and C<length> elements. For
example,
$ echo AABBBBCCCCCCDDDDDDDDEEEEEEEEEE | ansifold -w 2:10:2
is equivalent to:
$ echo AABBBBCCCCCCDDDDDDDDEEEEEEEEEE | ansifold -w 2,4,6,8,10
and produces output like this:
AA
BBBB
CCCCCC
DDDDDDDD
EEEEEEEEEE
=head2 SEPARATOR/TERMINATOR
Option B<-n> eliminates newlines between columns.
$ LANG=C date | ansifold -w 3,-1,3,-1,2 -n
WedDec19
Option B<--separate> set the output separator string.
$ echo ABCDEF | ansifold --separate=: -w 1,0,1,0,1,-1
A::B::C:DEF
Option B<-n> is a short-cut for C<--separate ''>.
Option B<--paragraph> (or B<-p>) print extra newline after each line.
This is convenient when a paragraph is made up of single line, like
microsoft word document. The B<-p> option can be repeated multiple
times and will output that many newline characters.
=head2 PREFIX
=head3 B<--prefix>=I<string>
If a string is given by B<--prefix> option, that string is inserted at
the beginning of each folded text. This is convenient to produce
indented text block. Because the first line is not affected, insert
appropiate prefix if necessary. Originally made for
L<App::Greple::frame> module.
=head3 B<--autoindent>=I<pattern>
An experimental B<--autoindent> option takes a regex pattern for the
indent label, and set the prefix string as a space string of that
label length. For example, command C<ps auxgw> produce very long line
output and you may want to fold C<COMMAND> portion with appropiate
indentation. In this case use B<--autoindent> option like this:
$ ps axgw | ansifold --autoindent '.*TIME (?=COMMAND)' -w= --boundary=word
PID TT STAT TIME COMMAND
1 ?? Ss 817:25.87 /sbin/launchd
354 ?? S 4:30.01 /System/Applications/TextEdit.app/Contents/
MacOS/TextEdit
522 ?? Ss 2:50.67 /System/Library/PrivateFrameworks/Uninstall.
framework/Resources/uninstalld
=head3 B<--keepindent>
If the C<--keepindent> option is specified, the string matched by
C<--autoindent> is inserted at the beginning of the line rather than
indenting with spaces.
=head2 REFILL
Option B<--refill> (or B<-r>) makes the command to run in paragraph
mode, which read consecutive non-blank lines at once, and join them
into single line before processing. So all paragraphs are reformatted
by new text width. You can use this with B<--autoindent> option.
Option B<-rw-1> will just fill paragraphs without reformatting.
=head1 LINE BREAKING
Line break adjustment is supported for ASCII word boundaries. As for
Japanese, more complicated prohibition processing is performed. Use
option B<-s> to enable everything.
=head2 B<--boundary>=I<word>|I<space>
This option prohibit breaking line in the middle of ASCII/Latin word.
Context of word is defined by option value; I<word> means
alpha-numeric sequence, while I<space> means simply non-space
printables.
=head2 B<--linebreak>=I<all>|I<runin>|I<runout>|I<none>, B<--lb>=...
Option B<--linebreak> takes a value of I<all>, I<runin>, I<runout> or
I<none>. Default value is I<none>.
When B<--linebreak> option is enabled, if the cut-off text start with
space or prohibited characters (e.g. closing parenthesis), they are
ran-in at the end of current line as much as possible.
If the trimmed text end with prohibited characters (e.g. opening
parenthesis), they are ran-out to the head of next line, provided it
fits to maximum width.
=head2 B<--runin>=I<width>, B<--runout>=I<width>
=head2 B<--runlen>=I<width>
Maximum width of run-in/run-out characters are defined by B<--runin>
and B<--runout> option. Default values are 4.
Option B<--runlen> set both run-in/run-out width at once.
=head2 B<--splitwide>[=I<lefthalf>[I<righthalf>]]
If it becomes necessary to break in the middle of a wide character,
split the character into left and right half. Replacement characters
are C<\N{LEFT HALF BLACK CIRCLE}> (C<◖>) and C<\N{RIGHT HALF BLACK
CIRCLE}> (C<◗>) by default.
If a parameter is given, the first character is used as the left half.
The next character, if any, is used as the right half, otherwise the
first character is used.
=head2 B<--smart>, B<-s>
Option B<--smart> (or simply B<-s>) set both B<--boundary=word> and
B<--linebreak=all>, and enables all smart text formatting capability.
Use option B<--boundary=space> if you want the command to behave more
like B<-s> option of L<fold(1)> command.
=head1 TAB EXPANSION
=head2 B<--expand>
Option B<--expand> (or B<-x>) enables tab character expansion.
$ ansifold --expand
Takes optional number for tabstop and it precedes to B<--tabstop>
option.
$ ansifold -x4w-1
If the command is executed with the name C<ansiexpand>, it works as if
the B<--expand> option were given, and set default folding width to
-1. L<App::ansiexpand> is a bit more sophisticated and we recommend
using that one rather.
=head2 B<--tabhead>, B<--tabspace>
Each tab character is converted to B<tabhead> and following
B<tabspace> characters (both are space by default). They can be
specified by B<--tabhead> and B<--tabspace> option. If the option
value is longer than single characger, it is evaluated as unicode
name. Next example makes tab character visible keeping text layout.
$ ansifold --expand --tabhead="MEDIUM SHADE" --tabspace="LIGHT SHADE"
=head2 B<--tabstyle>
Option B<--tabstyle> allow to set B<--tabhead> and B<--tabspace>
characters at once according to the given style name. Select from
C<dot>, C<symbol> or C<shade>. Styles are defined in
L<Text::ANSI::Fold> library.
$ ansifold --expand --tabstyle=shade
=head1 COLRM COMPATIBLE
=head2 B<--colrm> [ I<start> [ I<end> ] ... ]
Option B<--colrm> takes L<colrm(1)> command compatible arguments.
Since the output separator string is not set, use the B<-n> option to
get the same result as the L<colrm(1)> command; when invoked as
B<ansicolrm> command, the separator string is set to the empty by
default.
Next command behave exactly like C<colrm start end> and takes care of
ANSI terminal sequences.
$ ansifold -n --colrm start end
$ ansicolrm start end
Unlike standard L<colrm(1)>, I<start> and I<end> can be repeated as
many times as desired. Next command removes column 1-3 and 7-9, and
produces C<4560> as a result.
$ echo 1234567890 | ansifold -n --colrm 1 3 7 9
^^^ ^^^
=head1 CUT COMPATIBLE
=head2 B<--cut> list ...
=head2 B<-c> list ...
Option B<--cut> (or B<-c>) takes L<cut(1)> command compatible
arguments.
Since the output separator string is set, use the B<-n> option to get
the same result as the L<cut(1)> command; when invoked as B<ansicut>
command, the separator string is set to the empty by default.
Next command behave exactly like C<cut -c list> and takes care of ANSI
terminal sequences.
$ ansifold -n -c list ...
$ ansicut -c list ...
Next command retrieve column 4-6,9- and produces C<45690> as a result.
$ echo 1234567890 | ansifold -nc 4-6,9-
^^^ ^^
Unlike L<cut(1)>'s B<-c> option, parameter number is taken as screen
columns of the terminal, rather than number of logical characters.
=head1 BUGS
Option B<--refill> will join Hangul string without space. Probably
this is not a correct behavior.
=head1 FILES
=over 4
=item F<~/.ansifoldrc>
Start-up file.
See L<Getopt::EX::Module> for format.
=back
=head1 INSTALL
=head2 CPANMINUS
$ cpanm App::ansifold
=head1 SEE ALSO
L<ANSI Tool collection|https://github.com/tecolicom/ANSI-Tools>
L<ansifold|https://github.com/tecolicom/App-ansifold>
L<ansiexpand|https://github.com/tecolicom/App-ansiexpand>
L<ansicolumn|https://github.com/tecolicom/App-ansicolumn>
L<Text::ANSI::Fold|https://github.com/tecolicom/Text-ANSI-Fold>
L<Text::ANSI::Fold::Util|https://github.com/tecolicom/Text-ANSI-Fold-Util>
L<Getopt::EX::Numbers>
Requirements for Japanese Text Layout,
W3C Working Group Note 11 August 2020
=head1 AUTHOR
Kazumasa Utashiro
=head1 LICENSE
Copyright ©︎ 2018-2025 Kazumasa Utashiro
This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.
=cut
# LocalWords: unicode ansifold LANG colrm KINSOKU ansiexpand
# LocalWords: ansicolrm