| 1 |
NAME |
| 2 |
Text::ANSI::Util - Routines for text containing ANSI color codes |
| 3 |
|
| 4 |
VERSION |
| 5 |
This document describes version 0.234 of Text::ANSI::Util (from Perl |
| 6 |
distribution Text-ANSI-Util), released on 2023-02-27. |
| 7 |
|
| 8 |
SYNOPSIS |
| 9 |
use Text::ANSI::Util qw( |
| 10 |
ta_add_color_resets |
| 11 |
ta_detect |
| 12 |
ta_extract_codes |
| 13 |
ta_highlight |
| 14 |
ta_highlight_all |
| 15 |
ta_length |
| 16 |
ta_length_height |
| 17 |
ta_pad |
| 18 |
ta_split_codes |
| 19 |
ta_split_codes_single |
| 20 |
ta_strip |
| 21 |
ta_substr |
| 22 |
ta_trunc |
| 23 |
ta_wrap |
| 24 |
); |
| 25 |
|
| 26 |
# detect whether text has ANSI color codes? |
| 27 |
say ta_detect("red"); # => false |
| 28 |
say ta_detect("\e[31mred"); # => true |
| 29 |
|
| 30 |
# calculate length of text (excluding the ANSI color codes) |
| 31 |
say ta_length("red"); # => 3 |
| 32 |
say ta_length("\e[31mred"); # => 3 |
| 33 |
|
| 34 |
# strip ANSI color codes |
| 35 |
say ta_strip("\e[31mred"); # => "red" |
| 36 |
|
| 37 |
# split codes (ANSI color codes are always on the even positions) |
| 38 |
my @parts = ta_split_codes("\e[31mred"); # => ("", "\e[31m", "red") |
| 39 |
|
| 40 |
# wrap text to a certain column width, handle ANSI color codes |
| 41 |
say ta_wrap("....", 40); |
| 42 |
|
| 43 |
# pad (left, right, center) text to a certain width |
| 44 |
say ta_pad("foo", 10); # => "foo " |
| 45 |
say ta_pad("foo", 10, "left"); # => " foo" |
| 46 |
say ta_pad("foo\nbarbaz\n", 10, "center", "."); # => "...foo....\n..barbaz..\n" |
| 47 |
|
| 48 |
# truncate text to a certain width while still passing ANSI color codes |
| 49 |
use Term::ANSIColor; |
| 50 |
my $text = color("red")."red text".color("reset"); # => "\e[31mred text\e[0m" |
| 51 |
say ta_trunc($text, 5); # => "\e[31mred t\e[0m" |
| 52 |
|
| 53 |
# highlight the first occurrence of some string within text |
| 54 |
say ta_highlight("some text", "ome", "\e[7m\e[31m"); |
| 55 |
|
| 56 |
# ditto, but highlight all occurrences |
| 57 |
say ta_highlight_all(...); |
| 58 |
|
| 59 |
# get substring |
| 60 |
my $substr = ta_substr("...", $pos, $len); |
| 61 |
|
| 62 |
# return text but with substring replaced with replacement |
| 63 |
say ta_substr("...", $pos, $len, $replacement); |
| 64 |
|
| 65 |
DESCRIPTION |
| 66 |
This module provides routines for dealing with text that contains ANSI |
| 67 |
color codes, e.g. for determining its length/width (excluding the color |
| 68 |
codes), stripping the color codes, extracting the color codes, and so |
| 69 |
on. |
| 70 |
|
| 71 |
For functions that support wide characters, see Text::ANSI::WideUtil. |
| 72 |
|
| 73 |
Current caveats: |
| 74 |
|
| 75 |
* Other ANSI codes (non-color codes) are ignored |
| 76 |
|
| 77 |
These are codes like for altering cursor positions, etc. |
| 78 |
|
| 79 |
* Single-character CSI (control sequence introducer) currently ignored |
| 80 |
|
| 81 |
Only "ESC+[" (two-character CSI) is currently parsed. |
| 82 |
|
| 83 |
BTW, in ASCII terminals, single-character CSI is 0x9b. In UTF-8 |
| 84 |
terminals, it is "0xc2, 0x9b" (2 bytes). |
| 85 |
|
| 86 |
* Private-mode- and trailing-intermediate character currently not |
| 87 |
parsed |
| 88 |
|
| 89 |
* Only color reset code \e[0m is recognized |
| 90 |
|
| 91 |
For simplicity, currently multiple SGR parameters inside a single |
| 92 |
ANSI color code is not parsed. This means that color reset code like |
| 93 |
"\e[1;0m" or "\e[31;47;0m" is not recognized, only "\e[0m" is. I |
| 94 |
believe this should not be a problem with most real-world text out |
| 95 |
there. |
| 96 |
|
| 97 |
FUNCTIONS |
| 98 |
ta_add_color_resets(@text) => LIST |
| 99 |
Make sure that a color reset command (add "\e[0m") to the end of each |
| 100 |
element and a replay of all the color codes from the previous element, |
| 101 |
from the last color reset) to the start of the next element, and so on. |
| 102 |
Return the new list. |
| 103 |
|
| 104 |
This makes each element safe to be combined with other array of text |
| 105 |
into a single line, e.g. in a multicolumn/tabular layout. An example: |
| 106 |
|
| 107 |
Without color resets: |
| 108 |
|
| 109 |
my @col1 = split /\n/, "\e[31mred\nmerah\e[0m"; |
| 110 |
my @col2 = split /\n/, "\e[32mgreen\e[1m\nhijau tebal\e[0m"; |
| 111 |
|
| 112 |
printf "%s | %s\n", $col1[0], $col2[0]; |
| 113 |
printf "%s | %s\n", $col1[1], $col2[1]; |
| 114 |
|
| 115 |
the printed output: |
| 116 |
|
| 117 |
\e[31mred | \e[32mgreen |
| 118 |
merah\e[0m | \e[1mhijau tebal\e[0m |
| 119 |
|
| 120 |
The "merah" text on the second line will become green because of the |
| 121 |
effect of the last color command printed ("\e[32m"). However, with |
| 122 |
ta_add_color_resets(): |
| 123 |
|
| 124 |
my @col1 = ta_add_color_resets(split /\n/, "\e[31mred\nmerah\e[0m"); |
| 125 |
my @col2 = ta_add_color_resets(split /\n/, "\e[32mgreen\e[1m\nhijau tebal\e[0m"); |
| 126 |
|
| 127 |
printf "%s | %s\n", $col1[0], $col2[0]; |
| 128 |
printf "%s | %s\n", $col1[1], $col2[1]; |
| 129 |
|
| 130 |
the printed output ("<...>") marks the code added by |
| 131 |
ta_add_color_resets(): |
| 132 |
|
| 133 |
\e[31mred<\e[0m> | \e[32mgreen\e[1m<\e[0m> |
| 134 |
<\e[31m>merah\e[0m | <\e[32m\e[1m>hijau tebal\e[0m |
| 135 |
|
| 136 |
All the cells are printed with the intended colors. |
| 137 |
|
| 138 |
ta_detect($text) => BOOL |
| 139 |
Return true if $text contains ANSI color codes, false otherwise. |
| 140 |
|
| 141 |
ta_extract_codes($text) => STR |
| 142 |
This is the opposite of "ta_strip()", return only the ANSI codes in |
| 143 |
$text. |
| 144 |
|
| 145 |
ta_highlight($text, $needle, $color) => STR |
| 146 |
Highlight the first occurrence of $needle in $text with <$color>, taking |
| 147 |
care not to mess up existing colors. |
| 148 |
|
| 149 |
$needle can be a string or a Regexp object. |
| 150 |
|
| 151 |
Implementation note: to not mess up colors, we save up all color codes |
| 152 |
from the last reset ("\e[0m") before inserting the highlight color + |
| 153 |
highlight text. Then we issue "\e[0m" and the saved up color code to |
| 154 |
return back to the color state before the highlight is inserted. This is |
| 155 |
the same technique as described in "ta_add_color_resets()". |
| 156 |
|
| 157 |
ta_highlight_all($text, $needle, $color) => STR |
| 158 |
Like "ta_highlight()", but highlight all occurrences instead of only the |
| 159 |
first. |
| 160 |
|
| 161 |
ta_length($text) => INT |
| 162 |
Count the number of characters in $text, while ignoring ANSI color |
| 163 |
codes. Equivalent to "length(ta_strip($text))". See also: |
| 164 |
"ta_mbswidth()" in Text::ANSI::WideUtil. |
| 165 |
|
| 166 |
ta_length_height($text) => [INT, INT] |
| 167 |
Like "ta_length()", but also gives height (number of lines). For |
| 168 |
example, "ta_length_height("foobar\nb\n")" gives "[6, 3]". |
| 169 |
|
| 170 |
See also: "ta_mbswidth_height()" in Text::ANSI::WideUtil. |
| 171 |
|
| 172 |
ta_pad($text, $width[, $which[, $padchar[, $truncate]]]) => STR |
| 173 |
Return $text padded with $padchar to $width columns. $which is either |
| 174 |
"r" or "right" for padding on the right (the default if not specified), |
| 175 |
"l" or "left" for padding on the right, or "c" or "center" or "centre" |
| 176 |
for left+right padding to center the text. |
| 177 |
|
| 178 |
$padchar is whitespace if not specified. It should be string having the |
| 179 |
width of 1 column. |
| 180 |
|
| 181 |
Does *not* handle multiline text; you can split text by "/\r?\n/" |
| 182 |
yourself. |
| 183 |
|
| 184 |
See also: "ta_mbpad()" in Text::ANSI::WideUtil. |
| 185 |
|
| 186 |
ta_split_codes($text) => LIST |
| 187 |
Split $text to a list containing alternating ANSI color codes and text. |
| 188 |
ANSI color codes are always on the second element, fourth, and so on. |
| 189 |
Example: |
| 190 |
|
| 191 |
ta_split_codes(""); # => () |
| 192 |
ta_split_codes("a"); # => ("a") |
| 193 |
ta_split_codes("a\e[31m"); # => ("a", "\e[31m") |
| 194 |
ta_split_codes("\e[31ma"); # => ("", "\e[31m", "a") |
| 195 |
ta_split_codes("\e[31ma\e[0m"); # => ("", "\e[31m", "a", "\e[0m") |
| 196 |
ta_split_codes("\e[31ma\e[0mb"); # => ("", "\e[31m", "a", "\e[0m", "b") |
| 197 |
ta_split_codes("\e[31m\e[0mb"); # => ("", "\e[31m\e[0m", "b") |
| 198 |
|
| 199 |
so you can do something like: |
| 200 |
|
| 201 |
my @parts = ta_split_codes($text); |
| 202 |
while (my ($text, $ansicode) = splice(@parts, 0, 2)) { |
| 203 |
... |
| 204 |
} |
| 205 |
|
| 206 |
ta_split_codes_single($text) => LIST |
| 207 |
Like "ta_split_codes()" but each ANSI color code is split separately, |
| 208 |
instead of grouped together. This routine is currently used internally |
| 209 |
e.g. for "ta_wrap()" and "ta_highlight()" to trace color reset/replay |
| 210 |
codes. |
| 211 |
|
| 212 |
ta_strip($text) => STR |
| 213 |
Strip ANSI color codes from $text, returning the stripped text. |
| 214 |
|
| 215 |
ta_substr($text, $pos, $len[ , $replacement ]) => STR |
| 216 |
A bit like Perl's "substr()". If $replacement is not specified, will |
| 217 |
return the substring. If $replacement is specified, will return $text |
| 218 |
with the substring replaced by $replacement. |
| 219 |
|
| 220 |
See also: "ta_mbsubstr()" in Text::ANSI::WideUtil. |
| 221 |
|
| 222 |
ta_trunc($text, $width) => STR |
| 223 |
Truncate $text to $width columns while still including all the ANSI |
| 224 |
color codes. This ensures that truncated text still reset colors, etc. |
| 225 |
|
| 226 |
Does *not* handle multiline text; you can split text by "/\r?\n/" |
| 227 |
yourself. |
| 228 |
|
| 229 |
See also: "ta_mbtrunc()" in Text::ANSI::WideUtil. |
| 230 |
|
| 231 |
ta_wrap($text, $width, \%opts) => STR |
| 232 |
Like Text::WideChar::Util's "wrap()" except handles ANSI color codes. |
| 233 |
Perform color reset at the end of each line and a color replay at the |
| 234 |
start of subsequent line so the text is safe for combining in a |
| 235 |
multicolumn/tabular layout. |
| 236 |
|
| 237 |
Options: |
| 238 |
|
| 239 |
* flindent => STR |
| 240 |
|
| 241 |
First line indent. Currently must not contain ANSI color codes or |
| 242 |
wide characters. |
| 243 |
|
| 244 |
* slindent => STR |
| 245 |
|
| 246 |
Subsequent line indent. Currently must not contain ANSI color codes |
| 247 |
or wide characters. |
| 248 |
|
| 249 |
* tab_width => INT (default: 8) |
| 250 |
|
| 251 |
* pad => BOOL (default: 0) |
| 252 |
|
| 253 |
If set to true, will pad each line to $width. This is convenient if |
| 254 |
you need the lines padded, saves calls to ta_pad(). |
| 255 |
|
| 256 |
* return_stats => BOOL (default: 0) |
| 257 |
|
| 258 |
If set to true, then instead of returning the wrapped string, |
| 259 |
function will return "[$wrapped, $stats]" where $stats is a hash |
| 260 |
containing some information like "max_word_width", "min_word_width". |
| 261 |
|
| 262 |
* keep_trailing_space => BOOL (default: 0) |
| 263 |
|
| 264 |
If set to true, then trailing space that separates words will be |
| 265 |
kept at the end of wrapped lines. This option is useful if you want |
| 266 |
to rejoin the lines later. Without this option set to true, wrapping |
| 267 |
this line at width=4 (quotes shown): |
| 268 |
|
| 269 |
"some long line" |
| 270 |
|
| 271 |
will result in: |
| 272 |
|
| 273 |
"some" |
| 274 |
"long" |
| 275 |
"line" |
| 276 |
|
| 277 |
While if this option is set to true, the result will be: |
| 278 |
|
| 279 |
"some " |
| 280 |
"long " |
| 281 |
"line" |
| 282 |
|
| 283 |
Performance: ~500/s on my Core i5 1.7GHz laptop for a ~1KB of text (with |
| 284 |
zero to moderate amount of color codes). |
| 285 |
|
| 286 |
See also: "ta_mbwrap()" in Text::ANSI::WideUtil. |
| 287 |
|
| 288 |
FAQ |
| 289 |
Why split functionalities of wide character and color support into multiple modules/distributions? |
| 290 |
Performance (see numbers in the function description), dependency |
| 291 |
(Unicode::GCString is used for wide character support), and overhead |
| 292 |
(loading Unicode::GCString). |
| 293 |
|
| 294 |
How do I highlight a string case-insensitively? |
| 295 |
You can currently use a regex for the $needle and use the "i" modifier. |
| 296 |
Example: |
| 297 |
|
| 298 |
use Term::ANSIColor; |
| 299 |
ta_highlight($text, qr/\b(foo)\b/i, color("bold red")); |
| 300 |
|
| 301 |
HOMEPAGE |
| 302 |
Please visit the project's homepage at |
| 303 |
<https://metacpan.org/release/Text-ANSI-Util>. |
| 304 |
|
| 305 |
SOURCE |
| 306 |
Source repository is at |
| 307 |
<https://github.com/perlancar/perl-Text-ANSI-Util>. |
| 308 |
|
| 309 |
SEE ALSO |
| 310 |
String::Pad provides padding function for strings that do not contain |
| 311 |
ASCII escape codes nor wide characters. |
| 312 |
|
| 313 |
Text::NonWideChar::Util provides some other functions for strings that |
| 314 |
do not contain ASCII escape codes nor wide characters. |
| 315 |
|
| 316 |
Text::WideChar::Util provides utilities for strings that do not contain |
| 317 |
ANSI escape codes *but* contain wide characters. |
| 318 |
|
| 319 |
Text::ANSI::WideUtil provides utilities for strings that contain ANSI |
| 320 |
escape codes *and* wide characters. |
| 321 |
|
| 322 |
AUTHOR |
| 323 |
perlancar <[email protected]> |
| 324 |
|
| 325 |
CONTRIBUTOR |
| 326 |
Steven Haryanto <[email protected]> |
| 327 |
|
| 328 |
CONTRIBUTING |
| 329 |
To contribute, you can send patches by email/via RT, or send pull |
| 330 |
requests on GitHub. |
| 331 |
|
| 332 |
Most of the time, you don't need to build the distribution yourself. You |
| 333 |
can simply modify the code, then test via: |
| 334 |
|
| 335 |
% prove -l |
| 336 |
|
| 337 |
If you want to build the distribution (e.g. to try to install it locally |
| 338 |
on your system), you can install Dist::Zilla, |
| 339 |
Dist::Zilla::PluginBundle::Author::PERLANCAR, |
| 340 |
Pod::Weaver::PluginBundle::Author::PERLANCAR, and sometimes one or two |
| 341 |
other Dist::Zilla- and/or Pod::Weaver plugins. Any additional steps |
| 342 |
required beyond that are considered a bug and can be reported to me. |
| 343 |
|
| 344 |
COPYRIGHT AND LICENSE |
| 345 |
This software is copyright (c) 2023, 2021, 2016, 2015, 2014, 2013 by |
| 346 |
perlancar <[email protected]>. |
| 347 |
|
| 348 |
This is free software; you can redistribute it and/or modify it under |
| 349 |
the same terms as the Perl 5 programming language system itself. |
| 350 |
|
| 351 |
BUGS |
| 352 |
Please report any bugs or feature requests on the bugtracker website |
| 353 |
<https://rt.cpan.org/Public/Dist/Display.html?Name=Text-ANSI-Util> |
| 354 |
|
| 355 |
When submitting a bug or request, please include a test-file or a patch |
| 356 |
to an existing test-file that illustrates the bug or desired feature. |
| 357 |
|