| 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.238'; | |||||||||
| 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 | sub new { | ||||||||||
| 129 | 13 | ▶ | my $class = shift; | ||||||||
|
Subroutine
new: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 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 | sub active { | ||||||||||
| 158 | 3 | ▶ | return ${$_[0]}; | ||||||||
|
Subroutine
active: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 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 | =cut | ||||||||||
| 198 | |||||||||||
| 199 | # ----------------------------------------------------------------------------- | ||||||||||
| 200 | |||||||||||
| 201 | sub str { | ||||||||||
| 202 | 0 | ▶ | my ($self,$attr,$str) = @_; | ||||||||
|
Subroutine
str: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||
| 203 | |||||||||||
| 204 | 0 | ▶ | if ($$self) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 205 | 0 | my $esc = Term::ANSIColor::color($attr); | |||||||||
| 206 | 0 | my $reset = Term::ANSIColor::color('reset'); | |||||||||
| 207 | 0 | $str =~ s/^(\s*)(.*)/$1$esc$2$reset/mg; | |||||||||
| 208 | # $str =~ s/$/$reset/mg; | ||||||||||
| 209 | 0 | return $str; | |||||||||
| 210 | } | ||||||||||
| 211 | |||||||||||
| 212 | 0 | return $str; | |||||||||
| 213 | } | ||||||||||
| 214 | |||||||||||
| 215 | # ----------------------------------------------------------------------------- | ||||||||||
| 216 | |||||||||||
| 217 | =head3 strLn() - Formatiere Zeile mit Colorcodes | ||||||||||
| 218 | |||||||||||
| 219 | =head4 Synopsis | ||||||||||
| 220 | |||||||||||
| 221 | $str = $a->strLn($attr,$str); | ||||||||||
| 222 | |||||||||||
| 223 | =head4 Arguments | ||||||||||
| 224 | |||||||||||
| 225 | Siehe L<str|"str() - Formatiere String mit Colorcodes">() | ||||||||||
| 226 | |||||||||||
| 227 | =head4 Returns | ||||||||||
| 228 | |||||||||||
| 229 | Siehe L<str|"str() - Formatiere String mit Colorcodes">() | ||||||||||
| 230 | |||||||||||
| 231 | =head4 Description | ||||||||||
| 232 | |||||||||||
| 233 | Wie L<str|"str() - Formatiere String mit Colorcodes">(), nur dass der | ||||||||||
| 234 | Rückgabewert mit einem Newline beendet wird. | ||||||||||
| 235 | |||||||||||
| 236 | =cut | ||||||||||
| 237 | |||||||||||
| 238 | # ----------------------------------------------------------------------------- | ||||||||||
| 239 | |||||||||||
| 240 | sub strLn { | ||||||||||
| 241 | 0 | ▶ | return shift->str(@_)."\n"; | ||||||||
|
Subroutine
strLn: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 242 | } | ||||||||||
| 243 | |||||||||||
| 244 | # ----------------------------------------------------------------------------- | ||||||||||
| 245 | |||||||||||
| 246 | =head1 VERSION | ||||||||||
| 247 | |||||||||||
| 248 | 1.238 | ||||||||||
| 249 | |||||||||||
| 250 | =head1 AUTHOR | ||||||||||
| 251 | |||||||||||
| 252 | Frank Seitz, L<http://fseitz.de/> | ||||||||||
| 253 | |||||||||||
| 254 | =head1 COPYRIGHT | ||||||||||
| 255 | |||||||||||
| 256 | Copyright (C) 2026 Frank Seitz | ||||||||||
| 257 | |||||||||||
| 258 | =head1 LICENSE | ||||||||||
| 259 | |||||||||||
| 260 | This code is free software; you can redistribute it and/or modify | ||||||||||
| 261 | it under the same terms as Perl itself. | ||||||||||
| 262 | |||||||||||
| 263 | =cut | ||||||||||
| 264 | |||||||||||
| 265 | # ----------------------------------------------------------------------------- | ||||||||||
| 266 | |||||||||||
| 267 | 270 | 1; | |||||||||
| 268 | |||||||||||
| 269 | # eof | ||||||||||