Parse::...r::Tiny(3)
okay bestie, welcome to the Parse::ANSIColor::Tiny(3) page β yes, the manual name is doing a little truncated-header moment, but the real one is right here. π
π NAME π
Parse::ANSIColor::Tiny β Determine attributes of ANSI-Colored string. Thatβs the whole name section, bestie: it tells you exactly what this little icon does. π¦
β¨ VERSION β¨
version 0.700 β yes, thatβs the version, no cap. π
πΈ SYNOPSIS πΈ
Picture this: you got output from some command, and itβs all colorful. Hereβs the vibe in code:
# output from some command
my $output = "foo\e[31mbar\e[00m";
my $ansi = Parse::ANSIColor::Tiny->new();
my $marked = $ansi->parse($output);
is_deeply
$marked,
[
[ [], 'foo' ],
[ ['red'], 'bar' ],
],
'parse colored string';
# don't forget to html-encode the string!
my $html = join '',
'<div>',
(map { '<span class="' . join(' ', @{ $_->[0] }) . '">' . h($_->[1]) . '</span>' } @$marked),
'</div>';
is $html,
'<div><span class="">foo</span><span class="red">bar</span></div>',
'turned simple ansi into html';
So yeah: new() builds the parser, parse() hands you back the marked-up chunks, and the HTML bit is your reminder to h()-encode the string before stuffing it into markup. Donβt skip the html-encode step, bestie! π
π DESCRIPTION π
Okay so the whole point: this module takes a string colored with ANSI escape sequences and parses it into a structure thatβs suitable for reformatting β into HTML, for example. Terminal commands can spit out output marked up with colors and formatting, and in some instances youβd like to preserve that. Thatβs the moment this module serves. β¨
The tea: itβs essentially the inverse of Term::ANSIColor. The array refs that come back from parse can be passed right back in to Term::ANSIColor::colored. The strings may not match exactly, because there are different ways the attributes can be specified, but the end result should be colored the same. iykyk.
This is a ::Tiny module... it attempts to be correct for most cases with a small amount of code. It may not be 100% correct, especially in complex cases. It only handles the m escape sequence ("\033[0m") which produces colors and simple attributes (bold, underline) (like what can be produced with Term::ANSIColor). Other escape sequences are removed by default, but you can disable this by passing remove_escapes => 0 to the constructor. Important caveat, bestie!
If you do find bugs please submit tickets (with patches, if possible). π«Ά
π§ METHODS π§
new
Constructor. It takes a hash or hash ref of arguments: π
auto_reverseβ Automatically invert colors whenreverseis present; Disabled by default.backgroundβ Color to assume as background; Black by default. Currently used byprocess_reverse.foregroundβ Color to assume as foreground; White by default. Currently used byprocess_reverse.remove_escapesβ Remove other terminal escape sequences (not related to color). Passes strings throughremove_escape_sequencesbefore parsing.
colors
Returns a list of the base color names (in numeric escape sequence order). Thatβs it, thatβs the method. π
foreground_colors
Returns a list of the foreground colors (in numeric escape sequence order).
This includes the base colors, their bright_ variants, and the names from the 256 palette (prefixes of ansi, rgb, and grey). so many colors, so little time.
background_colors
Returns a list of the background colors (in numeric escape sequence order).
This includes the on_ and on_bright_ variants of the base colors and the on_ names for the 256 palette.
identify
my @names = $parser->identify('1;31');
# or $parser->identify('1', '31');
# returns ('bold', 'red')
Identifies attributes by their number; Returns a list of names.
This is similar to uncolor() in Term::ANSIColor.
Unknown codes will be ignored (remove from the output):
$parser->identify('33', '52');
# returns ('yellow') # drops the '52'
normalize
my @norm = $parser->normalize(@attributes);
Takes a list of named attributes (like those returned from identify) and reduces the list to only those that would have effect. Here are the rules:
- Duplicates will be removed
- a foreground color will overwrite any previous foreground color (and the previous ones will be removed)
- same for background colors
clearwill remove all previous attributes
my @norm = $parser->normalize(qw(red bold green));
# returns ('bold', 'green');
parse
my $marked = $parser->parse($output);
Parse the provided string and return an array ref of array refs describing the formatting:
# [
# [ [], 'plain words' ],
# [ ['red'], 'colored words' ],
# [
These array refs are consistent with the arguments to colored() in Term::ANSIColor:
Term::ANSIColor::colored( ['red'], 'colored words' );
process
Performs post-processing on the provided attributes.
This currently includes process_reverse if auto_reverse is enabled. π«
process_reverse
my @attr = $parser->process_reverse( $parser->normalize( '31;42;7' ) );
Translates a normalized set of attributes into something easier to process. This is called internally when auto_reverse is configured.
If reverse is included in the attributes it should invert the foreground and background colors.
This method makes the attributes more straight forward and likely easier for other things to process:
my @norm = $parser->normalize( '1;31;42;7' );
# returns qw( bold red on_green reverse );
my @attr = $parser->process_reverse( @norm );
# returns qw( bold on_red green );
This extra step is necessary to maintain state and properly handle reverse/reverse_off since two reverses do not cancel each other, but rather the second should be ignored. Important!
If no foreground or background color is currently active then the colors specified as foreground and background will be included (and reversed).
my @attr = $parser->process_reverse( qw( bold reverse ) );
# returns qw( bold on_white black );
my @attr = $parser->process_reverse( qw( bold reverse red ) );
# returns qw( bold on_red black );
This is consistent with the way it is drawn in the terminal. Explicitly specifying both colors should make it easy for anything downstream to process and display as intended.
remove_escape_sequences
my $clean = $parser->remove_escape_sequences( $string );
Strip other terminal escape sequences (those not relating to color) from the string to avoid unexpected characters in the output. This method is called from parse if remove_escapes is enabled.
π₯ FUNCTIONS π₯
identify_ansicolorβ Function wrapped aroundidentify.normalize_ansicolorβ Function wrapped aroundnormalize.parse_ansicolorβ Function wrapped aroundparse.
π EXPORTS π
Everything listed in FUNCTIONS is also available for export upon request. periodt.
π¦ SEE ALSO π¦
Term::ANSIColorβ For marking up text that will be printed to the terminalImage::TextMode(andImage::TextMode::Format::ANSI) β Successor toImage::ANSI; Specifically designed for parsing ANSI artTerm::VT102β Handles more than colors and is likely more robust but may be overkill in simple situations (and was difficult to install in the past).HTML::FromANSI::Tinyβ Uses this module to translate ANSI colored text to simple HTML
π SUPPORT π
Perldoc
You can find documentation for this module with the perldoc command.
perldoc Parse::ANSIColor::Tiny
Websites
The following websites have more information about this module, and may be of help to you. As always, in addition to those websites please use your favorite search engine to discover more resources.
- MetaCPAN β A modern, open-source CPAN search engine, useful to view POD in HTML format.
https://metacpan.org/release/Parse-ANSIColor-Tiny
Bugs / Feature Requests
Please report any bugs or feature requests by email to bug-parse-ansicolor-tiny at rt.cpan.org, or through the web interface at https://rt.cpan.org/Public/Bug/Report.html?Queue=Parse-ANSIColor-Tiny. You will be automatically notified of any progress on the request by the system. π«Ά
Source Code
https://github.com/rwstauner/Parse-ANSIColor-Tiny
git clone https://github.com/rwstauner/Parse-ANSIColor-Tiny.git
π AUTHOR π
Randy Stauner <rwstauner@cpan.org>
π CONTRIBUTORS π
Dmitry Fedin <dmitry.fedin@gmail.com>Randy Stauner <randy@r4s6.net>
π COPYRIGHT AND LICENSE π
This software is copyright (c) 2011 by Randy Stauner.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
okay bestie, thatβs the whole page π this footer was perl v5.42.0, dated 2022-10-23, from the Parse::...r::Tiny(3) footer β signing off with a little sparkle β¨π
π¦ where this page comes from
- opensuse-leap 16.0: perl-Parse-ANSIColor-Tiny 0.700-bp160.1.12 Β· Artistic-1.0 OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms