| 1 | # ----------------------------------------------------------------------------- | ||||||||||||
| 2 | |||||||||||||
| 3 | =encoding utf8 | ||||||||||||
| 4 | |||||||||||||
| 5 | =head1 NAME | ||||||||||||
| 6 | |||||||||||||
| 7 | Quiq::AnsiColor - Erzeuge Text mit/ohne ANSI Colorcodes | ||||||||||||
| 8 | |||||||||||||
| 9 | =head1 BASE CLASS | ||||||||||||
| 10 | |||||||||||||
| 11 | L<Quiq::Object> | ||||||||||||
| 12 | |||||||||||||
| 13 | =head1 SYNOPSIS | ||||||||||||
| 14 | |||||||||||||
| 15 | use Quiq::AnsiColor; | ||||||||||||
| 16 | |||||||||||||
| 17 | my $a = Quiq::AnsiColor->new(-t STDOUT); | ||||||||||||
| 18 | say $a->str('bold white on_cyan','Hello, world!'); | ||||||||||||
| 19 | |||||||||||||
| 20 | =head1 DESCRIPTION | ||||||||||||
| 21 | |||||||||||||
| 22 | Die Klasse erlaubt es, Textausgaben - die typischerweise aufs | ||||||||||||
| 23 | Terminal gehen - mit ANSI Colorcodes auszuzeichnen und diese | ||||||||||||
| 24 | Auszeichnung bei Aufruf des Konstruktors zentral an- oder ab-zuschalten. | ||||||||||||
| 25 | |||||||||||||
| 26 | =head2 Terminal-Eigenschaften | ||||||||||||
| 27 | |||||||||||||
| 28 | Allgemein Vordergrund Hintergrund | ||||||||||||
| 29 | ----------- ----------- ----------- | ||||||||||||
| 30 | dark black on_black | ||||||||||||
| 31 | bold red on_red | ||||||||||||
| 32 | underline green on_green | ||||||||||||
| 33 | blink yellow on_yellow | ||||||||||||
| 34 | reverse blue on_blue | ||||||||||||
| 35 | concealed magenta on_magenta | ||||||||||||
| 36 | reset cyan on_cyan | ||||||||||||
| 37 | white on_white | ||||||||||||
| 38 | |||||||||||||
| 39 | Es kann eine Kombination aus Eigenschaften angegeben werden. Mehrere | ||||||||||||
| 40 | aus der Rubrik "Allgemein", eine aus der Rubrik "Vordergrund", | ||||||||||||
| 41 | eine aus der Rubrik "Hintergrund". Werden mehrere Eigenschaften | ||||||||||||
| 42 | angegeben, werden diese durch Leerzeichen getrennt. | ||||||||||||
| 43 | |||||||||||||
| 44 | Beispiele: 'bold reverse' oder 'dark red on_green' | ||||||||||||
| 45 | |||||||||||||
| 46 | =head2 Texte mit Colorcodes weiter verarbeiten | ||||||||||||
| 47 | |||||||||||||
| 48 | Die im folgenden genannten Programme C<aha> und C<wkhtmltopdf> sind im | ||||||||||||
| 49 | Debian-Repository enthalten. | ||||||||||||
| 50 | |||||||||||||
| 51 | =head3 Nach PDF wandeln | ||||||||||||
| 52 | |||||||||||||
| 53 | $ PROGRAM | aha | wkhtmltopdf - FILE.pdf | ||||||||||||
| 54 | |||||||||||||
| 55 | =head3 Drucken | ||||||||||||
| 56 | |||||||||||||
| 57 | $ PROGRAM | aha | wkhtmltopdf - - | lpr | ||||||||||||
| 58 | |||||||||||||
| 59 | =head3 Im Pager anzeigen | ||||||||||||
| 60 | |||||||||||||
| 61 | $ PROGRAM | less -R | ||||||||||||
| 62 | |||||||||||||
| 63 | =head1 SEE ALSO | ||||||||||||
| 64 | |||||||||||||
| 65 | =over 2 | ||||||||||||
| 66 | |||||||||||||
| 67 | =item * | ||||||||||||
| 68 | |||||||||||||
| 69 | Term::ANSIColor | ||||||||||||
| 70 | |||||||||||||
| 71 | =back | ||||||||||||
| 72 | |||||||||||||
| 73 | =cut | ||||||||||||
| 74 | |||||||||||||
| 75 | # ----------------------------------------------------------------------------- | ||||||||||||
| 76 | |||||||||||||
| 77 | package Quiq::AnsiColor; | ||||||||||||
| 78 | 270 | ▶ | use base qw/Quiq::Object/; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 79 | |||||||||||||
| 80 | 270 | ▶ | use v5.10; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 81 | 270 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 82 | 270 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 83 | |||||||||||||
| 84 | 270 | our $VERSION = '1.240'; | |||||||||||
| 85 | |||||||||||||
| 86 | 270 | ▶ | use Term::ANSIColor (); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 87 | |||||||||||||
| 88 | # ----------------------------------------------------------------------------- | ||||||||||||
| 89 | |||||||||||||
| 90 | =head1 METHODS | ||||||||||||
| 91 | |||||||||||||
| 92 | =head2 Konstruktor | ||||||||||||
| 93 | |||||||||||||
| 94 | =head3 new() - Instantiiere Objekt | ||||||||||||
| 95 | |||||||||||||
| 96 | =head4 Synopsis | ||||||||||||
| 97 | |||||||||||||
| 98 | $a = $class->new; | ||||||||||||
| 99 | $a = $class->new($bool); | ||||||||||||
| 100 | |||||||||||||
| 101 | =head4 Arguments | ||||||||||||
| 102 | |||||||||||||
| 103 | =over 4 | ||||||||||||
| 104 | |||||||||||||
| 105 | =item $bool (Default: 1) | ||||||||||||
| 106 | |||||||||||||
| 107 | Wenn wahr, findet eine Auszeichnung mit ANSI Colorcodes statt, wenn | ||||||||||||
| 108 | falsch, nicht. Ist das Argument nicht angegeben, ist dies | ||||||||||||
| 109 | gleichbedeutend mit wahr. | ||||||||||||
| 110 | |||||||||||||
| 111 | =back | ||||||||||||
| 112 | |||||||||||||
| 113 | =head4 Returns | ||||||||||||
| 114 | |||||||||||||
| 115 | AnsiColor-Objekt | ||||||||||||
| 116 | |||||||||||||
| 117 | =head4 Description | ||||||||||||
| 118 | |||||||||||||
| 119 | Instantiiere ein Objekt der Klasse und liefere dieses zurück. Durch | ||||||||||||
| 120 | den Parameter $bool wird entschieden, ob die Methode | ||||||||||||
| 121 | L<str|"str() - Formatiere String mit Colorcodes">() ihren Rückgabewert mit | ||||||||||||
| 122 | oder ohne ANSI Colorcodes liefert. | ||||||||||||
| 123 | |||||||||||||
| 124 | =cut | ||||||||||||
| 125 | |||||||||||||
| 126 | # ----------------------------------------------------------------------------- | ||||||||||||
| 127 | |||||||||||||
| 128 | 13 | ▶ | sub new { | ||||||||||
|
Subroutine
new: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||
| 129 | 13 | my $class = shift; | |||||||||||
| 130 | 13 | ▶ | my $bool = @_? shift: 1; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 131 | |||||||||||||
| 132 | 13 | return bless \$bool,$class; | |||||||||||
| 133 | } | ||||||||||||
| 134 | |||||||||||||
| 135 | # ----------------------------------------------------------------------------- | ||||||||||||
| 136 | |||||||||||||
| 137 | =head2 Objektmethoden | ||||||||||||
| 138 | |||||||||||||
| 139 | =head3 active() - Farbdarstellung eingeschaltet? | ||||||||||||
| 140 | |||||||||||||
| 141 | =head4 Synopsis | ||||||||||||
| 142 | |||||||||||||
| 143 | $bool = $a->active; | ||||||||||||
| 144 | |||||||||||||
| 145 | =head4 Returns | ||||||||||||
| 146 | |||||||||||||
| 147 | Bool | ||||||||||||
| 148 | |||||||||||||
| 149 | =head4 Description | ||||||||||||
| 150 | |||||||||||||
| 151 | Liefere wahr, wenn ANSI Colorcodes aktiviert sind, anderfalls falsch. | ||||||||||||
| 152 | |||||||||||||
| 153 | =cut | ||||||||||||
| 154 | |||||||||||||
| 155 | # ----------------------------------------------------------------------------- | ||||||||||||
| 156 | |||||||||||||
| 157 | 3 | ▶ | sub active { | ||||||||||
|
Subroutine
active: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 158 | 3 | return ${$_[0]}; | |||||||||||
| 159 | } | ||||||||||||
| 160 | |||||||||||||
| 161 | # ----------------------------------------------------------------------------- | ||||||||||||
| 162 | |||||||||||||
| 163 | =head3 str() - Formatiere String mit Colorcodes | ||||||||||||
| 164 | |||||||||||||
| 165 | =head4 Synopsis | ||||||||||||
| 166 | |||||||||||||
| 167 | $str = $a->str($attr,$str); | ||||||||||||
| 168 | |||||||||||||
| 169 | =head4 Arguments | ||||||||||||
| 170 | |||||||||||||
| 171 | =over 4 | ||||||||||||
| 172 | |||||||||||||
| 173 | =item $attr | ||||||||||||
| 174 | |||||||||||||
| 175 | Attribut-Spezifikation gemäß Term::ANSIColor (siehe auch | ||||||||||||
| 176 | L<Terminal-Eigenschaften|"Terminal-Eigenschaften">). | ||||||||||||
| 177 | |||||||||||||
| 178 | =item $str | ||||||||||||
| 179 | |||||||||||||
| 180 | Zeichenkette, die mit ANSI Colorcodes formatiert wird. | ||||||||||||
| 181 | |||||||||||||
| 182 | =back | ||||||||||||
| 183 | |||||||||||||
| 184 | =head4 Returns | ||||||||||||
| 185 | |||||||||||||
| 186 | Zeichenkette mit ANSI Colorcodes (String) | ||||||||||||
| 187 | |||||||||||||
| 188 | =head4 Description | ||||||||||||
| 189 | |||||||||||||
| 190 | Formatiere Zeichenkette $str mit ANSI Colorcodes gemäß | ||||||||||||
| 191 | Spezifikation $attr und liefere das Resultat zurück. Die | ||||||||||||
| 192 | Colorcodes werden der Zeichenkette vorangestellt und mit dem | ||||||||||||
| 193 | Colorcode 'reset' beendet. Ist die Erzeugung von Colorcodes | ||||||||||||
| 194 | abgeschaltet (siehe Konstruktor), wird die Zeichenkette unverändert | ||||||||||||
| 195 | geliefert. | ||||||||||||
| 196 | |||||||||||||
| 197 | Ist $str C<undef>, wird ein Leerstring geliefert. | ||||||||||||
| 198 | |||||||||||||
| 199 | =cut | ||||||||||||
| 200 | |||||||||||||
| 201 | # ----------------------------------------------------------------------------- | ||||||||||||
| 202 | |||||||||||||
| 203 | 0 | ▶ | sub str { | ||||||||||
|
Subroutine
str: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||
| 204 | 0 | my ($self,$attr,$str) = @_; | |||||||||||
| 205 | |||||||||||||
| 206 | 0 | ▶ | $str //= ''; | ||||||||||
|
Condition0%0/2
$str //= ""
MC/DC0%0/1
$str //= ""
$str
Truth table0%0/2
A
A= $str
| |||||||||||||
| 207 | |||||||||||||
| 208 | 0 | ▶ | if ($$self) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 209 | 0 | my $esc = Term::ANSIColor::color($attr); | |||||||||||
| 210 | 0 | my $reset = Term::ANSIColor::color('reset'); | |||||||||||
| 211 | 0 | $str =~ s/^(\s*)(.*)/$1$esc$2$reset/mg; | |||||||||||
| 212 | # $str =~ s/$/$reset/mg; | ||||||||||||
| 213 | 0 | return $str; | |||||||||||
| 214 | } | ||||||||||||
| 215 | |||||||||||||
| 216 | 0 | return $str; | |||||||||||
| 217 | } | ||||||||||||
| 218 | |||||||||||||
| 219 | # ----------------------------------------------------------------------------- | ||||||||||||
| 220 | |||||||||||||
| 221 | =head3 strLn() - Formatiere Zeile mit Colorcodes | ||||||||||||
| 222 | |||||||||||||
| 223 | =head4 Synopsis | ||||||||||||
| 224 | |||||||||||||
| 225 | $str = $a->strLn($attr,$str); | ||||||||||||
| 226 | |||||||||||||
| 227 | =head4 Arguments | ||||||||||||
| 228 | |||||||||||||
| 229 | Siehe L<str|"str() - Formatiere String mit Colorcodes">() | ||||||||||||
| 230 | |||||||||||||
| 231 | =head4 Returns | ||||||||||||
| 232 | |||||||||||||
| 233 | Siehe L<str|"str() - Formatiere String mit Colorcodes">() | ||||||||||||
| 234 | |||||||||||||
| 235 | =head4 Description | ||||||||||||
| 236 | |||||||||||||
| 237 | Wie L<str|"str() - Formatiere String mit Colorcodes">(), nur dass der | ||||||||||||
| 238 | Rückgabewert mit einem Newline beendet wird. | ||||||||||||
| 239 | |||||||||||||
| 240 | =cut | ||||||||||||
| 241 | |||||||||||||
| 242 | # ----------------------------------------------------------------------------- | ||||||||||||
| 243 | |||||||||||||
| 244 | 0 | ▶ | sub strLn { | ||||||||||
|
Subroutine
strLn: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||
| 245 | 0 | return shift->str(@_)."\n"; | |||||||||||
| 246 | } | ||||||||||||
| 247 | |||||||||||||
| 248 | # ----------------------------------------------------------------------------- | ||||||||||||
| 249 | |||||||||||||
| 250 | =head1 VERSION | ||||||||||||
| 251 | |||||||||||||
| 252 | 1.240 | ||||||||||||
| 253 | |||||||||||||
| 254 | =head1 AUTHOR | ||||||||||||
| 255 | |||||||||||||
| 256 | Frank Seitz, L<http://fseitz.de/> | ||||||||||||
| 257 | |||||||||||||
| 258 | =head1 COPYRIGHT | ||||||||||||
| 259 | |||||||||||||
| 260 | Copyright (C) 2026 Frank Seitz | ||||||||||||
| 261 | |||||||||||||
| 262 | =head1 LICENSE | ||||||||||||
| 263 | |||||||||||||
| 264 | This code is free software; you can redistribute it and/or modify | ||||||||||||
| 265 | it under the same terms as Perl itself. | ||||||||||||
| 266 | |||||||||||||
| 267 | =cut | ||||||||||||
| 268 | |||||||||||||
| 269 | # ----------------------------------------------------------------------------- | ||||||||||||
| 270 | |||||||||||||
| 271 | 270 | 1; | |||||||||||
| 272 | |||||||||||||
| 273 | # eof | ||||||||||||