String-Tagged-Terminal-0.07000755001750001750 014527770376 14413 5ustar00leoleo000000000000String-Tagged-Terminal-0.07/.editorconfig000444001750001750 5314527770376 17163 0ustar00leoleo000000000000root = true [*.{pm,pl,t}] indent_size = 3 String-Tagged-Terminal-0.07/Build.PL000444001750001750 115014527770376 16041 0ustar00leoleo000000000000use v5; use strict; use warnings; use Module::Build; my $build = Module::Build->new( module_name => 'String::Tagged::Terminal', requires => { 'Convert::Color' => 0, 'Convert::Color::XTerm' => 0, 'String::Tagged' => 0, 'constant' => 0, ( $^O eq "MSWin32" ? ( "Win32::Console" => 0 ) : () ), 'perl' => '5.014', }, test_requires => { 'Test2::V0' => 0, }, configure_requires => { 'Module::Build' => '0.4004', # test_requires }, license => 'perl', create_license => 1, create_readme => 1, ); $build->create_build_script; String-Tagged-Terminal-0.07/Changes000444001750001750 222514527770376 16044 0ustar00leoleo000000000000Revision history for String-Tagged-Terminal 0.07 2023-11-24 [CHANGES] * Added `->parse_terminal` constructor 0.06 2023-02-06 [CHANGES] * Support the new `sizepos` Formatting tag, implemented using mintty-style SGR 73/74/75 * Swap all unit tests from `Test::More` to `Test2::V0` 0.05 2021-06-18 [CHANGES] * Style updates: use v5.14, package NAME VERSION; [BUGFIXES] * Round fgindex/bgindex values to nearest integer (RT136840) 0.04 2018-11-29 14:17:27 [CHANGES] * Experimental MSWin32 support, by Max Maischein [BUGFIXES] * Bugfix to bold attribute reset on escape-driven terminals when other rendering attributes are still present 0.03 2018-08-17 12:22:57 [CHANGES] * Added ->print_to_terminal and ->say_to_terminal methods * Support the NO_COLOR environment variable (http://no-color.org) 0.02 2017-10-03 15:53:04 [BUGFIXES] * Declare dependency on Convert::Color::XTerm (RT123182) 0.01 2017-10-02 19:27:18 First version, released on an unsuspecting world. String-Tagged-Terminal-0.07/LICENSE000444001750001750 4653414527770376 15611 0ustar00leoleo000000000000This software is copyright (c) 2023 by Paul Evans . This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself. Terms of the Perl programming language system itself a) the GNU General Public License as published by the Free Software Foundation; either version 1, or (at your option) any later version, or b) the "Artistic License" --- The GNU General Public License, Version 1, February 1989 --- This software is Copyright (c) 2023 by Paul Evans . This is free software, licensed under: The GNU General Public License, Version 1, February 1989 GNU GENERAL PUBLIC LICENSE Version 1, February 1989 Copyright (C) 1989 Free Software Foundation, Inc. 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed. Preamble The license agreements of most software companies try to keep users at the mercy of those companies. By contrast, our General Public License is intended to guarantee your freedom to share and change free software--to make sure the software is free for all its users. The General Public License applies to the Free Software Foundation's software and to any other program whose authors commit to using it. You can use it for your programs, too. When we speak of free software, we are referring to freedom, not price. Specifically, the General Public License is designed to make sure that you have the freedom to give away or sell copies of free software, that you receive source code or can get it if you want it, that you can change the software or use pieces of it in new free programs; and that you know you can do these things. To protect your rights, we need to make restrictions that forbid anyone to deny you these rights or to ask you to surrender the rights. These restrictions translate to certain responsibilities for you if you distribute copies of the software, or if you modify it. For example, if you distribute copies of a such a program, whether gratis or for a fee, you must give the recipients all the rights that you have. You must make sure that they, too, receive or can get the source code. And you must tell them their rights. We protect your rights with two steps: (1) copyright the software, and (2) offer you this license which gives you legal permission to copy, distribute and/or modify the software. Also, for each author's protection and ours, we want to make certain that everyone understands that there is no warranty for this free software. If the software is modified by someone else and passed on, we want its recipients to know that what they have is not the original, so that any problems introduced by others will not reflect on the original authors' reputations. The precise terms and conditions for copying, distribution and modification follow. GNU GENERAL PUBLIC LICENSE TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 0. This License Agreement applies to any program or other work which contains a notice placed by the copyright holder saying it may be distributed under the terms of this General Public License. The "Program", below, refers to any such program or work, and a "work based on the Program" means either the Program or any work containing the Program or a portion of it, either verbatim or with modifications. Each licensee is addressed as "you". 1. You may copy and distribute verbatim copies of the Program's source code as you receive it, in any medium, provided that you conspicuously and appropriately publish on each copy an appropriate copyright notice and disclaimer of warranty; keep intact all the notices that refer to this General Public License and to the absence of any warranty; and give any other recipients of the Program a copy of this General Public License along with the Program. You may charge a fee for the physical act of transferring a copy. 2. You may modify your copy or copies of the Program or any portion of it, and copy and distribute such modifications under the terms of Paragraph 1 above, provided that you also do the following: a) cause the modified files to carry prominent notices stating that you changed the files and the date of any change; and b) cause the whole of any work that you distribute or publish, that in whole or in part contains the Program or any part thereof, either with or without modifications, to be licensed at no charge to all third parties under the terms of this General Public License (except that you may choose to grant warranty protection to some or all third parties, at your option). c) If the modified program normally reads commands interactively when run, you must cause it, when started running for such interactive use in the simplest and most usual way, to print or display an announcement including an appropriate copyright notice and a notice that there is no warranty (or else, saying that you provide a warranty) and that users may redistribute the program under these conditions, and telling the user how to view a copy of this General Public License. d) You may charge a fee for the physical act of transferring a copy, and you may at your option offer warranty protection in exchange for a fee. Mere aggregation of another independent work with the Program (or its derivative) on a volume of a storage or distribution medium does not bring the other work under the scope of these terms. 3. You may copy and distribute the Program (or a portion or derivative of it, under Paragraph 2) in object code or executable form under the terms of Paragraphs 1 and 2 above provided that you also do one of the following: a) accompany it with the complete corresponding machine-readable source code, which must be distributed under the terms of Paragraphs 1 and 2 above; or, b) accompany it with a written offer, valid for at least three years, to give any third party free (except for a nominal charge for the cost of distribution) a complete machine-readable copy of the corresponding source code, to be distributed under the terms of Paragraphs 1 and 2 above; or, c) accompany it with the information you received as to where the corresponding source code may be obtained. (This alternative is allowed only for noncommercial distribution and only if you received the program in object code or executable form alone.) Source code for a work means the preferred form of the work for making modifications to it. For an executable file, complete source code means all the source code for all modules it contains; but, as a special exception, it need not include source code for modules which are standard libraries that accompany the operating system on which the executable file runs, or for standard header files or definitions files that accompany that operating system. 4. You may not copy, modify, sublicense, distribute or transfer the Program except as expressly provided under this General Public License. Any attempt otherwise to copy, modify, sublicense, distribute or transfer the Program is void, and will automatically terminate your rights to use the Program under this License. However, parties who have received copies, or rights to use copies, from you under this General Public License will not have their licenses terminated so long as such parties remain in full compliance. 5. By copying, distributing or modifying the Program (or any work based on the Program) you indicate your acceptance of this license to do so, and all its terms and conditions. 6. Each time you redistribute the Program (or any work based on the Program), the recipient automatically receives a license from the original licensor to copy, distribute or modify the Program subject to these terms and conditions. You may not impose any further restrictions on the recipients' exercise of the rights granted herein. 7. The Free Software Foundation may publish revised and/or new versions of the General Public License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns. Each version is given a distinguishing version number. If the Program specifies a version number of the license which applies to it and "any later version", you have the option of following the terms and conditions either of that version or of any later version published by the Free Software Foundation. If the Program does not specify a version number of the license, you may choose any version ever published by the Free Software Foundation. 8. If you wish to incorporate parts of the Program into other free programs whose distribution conditions are different, write to the author to ask for permission. For software which is copyrighted by the Free Software Foundation, write to the Free Software Foundation; we sometimes make exceptions for this. Our decision will be guided by the two goals of preserving the free status of all derivatives of our free software and of promoting the sharing and reuse of software generally. NO WARRANTY 9. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 10. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. END OF TERMS AND CONDITIONS Appendix: How to Apply These Terms to Your New Programs If you develop a new program, and you want it to be of the greatest possible use to humanity, the best way to achieve this is to make it free software which everyone can redistribute and change under these terms. To do so, attach the following notices to the program. It is safest to attach them to the start of each source file to most effectively convey the exclusion of warranty; and each file should have at least the "copyright" line and a pointer to where the full notice is found. Copyright (C) 19yy This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 1, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA Also add information on how to contact you by electronic and paper mail. If the program is interactive, make it output a short notice like this when it starts in an interactive mode: Gnomovision version 69, Copyright (C) 19xx name of author Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. This is free software, and you are welcome to redistribute it under certain conditions; type `show c' for details. The hypothetical commands `show w' and `show c' should show the appropriate parts of the General Public License. Of course, the commands you use may be called something other than `show w' and `show c'; they could even be mouse-clicks or menu items--whatever suits your program. You should also get your employer (if you work as a programmer) or your school, if any, to sign a "copyright disclaimer" for the program, if necessary. Here a sample; alter the names: Yoyodyne, Inc., hereby disclaims all copyright interest in the program `Gnomovision' (a program to direct compilers to make passes at assemblers) written by James Hacker. , 1 April 1989 Ty Coon, President of Vice That's all there is to it! --- The Perl Artistic License 1.0 --- This software is Copyright (c) 2023 by Paul Evans . This is free software, licensed under: The Perl Artistic License 1.0 The "Artistic License" Preamble The intent of this document is to state the conditions under which a Package may be copied, such that the Copyright Holder maintains some semblance of artistic control over the development of the package, while giving the users of the package the right to use and distribute the Package in a more-or-less customary fashion, plus the right to make reasonable modifications. Definitions: "Package" refers to the collection of files distributed by the Copyright Holder, and derivatives of that collection of files created through textual modification. "Standard Version" refers to such a Package if it has not been modified, or has been modified in accordance with the wishes of the Copyright Holder as specified below. "Copyright Holder" is whoever is named in the copyright or copyrights for the package. "You" is you, if you're thinking about copying or distributing this Package. "Reasonable copying fee" is whatever you can justify on the basis of media cost, duplication charges, time of people involved, and so on. (You will not be required to justify it to the Copyright Holder, but only to the computing community at large as a market that must bear the fee.) "Freely Available" means that no fee is charged for the item itself, though there may be fees involved in handling the item. It also means that recipients of the item may redistribute it under the same conditions they received it. 1. You may make and give away verbatim copies of the source form of the Standard Version of this Package without restriction, provided that you duplicate all of the original copyright notices and associated disclaimers. 2. You may apply bug fixes, portability fixes and other modifications derived from the Public Domain or from the Copyright Holder. A Package modified in such a way shall still be considered the Standard Version. 3. You may otherwise modify your copy of this Package in any way, provided that you insert a prominent notice in each changed file stating how and when you changed that file, and provided that you do at least ONE of the following: a) place your modifications in the Public Domain or otherwise make them Freely Available, such as by posting said modifications to Usenet or an equivalent medium, or placing the modifications on a major archive site such as uunet.uu.net, or by allowing the Copyright Holder to include your modifications in the Standard Version of the Package. b) use the modified Package only within your corporation or organization. c) rename any non-standard executables so the names do not conflict with standard executables, which must also be provided, and provide a separate manual page for each non-standard executable that clearly documents how it differs from the Standard Version. d) make other distribution arrangements with the Copyright Holder. 4. You may distribute the programs of this Package in object code or executable form, provided that you do at least ONE of the following: a) distribute a Standard Version of the executables and library files, together with instructions (in the manual page or equivalent) on where to get the Standard Version. b) accompany the distribution with the machine-readable source of the Package with your modifications. c) give non-standard executables non-standard names, and clearly document the differences in manual pages (or equivalent), together with instructions on where to get the Standard Version. d) make other distribution arrangements with the Copyright Holder. 5. You may charge a reasonable copying fee for any distribution of this Package. You may charge any fee you choose for support of this Package. You may not charge a fee for this Package itself. However, you may distribute this Package in aggregate with other (possibly commercial) programs as part of a larger (possibly commercial) software distribution provided that you do not advertise this Package as a product of your own. You may embed this Package's interpreter within an executable of yours (by linking); this shall be construed as a mere form of aggregation, provided that the complete Standard Version of the interpreter is so embedded. 6. The scripts and library files supplied as input to or produced as output from the programs of this Package do not automatically fall under the copyright of this Package, but belong to whoever generated them, and may be sold commercially, and may be aggregated with this Package. If such scripts or library files are aggregated with this Package via the so-called "undump" or "unexec" methods of producing a binary executable image, then distribution of such an image shall neither be construed as a distribution of this Package nor shall it fall under the restrictions of Paragraphs 3 and 4, provided that you do not represent such an executable image as a Standard Version of this Package. 7. C subroutines (or comparably compiled subroutines in other languages) supplied by you and linked into this Package in order to emulate subroutines and variables of the language defined by this Package shall not be considered part of this Package, but are the equivalent of input as in Paragraph 6, provided these subroutines do not change the language in any way that would cause it to fail the regression tests for the language. 8. Aggregation of this Package with a commercial distribution is always permitted provided that the use of this Package is embedded; that is, when no overt attempt is made to make this Package's interfaces visible to the end user of the commercial distribution. Such use shall not be construed as a distribution of this Package. 9. The name of the Copyright Holder may not be used to endorse or promote products derived from this software without specific prior written permission. 10. THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. The End String-Tagged-Terminal-0.07/MANIFEST000444001750001750 44514527770376 15664 0ustar00leoleo000000000000.editorconfig Build.PL Changes examples/ex.pl examples/random.pl lib/String/Tagged/Terminal.pm lib/String/Tagged/Terminal/Win32Console.pm LICENSE MANIFEST This list of files META.json META.yml README t/00use.t t/01build.t t/02parse.t t/10formatting.t t/20print.t t/30print-win32.t t/99pod.t String-Tagged-Terminal-0.07/META.json000444001750001750 251714527770376 16176 0ustar00leoleo000000000000{ "abstract" : "format terminal output using C", "author" : [ "Paul Evans " ], "dynamic_config" : 1, "generated_by" : "Module::Build version 0.4234", "license" : [ "perl_5" ], "meta-spec" : { "url" : "http://search.cpan.org/perldoc?CPAN::Meta::Spec", "version" : 2 }, "name" : "String-Tagged-Terminal", "prereqs" : { "configure" : { "requires" : { "Module::Build" : "0.4004" } }, "runtime" : { "requires" : { "Convert::Color" : "0", "Convert::Color::XTerm" : "0", "String::Tagged" : "0", "constant" : "0", "perl" : "5.014" } }, "test" : { "requires" : { "Test2::V0" : "0" } } }, "provides" : { "String::Tagged::Terminal" : { "file" : "lib/String/Tagged/Terminal.pm", "version" : "0.07" }, "String::Tagged::Terminal::Win32Console" : { "file" : "lib/String/Tagged/Terminal/Win32Console.pm", "version" : "0.07" } }, "release_status" : "stable", "resources" : { "license" : [ "http://dev.perl.org/licenses/" ] }, "version" : "0.07", "x_serialization_backend" : "JSON::PP version 4.07" } String-Tagged-Terminal-0.07/META.yml000444001750001750 156314527770376 16026 0ustar00leoleo000000000000--- abstract: 'format terminal output using C' author: - 'Paul Evans ' build_requires: Test2::V0: '0' configure_requires: Module::Build: '0.4004' dynamic_config: 1 generated_by: 'Module::Build version 0.4234, CPAN::Meta::Converter version 2.150010' license: perl meta-spec: url: http://module-build.sourceforge.net/META-spec-v1.4.html version: '1.4' name: String-Tagged-Terminal provides: String::Tagged::Terminal: file: lib/String/Tagged/Terminal.pm version: '0.07' String::Tagged::Terminal::Win32Console: file: lib/String/Tagged/Terminal/Win32Console.pm version: '0.07' requires: Convert::Color: '0' Convert::Color::XTerm: '0' String::Tagged: '0' constant: '0' perl: '5.014' resources: license: http://dev.perl.org/licenses/ version: '0.07' x_serialization_backend: 'CPAN::Meta::YAML version 0.018' String-Tagged-Terminal-0.07/README000444001750001750 1166614527770376 15462 0ustar00leoleo000000000000NAME String::Tagged::Terminal - format terminal output using String::Tagged 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; DESCRIPTION This subclass of String::Tagged provides a method, build_terminal, for outputting the formatting tags embedded in the string as terminal escape sequences, to render the the output in the appropriate style. TAGS The following tag names are recognised: bold, under, italic, strike, blink, reverse These tags take a boolean value. If the value is true then the corresponding terminal rendering attribute is enabled. altfont This tag takes an integer value. If defined it uses the "alternate font selection" sequence. fgindex, bgindex These tags take an integer value in the range 0 to 255. These select the foreground or background colour by using VGA, high-brightness extended 16 colour, or xterm 256 palette mode attributes, depending on the value. The ECMA-48-corrected string encoding form of CSI 38:5:nnn m is used to set the 256 palette values. Values will be rounded down to the nearest integer by calling int(). This convenience allows things like the rand function for generating random colours: $st->append_tagged( "text", fgindex => 1 + rand 6 ); sizepos (experimental) This tag takes a value indicating an adjustment to the vertical positioning, and possibly also size, in order to create subscript or superscript effects. Recognised values are sub for subscript, and super for superscript. These are implemented using the mintty-style CSI 73/74/75 m codes. 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 are converted to their nearest index in the xterm 256 colour palette. The monospace Formatting attribute is rendered by selecting the first alternate font using altfont. parse_terminal $st = String::Tagged::Terminal->parse_terminal( $str ); Since version 0.07. Returns a new instance by parsing a string containing SGR terminal escape sequences mixed with plain string content. 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 are present, an exception is thrown. Conversely, unrecognised formatting codes in SGR sequences are simply ignored without warning. METHODS The following methods are provided in addition to those provided by String::Tagged. build_terminal $str = $st->build_terminal( %opts ); Returns a string containing terminal escape sequences mixed with string content to render the string to a terminal. As this string will contain literal terminal control escape sequences, care should be taken when passing it around, printing it for debugging purposes, or similar. Takes the following additional named options: no_color If true, the fgindex and bgindex attributes will be ignored. This has the result of performing some formatting using the other attributes, but not setting colours. as_formatting $fmt = $st->as_formatting; Returns a new String::Tagged instance tagged with String::Tagged::Formatting standard tags. 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 not supplied). This method will pass the value of the NO_COLOR environment variable to the underlying "build_terminal" method call, meaning if that has a true value then colouring tags will be ignored, yielding a monochrome output. This follows the suggestion of http://no-color.org/. say_to_terminal $str->say_to_terminal( $fh ); Since version 0.03. Prints the string to the terminal as per "print_to_terminal", followed by a linefeed. COMPATIBILITY NOTES On Windows, the following notes apply: * On all versions of Windows, the attributes bold, fgindex and bgindex are supported. The bold attribute is implemented by using high-intensity colours, so will be indistinguishable from using high-intensity colour indexes without bold. The full 256-color palette is not supported by Windows, so it is down-converted to the 16 colours that are. * Starting with Windows 10, also under and reverse are supported. * The attributes italic, strike, altfont, blink are not supported on any Windows version. * On Windows, only a single output console is supported. AUTHOR Paul Evans String-Tagged-Terminal-0.07/examples000755001750001750 014527770376 16231 5ustar00leoleo000000000000String-Tagged-Terminal-0.07/examples/ex.pl000444001750001750 351214527770376 17340 0ustar00leoleo000000000000#!/usr/bin/perl use v5.14; use warnings; use String::Tagged::Terminal; my $st; $st = String::Tagged::Terminal->new( "Basic colours: " ); $st->append_tagged( sprintf( " [%d] ", $_ ), fgindex => $_, bgindex => $_==0 ? 7 : undef ) for 0 .. 7; $st->say_to_terminal; $st = String::Tagged::Terminal->new( " " ); $st->append_tagged( sprintf( " [%d] ", $_ ), bgindex => $_, fgindex => $_==7 ? 0 : undef ) for 0 .. 7; $st->say_to_terminal; print "\n"; $st = String::Tagged::Terminal->new( "Bold colours: " ); $st->append_tagged( sprintf( " [%d] ", $_ ), bold => 1, fgindex => $_, bgindex => $_==0 ? 7 : undef ) for 0 .. 7; $st->say_to_terminal; $st = String::Tagged::Terminal->new( " " ); $st->append_tagged( sprintf( " [%d] ", $_ ), bold => 1, bgindex => $_, fgindex => $_==7 ? 0 : undef ) for 0 .. 7; $st->say_to_terminal; print "\n"; $st = String::Tagged::Terminal->new( "HI colours: " ); $st->append_tagged( sprintf( " [%d] ", $_ ), fgindex => $_ ) for 8 .. 15; $st->say_to_terminal; $st = String::Tagged::Terminal->new( " " ); $st->append_tagged( sprintf( " [%d] ", $_ ), bgindex => $_ ) for 8 .. 15; $st->say_to_terminal; print "\n"; $st = String::Tagged::Terminal->new( "Attrs: " ); $st->append_tagged( $_, $_ => 1 ) ->append( " " ) for qw( bold italic under strike blink reverse altfont ); $st->append_tagged( "super", baseline => "super" ) ->append( "script "); $st->append_tagged( "sub", baseline => "sub" ) ->append( "script "); $st->say_to_terminal; print "\n"; print "256 colours:\n"; $st = String::Tagged::Terminal->new; $st->append_tagged( " ", bgindex => $_ ) for 0 .. 15; $st .= "\n\n"; foreach my $b ( 0 .. 5 ) { $st->append_tagged( " ", bgindex => 16 + $b*36 + $_ ) for 0 .. 35; $st .= "\n"; } $st .= "\n"; $st->append_tagged( " ", bgindex => 232 + $_ ) for 0 .. 23; $st->say_to_terminal; String-Tagged-Terminal-0.07/examples/random.pl000444001750001750 46614527770376 20171 0ustar00leoleo000000000000#!/usr/bin/perl use v5.14; use warnings; use String::Tagged::Terminal; my $st = String::Tagged::Terminal->new( "Random stuff: " ); foreach ( 1 .. 100 ) { my $str = chr( 0x41 + rand 26 ) x ( 5 + rand 5 ); $st->append_tagged( $str, fgindex => 1 + rand 6 ); $st->append( " " ); } $st->say_to_terminal; String-Tagged-Terminal-0.07/lib000755001750001750 014527770376 15161 5ustar00leoleo000000000000String-Tagged-Terminal-0.07/lib/String000755001750001750 014527770376 16427 5ustar00leoleo000000000000String-Tagged-Terminal-0.07/lib/String/Tagged000755001750001750 014527770376 17622 5ustar00leoleo000000000000String-Tagged-Terminal-0.07/lib/String/Tagged/Terminal.pm000444001750001750 3044014527770376 22111 0ustar00leoleo000000000000# You may distribute under the terms of either the GNU General Public License # or the Artistic License (the same terms as Perl itself) # # (C) Paul Evans, 2017-2023 -- leonerd@leonerd.org.uk package String::Tagged::Terminal 0.07; use v5.14; use warnings; use base qw( String::Tagged ); use Carp; use constant HAVE_MSWIN32 => $^O eq "MSWin32"; HAVE_MSWIN32 and require String::Tagged::Terminal::Win32Console; require IO::Handle; =head1 NAME C - format terminal output using C =head1 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; =head1 DESCRIPTION This subclass of L provides a method, C, for outputting the formatting tags embedded in the string as terminal escape sequences, to render the the output in the appropriate style. =head1 TAGS The following tag names are recognised: =head2 bold, under, italic, strike, blink, reverse These tags take a boolean value. If the value is true then the corresponding terminal rendering attribute is enabled. =head2 altfont This tag takes an integer value. If defined it uses the "alternate font selection" sequence. =head2 fgindex, bgindex These tags take an integer value in the range 0 to 255. These select the foreground or background colour by using VGA, high-brightness extended 16 colour, or xterm 256 palette mode attributes, depending on the value. The ECMA-48-corrected string encoding form of C is used to set the 256 palette values. Values will be rounded down to the nearest integer by calling C. This convenience allows things like the C function for generating random colours: $st->append_tagged( "text", fgindex => 1 + rand 6 ); =head2 sizepos (experimental) This tag takes a value indicating an adjustment to the vertical positioning, and possibly also size, in order to create subscript or superscript effects. Recognised values are C for subscript, and C for superscript. These are implemented using the F-style C codes. =cut =head1 CONSTRUCTORS =cut =head2 new_from_formatting $st = String::Tagged::Terminal->new_from_formatting( $fmt ) Returns a new instance by converting L standard tags. Foreground and background colours are converted to their nearest index in the xterm 256 colour palette. The C Formatting attribute is rendered by selecting the first alternate font using C. =cut sub new_from_formatting { my $class = shift; my ( $orig ) = @_; require Convert::Color::XTerm; return $class->clone( $orig, only_tags => [qw( bold under italic strike blink reverse sizepos monospace fg bg )], convert_tags => { monospace => sub { $_[1] ? ( altfont => 1 ) : () }, fg => sub { fgindex => $_[1]->as_xterm->index }, bg => sub { bgindex => $_[1]->as_xterm->index }, }, ); } =head2 parse_terminal $st = String::Tagged::Terminal->parse_terminal( $str ); I Returns a new instance by parsing a string containing SGR terminal escape sequences mixed with plain string content. The parser will only accept 7- or 8-bit encodings of the SGR escape sequence (C<\e[ ... m> or C<\x9b[ ... m>). If any other escape sequences are present, an exception is thrown. Conversely, unrecognised formatting codes in SGR sequences are simply ignored without warning. =cut my $CSI_args = qr/[0-9;:]*/; sub parse_terminal { my $class = shift; my ( $s ) = @_; my $self = $class->new; pos($s) = 0; my %tags; while( pos($s) < length($s) ) { if( $s =~ m/\G([^\e]+)/gc ) { $self->append_tagged( $1, %tags ); } elsif( $s =~ m/\G\e\[($CSI_args)m/gc || $s =~ m/\G\x9b($CSI_args)m/gc ) { my $args = $1; length $args or $args = "0"; foreach my $arg ( split m/;/, $args ) { my ( $a0, @arest ) = map { int $_ } split m/:/, $arg; # Reset if( $a0 == 0 ) { %tags = () } # Simple boolean attributes elsif( $a0 == 1 ) { $tags{bold} = 1; } elsif( $a0 == 22 ) { delete $tags{bold}; } elsif( $a0 == 4 ) { $tags{under} = 1; } elsif( $a0 == 24 ) { delete $tags{under}; } elsif( $a0 == 3 ) { $tags{italic} = 1; } elsif( $a0 == 23 ) { delete $tags{italic}; } elsif( $a0 == 9 ) { $tags{strike} = 1; } elsif( $a0 == 29 ) { delete $tags{strike}; } elsif( $a0 == 5 ) { $tags{blink} = 1; } elsif( $a0 == 25 ) { delete $tags{blink}; } elsif( $a0 == 7 ) { $tags{reverse} = 1; } elsif( $a0 == 27 ) { delete $tags{reverse}; } # Numerical attributes elsif( $a0 >= 10 && $a0 <= 19 ) { $a0 > 10 ? $tags{altfont} = $a0 - 10 : delete $tags{altfont}; } # Colours elsif( $a0 >= 30 && $a0 <= 39 or $a0 >= 90 && $a0 <= 97 or $a0 >= 40 && $a0 <= 49 or $a0 >= 100 && $a0 <= 107 ) { my $hi = $a0 >= 90 ? 8 : 0; $a0 -= 60 if $hi; my $attr = $a0 < 40 ? "fgindex" : "bgindex"; $a0 %= 10; if ( $a0 == 9 ) { delete $tags{$attr} } elsif( $a0 == 8 ) { if( @arest >= 2 and $arest[0] == 5 ) { $tags{$attr} = $arest[1]; } # Else unrecognised } else { $tags{$attr} = $a0 + $hi } } # Sub/superscript elsif( $a0 == 73 ) { $tags{sizepos} = "super"; } elsif( $a0 == 74 ) { $tags{sizepos} = "sub"; } elsif( $a0 == 75 ) { delete $tags{sizepos}; } # Else unrecognised } } else { croak "Found an escape sequence that is not SGR"; } } return $self; } =head1 METHODS The following methods are provided in addition to those provided by L. =cut =head2 build_terminal $str = $st->build_terminal( %opts ); Returns a string containing terminal escape sequences mixed with string content to render the string to a terminal. As this string will contain literal terminal control escape sequences, care should be taken when passing it around, printing it for debugging purposes, or similar. Takes the following additional named options: =over 4 =item no_color If true, the C and C attributes will be ignored. This has the result of performing some formatting using the other attributes, but not setting colours. =back =cut sub build_terminal { my $self = shift; my %opts = @_; my $ret = ""; my %pen; $self->iter_substr_nooverlap( sub { my ( $s, %tags ) = @_; my @sgr; # Simple boolean attributes first foreach ( [ bold => 1, 22 ], [ under => 4, 24 ], [ italic => 3, 23 ], [ strike => 9, 29 ], [ blink => 5, 25 ], [ reverse => 7, 27 ], ) { my ( $tag, $on, $off ) = @$_; push( @sgr, $on ), $pen{$tag} = 1 if $tags{$tag} and !$pen{$tag}; push( @sgr, $off ), delete $pen{$tag} if !$tags{$tag} and $pen{$tag}; } # Numerical attributes foreach ( [ altfont => 10, 9 ], ) { my ( $tag, $base, $max ) = @$_; if( defined $pen{$tag} and !defined $tags{$tag} ) { push @sgr, $base; delete $pen{$tag}; } elsif( defined $pen{$tag} and defined $tags{$tag} and $pen{$tag} == $tags{$tag} ) { # Leave it } elsif( defined $tags{$tag} ) { my $val = $tags{$tag}; $val = $max if $val > $max; push @sgr, $base + $val; $pen{$tag} = $val; } } # Colour index attributes foreach ( [ fgindex => 30 ], [ bgindex => 40 ], ) { my ( $tag, $base ) = @$_; my $val = $tags{$tag}; $val = int $val if defined $val; if( defined $pen{$tag} and !defined $val ) { # Turn it off push @sgr, $base + 9; delete $pen{$tag}; } elsif( defined $pen{$tag} and defined $val and $pen{$tag} == $val ) { # Leave it } elsif( defined $val ) { if( $val < 8 ) { # VGA 8 push @sgr, $base + $val; } elsif( $val < 16 ) { # Hi 16 push @sgr, $base + 60 + ( $val - 8 ); } else { # Xterm256 palette 5 = 256 colours push @sgr, sprintf "%d:%d:%d", $base + 8, 5, $val; } $pen{$tag} = $val; } } { if( defined $pen{sizepos} and !defined $tags{sizepos} ) { push @sgr, 75; # reset delete $pen{sizepos}; } elsif( defined $pen{sizepos} and defined $tags{sizepos} and $pen{sizepos} eq $tags{sizepos} ) { # Leave it } elsif( defined( my $val = $tags{sizepos} ) ) { if( $val eq "sub" ) { push @sgr, 74; } elsif( $val eq "super" ) { push @sgr, 73; } $pen{sizepos} = $val; } } if( @sgr and %pen ) { $ret .= "\e[" . join( ";", @sgr ) . "m"; } elsif( @sgr ) { $ret .= "\e[m"; } $ret .= $s; }, ( $opts{no_color} ? ( except => [qw( fgindex bgindex )] ) : () ) ); $ret .= "\e[m" if %pen; return $ret; } =head2 as_formatting $fmt = $st->as_formatting; Returns a new C instance tagged with L standard tags. =cut sub as_formatting { my $self = shift; require Convert::Color::XTerm; return String::Tagged->clone( $self, only_tags => [qw( bold under italic strike blink reverse sizepos altfont fgindex bgindex )], convert_tags => { altfont => sub { $_[1] == 1 ? ( monospace => 1 ) : () }, fgindex => sub { fg => Convert::Color::XTerm->new( $_[1] ) }, bgindex => sub { bg => Convert::Color::XTerm->new( $_[1] ) }, }, ); } =head2 print_to_terminal $str->print_to_terminal( $fh ); I Prints the string to the terminal by building a terminal escape string then printing it to the given IO handle (or C if not supplied). This method will pass the value of the C environment variable to the underlying L method call, meaning if that has a true value then colouring tags will be ignored, yielding a monochrome output. This follows the suggestion of L. =cut sub print_to_terminal { my $self = shift; my ( $fh, %options ) = @_; $fh //= \*STDOUT; $options{win32}++ if HAVE_MSWIN32 and not exists $options{win32}; if( $options{win32} ) { $self->String::Tagged::Terminal::Win32Console::print_to_console( $fh, %options ); } else { $fh->print( $self->build_terminal( no_color => $ENV{NO_COLOR} ) ); } } =head2 say_to_terminal $str->say_to_terminal( $fh ); I Prints the string to the terminal as per L, followed by a linefeed. =cut sub say_to_terminal { my $self = shift; my ( $fh, %options ) = @_; $fh //= \*STDOUT; $self->print_to_terminal( $fh, %options ); $fh->say; } =head1 COMPATIBILITY NOTES On Windows, the following notes apply: =over 4 =item * On all versions of Windows, the attributes C, C and C are supported. The C attribute is implemented by using high-intensity colours, so will be indistinguishable from using high-intensity colour indexes without bold. The full 256-color palette is not supported by Windows, so it is down-converted to the 16 colours that are. =item * Starting with Windows 10, also C and C are supported. =item * The attributes C, C, C, C are not supported on any Windows version. =item * On Windows, only a single output console is supported. =back =cut =head1 AUTHOR Paul Evans =cut 0x55AA; String-Tagged-Terminal-0.07/lib/String/Tagged/Terminal000755001750001750 014527770376 21375 5ustar00leoleo000000000000String-Tagged-Terminal-0.07/lib/String/Tagged/Terminal/Win32Console.pm000444001750001750 1042414527770376 24336 0ustar00leoleo000000000000# You may distribute under the terms of either the GNU General Public License # or the Artistic License (the same terms as Perl itself) # # (C) Paul Evans, 2017-2018 -- leonerd@leonerd.org.uk package String::Tagged::Terminal::Win32Console 0.07; use v5.14; use warnings; use Win32::Console; use List::Util qw( max ); =head1 NAME C - Windows-specific code for L =head1 SYNOPSIS # No user serviceable parts inside use String::Tagged::Terminal; =head1 DESCRIPTION This module provides support for L to print to the console on C. It is not intended to be used directly. =cut use constant { ATTR_BLUE => 0x0001, ATTR_GREEN => 0x0002, ATTR_RED => 0x0004, ATTR_INTENSITY => 0x0008, ATTR_REVERSE_VIDEO => 0x4000, # Windows 10 onwards ATTR_UNDERSCORE => 0x8000, # Windows 10 onwards }; # We can only ever allocate a single console on Windows our $WIN32_CONSOLE; my %color_to_attr; # a cache sub print_to_console { my $self = shift; my ( $fh, %opts ) = @_; # Convert filenos to native Win32 file handles, this should also try # Win32API::File::FdGetOsFHandle( $fh ); my $fileno = { 1 => Win32::Console::STD_OUTPUT_HANDLE(), 2 => Win32::Console::STD_ERROR_HANDLE(), }->{ $fh->fileno } || $fh->fileno; my %output_options = ( ( $opts{no_color} ? ( except => [qw( fgindex bgindex )] ) : () ), only => [qw( fgindex bgindex bold under reverse )], # only process what we can handle ); if( $fileno < 0 ) { # This looks like a Perl-internal FH, let's not output any formatting $fh->print( $self->build_terminal( %opts ) ); } else { my $console = $opts{console} || do { $WIN32_CONSOLE ||= Win32::Console->new( $fileno ); }; my $saved = $console->Attr(); my $attr = $saved; $self->iter_substr_nooverlap( sub { my ( $s, %tags ) = @_; # Simple boolean attributes first foreach ( # bold is handled at the end [ under => ATTR_UNDERSCORE ], # Rendering is flakey under Windows 10 # Windows console doesn't support italic, strike, blink [ reverse => ATTR_REVERSE_VIDEO ], ) { my ( $tag, $on ) = @$_; $attr &= ~$on; $attr |= $on if $tags{$tag}; } # Colour index attributes foreach ( [ fgindex => 0, ], [ bgindex => 4, ], ) { my ( $tag, $shift ) = @$_; my $mask = 0x000F << $shift; $attr &= ~$mask; if( defined $tags{$tag} ) { my $idx = $tags{$tag}; $attr |= ( $color_to_attr{$idx} //= _color_to_attr( $idx ) ) << $shift; } else { # Restore to previous $attr |= $saved & $mask; } } $attr |= ATTR_INTENSITY if $tags{bold}; $console->Attr($attr); $console->Write($s); }, %output_options ); $console->Attr( $saved ); } } sub _color_to_attr { my ( $idx ) = @_; my $attr = 0; if( $idx >= 16 ) { # Attempt to convert xterm256 range into RGB+I require Convert::Color; my $color = Convert::Color->new( "xterm:$idx" )->as_rgb; my ( $red, $green, $blue ) = $color->rgb; my $max = max( $red, $green, $blue ); $attr |= ATTR_RED if $red > 0.5; $attr |= ATTR_GREEN if $green > 0.5; $attr |= ATTR_BLUE if $blue > 0.5; $attr |= ATTR_INTENSITY if $max > 0.75; $attr = ATTR_INTENSITY if $attr == 0 and $red == $green and $red == $blue and $max > 0.25; # dark grey } else { # The bits are swapped between ANSI and Win32 console $attr |= ATTR_RED if $idx & 1; $attr |= ATTR_GREEN if $idx & 2; $attr |= ATTR_BLUE if $idx & 4; $attr |= ATTR_INTENSITY if $idx & 8; } return $attr; } =head1 COMPATIBILITY NOTES On Windows before Windows 10, only C, C and C are supported. Starting with Windows 10, also C and C are supported. On Windows, only a single output console is supported. =head1 AUTHOR Paul Evans , Max Maischein =cut 0x55AA; String-Tagged-Terminal-0.07/t000755001750001750 014527770376 14656 5ustar00leoleo000000000000String-Tagged-Terminal-0.07/t/00use.t000444001750001750 30714527770376 16114 0ustar00leoleo000000000000#!/usr/bin/perl use v5.14; use warnings; use Test2::V0; require String::Tagged::Terminal; require String::Tagged::Terminal::Win32Console if $^O eq "MSWin32"; pass "Modules loaded"; done_testing; String-Tagged-Terminal-0.07/t/01build.t000444001750001750 561114527770376 16443 0ustar00leoleo000000000000#!/usr/bin/perl use v5.14; use warnings; use Test2::V0; use String::Tagged::Terminal; # unformatted { my $st = String::Tagged::Terminal->new ->append( "Hello, world!" ); is( $st->build_terminal, "Hello, world!", '->build_terminal on unformatted' ); } # simple boolean attribute formatting { my $st = String::Tagged::Terminal->new ->append( "A " ) ->append_tagged( "bold", bold => 1 ) ->append( " string" ); is( $st->build_terminal, "A \e[1mbold\e[m string", '->build_terminal on bold' ); $st = String::Tagged::Terminal->new ->append( "An " ) ->append_tagged( "underlined", under => 1 ) ->append( " string" ); is( $st->build_terminal, "An \e[4munderlined\e[m string", '->build_terminal on under' ); } # Altfont attribute { my $st = String::Tagged::Terminal->new ->append( "Some " ) ->append_tagged( "fixedwidth", altfont => 1 ) ->append( " and " ) ->append_tagged( "fancy", altfont => 2 ) ->append( " formatting" ); is( $st->build_terminal, "Some \e[11mfixedwidth\e[m and \e[12mfancy\e[m formatting", '->build_terminal on altfont' ); } # Colours { my $st = String::Tagged::Terminal->new ->append( "With " ) ->append_tagged( "colour", fgindex => 1 ); is( $st->build_terminal, "With \e[31mcolour\e[m", '->build_terminal on VGA8 colour' ); $st = String::Tagged::Terminal->new ->append( "With " ) ->append_tagged( "hi-colour", fgindex => 10 ); is( $st->build_terminal, "With \e[92mhi-colour\e[m", '->build_terminal on Hi16 colour' ); $st = String::Tagged::Terminal->new ->append( "With " ) ->append_tagged( "palette colour", fgindex => 50 ); is( $st->build_terminal, "With \e[38:5:50mpalette colour\e[m", '->build_terminal on xterm256 colour' ); $st = String::Tagged::Terminal->new ->append( "With " ) ->append_tagged( "noninteger", fgindex => 3.14159 ); is( $st->build_terminal, "With \e[33mnoninteger\e[m", '->build_terminal rounds to integer' ); } # Trailing format gets reset { my $st = String::Tagged::Terminal->new ->append( "Has trailing " ) ->append_tagged( "formatting", italic => 1 ); is( $st->build_terminal, "Has trailing \e[3mformatting\e[m", 'Trailing formatting is reset' ); } # Neighbouring colours can be swapped { my $st = String::Tagged::Terminal->new ->append_tagged( "R", fgindex => 1 ) ->append_tagged( "G", fgindex => 2 ) ->append_tagged( "B", fgindex => 4 ); is( $st->build_terminal, "\e[31mR\e[32mG\e[34mB\e[m", 'Neighbouring colour tags behave' ); } # no_color { my $st = String::Tagged::Terminal->new( "abcde" ); $st->apply_tag( 0, 3, under => 1 ); $st->apply_tag( 2, 3, fgindex => 2 ); is( $st->build_terminal( no_color => 1 ), "\e[4mabc\e[mde", 'no_color option surpresses fgindex but not under' ); } done_testing; String-Tagged-Terminal-0.07/t/02parse.t000444001750001750 351214527770376 16455 0ustar00leoleo000000000000#!/usr/bin/perl use v5.14; use warnings; use Test2::V0; use String::Tagged::Terminal; sub test_roundtrip { my ( $string, $title ) = @_; my $st = String::Tagged::Terminal->parse_terminal( $string ); is( $st->build_terminal, $string, "$title round-trip parses" ); } test_roundtrip "Some plain text", 'plain text'; test_roundtrip "Some \e[1mbold\e[m text", 'bold text'; test_roundtrip "Some \e[4munder\e[m text", 'under text'; test_roundtrip "Some \e[3mitalic\e[m text", 'italic text'; test_roundtrip "Some \e[9mstrike\e[m text", 'strike text'; test_roundtrip "Some \e[5mblink\e[m text", 'blink text'; test_roundtrip "Some \e[7mreverse\e[m text", 'reverse text'; test_roundtrip "Some \e[11maltfont\e[m text", 'altfont text'; test_roundtrip "\e[31mred\e[m \e[32mgreen\e[m", 'fg basic colour text'; test_roundtrip "\e[91mhi-red\e[m", 'fg hi colour text'; test_roundtrip "\e[38:5:123mpalette\e[m", 'fg palette colour text'; test_roundtrip "\e[41mred\e[m \e[42mgreen\e[m", 'bg basic colour text'; test_roundtrip "\e[101mhi-red\e[m", 'bg hi colour text'; test_roundtrip "\e[73msuperscript\e[m \e[74msubscript\e[m", 'sub/superscript text'; sub test_parsebuild { my ( $inp, $out, $title ) = @_; my $st = String::Tagged::Terminal->parse_terminal( $inp ); is( $st->build_terminal, $out, "$title parses and builds" ); } # Various forms of reset test_parsebuild "\e[1mX\e[0mY", "\e[1mX\e[mY", 'SGR 0 reset'; test_parsebuild "\e[1mX\e[00mY", "\e[1mX\e[mY", 'SGR 00 reset'; # Other terminal escapes are not accepted like( dies { String::Tagged::Terminal->parse_terminal( "Here\e[AThere" ) }, qr/^Found an escape sequence that is not SGR at / ); # Unrecognised SGRs do not warn ok( no_warnings { String::Tagged::Terminal->parse_terminal( "\e[6mXYZ" ) }, 'Unrecognised SGR codes do not warn' ); done_testing; String-Tagged-Terminal-0.07/t/10formatting.t000444001750001750 200714527770376 17512 0ustar00leoleo000000000000#!/usr/bin/perl use v5.14; use warnings; use Test2::V0; use String::Tagged::Terminal; use Convert::Color; # ->new_from_formatting { # 100% red is index 9 my $st = String::Tagged::Terminal->new_from_formatting( String::Tagged->new_tagged( "red", fg => Convert::Color->new( 'rgb:1,0,0' ) ) ); is( $st->get_tag_at( 0, "fgindex" ), 9, '$st has fgindex tag' ); # monospace is altfont=1 $st = String::Tagged::Terminal->new_from_formatting( String::Tagged->new_tagged( "mono", monospace => 1 ) ); is( $st->get_tag_at( 0, "altfont" ), 1, '$st has altfont tag' ); } # ->as_formatting { # 100% green is index 10 my $st = String::Tagged::Terminal->new_tagged( "green", fgindex => 10 ) ->as_formatting; is( uc $st->get_tag_at( 0, "fg" )->hex, "00FF00", '$st has fg tag' ); # altfont=1 is monospace $st = String::Tagged::Terminal->new_tagged( "fixed", altfont => 1 ) ->as_formatting; ok( $st->get_tag_at( 0, "monospace" ), '$st has monospace tag' ); } done_testing; String-Tagged-Terminal-0.07/t/20print.t000444001750001750 113514527770376 16476 0ustar00leoleo000000000000#!/usr/bin/perl use v5.14; use warnings; use Test2::V0; use String::Tagged::Terminal; my $st = String::Tagged::Terminal->new ->append_tagged( "a string with " ) ->append_tagged( "bold", bold => 1 ) ->append_tagged( " formatting" ); { open my $fh, ">", \my $output; $st->print_to_terminal( $fh ); is( $output, "a string with \e[1mbold\e[m formatting", 'printed correct output' ); } { open my $fh, ">", \my $output; $st->say_to_terminal( $fh ); like( $output, qr/a string with \e\[1mbold\e\[m formatting\r?\n/, 'output includes linefeed' ); } done_testing; String-Tagged-Terminal-0.07/t/30print-win32.t000444001750001750 370014527770376 17437 0ustar00leoleo000000000000#!/usr/bin/perl use v5.14; use warnings; use Test2::V0; BEGIN { $^O = "MSWin32"; } my ( $Attr, @Log ); { BEGIN { $INC{"Win32/Console.pm"} = __FILE__; } package Win32::Console; use constant { STD_OUTPUT_HANDLE => 1, STD_ERROR_HANDLE => 2, }; sub new { return bless [], shift } sub Attr { shift; my $old = $Attr; push @Log, [ Attr => $Attr = $_[0] ] if @_; return $old } sub Write { shift; push @Log, [ Write => $_[0] ] } } use String::Tagged::Terminal; $Attr = 7; # default white-on-black, no underline { my $st = String::Tagged::Terminal->new ->append_tagged( "some " ) ->append_tagged( "underlined", under => 1 ) ->append_tagged( " text" ); undef @Log; $st->print_to_terminal; is( \@Log, [ [ Attr => 7 ], [ Write => "some " ], [ Attr => 0x8007 ], [ Write => "underlined" ], [ Attr => 7 ], [ Write => " text" ], [ Attr => 7 ], ], 'log of console methods for coloured output' ); } { my $st = String::Tagged::Terminal->new ->append_tagged( "output " ) ->append_tagged( "coloured", fgindex => 1 ) ->append_tagged( " text" ); undef @Log; $st->print_to_terminal; is( \@Log, [ [ Attr => 7 ], [ Write => "output " ], [ Attr => 4 ], [ Write => "coloured" ], [ Attr => 7 ], [ Write => " text" ], [ Attr => 7 ], ], 'log of console methods for coloured output' ); } { my $st = String::Tagged::Terminal->new ->append_tagged( "with " ) ->append_tagged( "bold", bold => 1 ) ->append_tagged( " text" ); undef @Log; $st->print_to_terminal; is( \@Log, [ [ Attr => 7 ], [ Write => "with " ], [ Attr => 15 ], [ Write => "bold" ], [ Attr => 7 ], [ Write => " text" ], [ Attr => 7 ], ], 'log of console methods for bold output' ); } done_testing; String-Tagged-Terminal-0.07/t/99pod.t000444001750001750 25514527770376 16126 0ustar00leoleo000000000000#!/usr/bin/perl use v5.14; use warnings; use Test2::V0; eval "use Test::Pod 1.00"; plan skip_all => "Test::Pod 1.00 required for testing POD" if $@; all_pod_files_ok();