String...rminal(3pm)
π String::Tagged::Terminal π
okay bestie so this is String::Tagged::Terminal, a whole module from the User Contributed Perl Documentation corner of the perl universe, and it's giving β¨aesthetic terminal outputβ¨. fr fr.
πΈ NAME πΈ
String::Tagged::Terminal β format terminal output using String::Tagged. no cap, that's literally the whole thing.
π SYNOPSIS π
use String::Tagged::Terminal;
my $st = String::Tagged::Terminal->new
->append( "Hello my name is " )
->append_tagged( $name, bold => 1, fgindex => 4 );
$st->say_to_terminal;
look at her go π she's so pretty and she just prints.
π¦ DESCRIPTION π¦
so basically this is a subclass of String::Tagged (her iconic mother module π
) and she brings one special gift to the group chat: a method called build_terminal. what does it do? it takes all the formatting tags you've tucked inside the string and turns them into actual terminal escape sequences, so your output renders in the right style when it hits the screen. it's giving glow-up. it's giving main character energy.
π TAGS π
these are the tag names she recognises. memorize them, bestie, they're on the exam:
β¨ bold, under, italic, strike, blink, reverse β¨
these ones take a boolean value. if the value is true β the corresponding terminal rendering attribute gets switched on. that's it, that's the vibe.
β¨ altfont β¨
takes an integer value. if it's defined, she uses the "alternate font selection" sequence. fancy. π©
β¨ fgindex, bgindex β¨
these take an integer value in the range 0 to 255. they pick the foreground or background colour by using VGA, high-brightness extended 16 colour, or xterm 256 palette mode attributes β which one depends on the value you hand over.
the ECMA-48-corrected string encoding form of CSI 38:5:nnn m is what's used to set the 256 palette values.
values get rounded down to the nearest integer, by calling int(). and honestly? this convenience is a gift because it means you can do stuff like use the rand function to generate random colours:
$st->append_tagged( "text", fgindex => 1 + rand 6 );
random colour chaos, slay π
β¨ sizepos β¨
since version 0.06.
(experimental) β yes bestie, she said experimental, be careful.
this tag takes a value that indicates an adjustment to the vertical positioning, and possibly also the size, in order to create subscript or superscript effects.
the recognised values are sub for subscript and super for superscript. these get implemented using the mintty-style CSI 73/74/75 m codes. iykyk.
β¨ link β¨
since version 0.08.
(experimental) β again, experimental, don't come for me.
this tag takes a HASH reference, whose uri key gets emitted using the OSC 8 hyperlink sequence. clickable text in the terminal, we love to see it π₯
π§ CONSTRUCTORS π§
π new_from_formatting π
$st = String::Tagged::Terminal->new_from_formatting( $fmt )
returns a new instance by converting String::Tagged::Formatting standard tags. π«
foreground and background colours get converted to their nearest index in the xterm 256 colour palette. and the monospace Formatting attribute? she renders it by selecting the first alternate font using altfont. cute.
π parse_terminal π
$st = String::Tagged::Terminal->parse_terminal( $str );
since version 0.07.
returns a new instance by parsing a string that has SGR terminal escape sequences mixed in with plain string content.
now listen: the parser will only accept 7- or 8-bit encodings of the SGR escape sequence (\e[ ... m or \x9b ... m). if any other escape sequences show up? exception gets thrown. periodt. π
but conversely β and this is the chill part β unrecognised formatting codes inside SGR sequences are just simply ignored, no warning, no drama, nothing. π
π METHODS π
these are the methods she provides in addition to everything String::Tagged already gives you. so many gifts.
β build_terminal β
$str = $st->build_terminal( %opts );
returns a string containing terminal escape sequences mixed with string content, so the whole thing renders to a terminal. β¨
careful bestie β since this string contains literal terminal control escape sequences, you should be careful when passing it around, printing it for debugging purposes, or doing anything similar. she's powerful and she's loud.
takes the following additional named options:
no_colorβ if true, thefgindexandbgindexattributes get ignored. the result of this is that some formatting still happens using the other attributes, but colours are simply not set. monochrome moment π€
β as_formatting β
$fmt = $st->as_formatting;
returns a new String::Tagged instance tagged with String::Tagged::Formatting standard tags. she's bilingual, she translates. π«Ά
β print_to_terminal β
$str->print_to_terminal( $fh );
since version 0.03.
prints the string to the terminal by building a terminal escape string, then printing it to the given IO handle β or STDOUT if you didn't supply one. no handle? she'll figure it out. π
this method passes the value of the NO_COLOR environment variable down to the underlying build_terminal method call. meaning: if that variable has a true value, then colouring tags get ignored and you get a monochrome output. this follows the suggestion of <http://no-color.org/>. respect the environment variable, bestie π±
β say_to_terminal β
$str->say_to_terminal( $fh );
since version 0.03.
prints the string to the terminal exactly like print_to_terminal does, and then follows it up with a linefeed. she says it. she adds the lil newline. iconic. π€
π COMPATIBILITY NOTES π
on Windows, the following notes apply β read them, i'm not joking:
- On all versions of Windows, the attributes
bold,fgindexandbgindexare supported. theboldattribute is implemented by using high-intensity colours, so it will be indistinguishable from using high-intensity colour indexes without bold. also the full 256-color palette is not supported by Windows, so it gets down-converted to the 16 colours that are. tragic, honestly π - Starting with Windows 10,
underandreverseare also supported. progress! - The attributes
italic,strike,altfont,blinkare not supported on any Windows version. at all. zero. nada. π - On Windows, only a single output console is supported.
πΈ AUTHOR πΈ
Paul Evans <leonerd@leonerd.org.uk> β the legend, the icon, the moment π
okay that's the whole page bestie, you're officially certified π go format some terminal output and make it gorgeous β¨
this has been String::Tagged::Terminal from the User Contributed Perl Documentation, perl v5.38.2, dated 2024-08-03 β mwah, love u, bye π«Άππ¦
π¦ where this page comes from
- debian trixie: libstring-tagged-terminal-perl 0.08-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu resolute: libstring-tagged-terminal-perl 0.08-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu stonking: libstring-tagged-terminal-perl 0.08-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms