File Coverage

blib/lib/Print/Colored.pm
Criterion Covered Total %
statement 30 30 100.0
branch n/a
condition n/a
subroutine 13 13 100.0
pod n/a
total 43 43 100.0


line stmt bran cond sub pod time code
1             package Print::Colored;
2 3     3   274364 use strict;
  3         34  
  3         85  
3 3     3   14 use warnings;
  3         4  
  3         140  
4 3     3   17 use utf8;
  3         4  
  3         20  
5 3     3   96 use v5.24.0;
  3         9  
6              
7 3     3   15 use Exporter 'import';
  3         5  
  3         88  
8 3     3   2142 use IO::Prompter;
  3         85892  
  3         19  
9 3     3   2027 use Term::ANSIColor qw|colored coloralias|;
  3         21299  
  3         2413  
10              
11             our $VERSION = '0.03';
12              
13             our @EXPORT = ();
14             our @EXPORT_OK = ();
15             our %EXPORT_TAGS = ();
16              
17             coloralias('error', 'bright_red');
18             coloralias('info', 'bright_blue');
19             coloralias('input', 'bright_cyan');
20             coloralias('ok', 'bright_green');
21             coloralias('warn', 'bright_magenta');
22              
23             # functions
24              
25             {
26 3     3   36 no strict 'refs'; ## no critic
  3         4  
  3         1621  
27             for my $context (qw|error info input ok warn|) {
28              
29             # color
30             my $fn = "color_$context";
31             push $EXPORT_TAGS{color}->@*, $fn;
32              
33 10     10   33363 *{__PACKAGE__ . "::$fn"} = sub { return colored [$context], @_ };
34              
35             # print
36             $fn = "print_$context";
37             push $EXPORT_TAGS{print}->@*, $fn;
38              
39 10     10   12621 *{__PACKAGE__ . "::$fn"} = sub { print colored [$context], @_ };
40              
41             # prompt
42             $fn = "prompt_$context";
43             push $EXPORT_TAGS{prompt}->@*, $fn;
44              
45             *{__PACKAGE__ . "::$fn"} = sub {
46 5     5   5295 my $style = coloralias($context) =~ s/bright_/bold /r;
47 5         70 return prompt shift, -v, -echostyle => $style, -style => $style, @_;
48             };
49              
50             # password
51             $fn = "password_$context";
52             push $EXPORT_TAGS{prompt}->@*, $fn;
53              
54             *{__PACKAGE__ . "::$fn"} = sub {
55 5     5   7250 my $style = coloralias($context) =~ s/bright_/bold /r;
56 5         63 return prompt shift, -v, -echo => '*', -echostyle => $style, -style => $style, @_;
57             };
58              
59             # say
60             $fn = "say_$context";
61             push $EXPORT_TAGS{say}->@*, $fn;
62              
63 10     10   13839 *{__PACKAGE__ . "::$fn"} = sub { say colored [$context], @_ };
64             }
65              
66             $EXPORT_TAGS{all}->@* = @EXPORT_OK = map { $EXPORT_TAGS{$_}->@* } qw|color print prompt say|;
67             }
68              
69             1;
70              
71             =encoding utf8
72              
73             =head1 NAME
74              
75             Print::Colored - print, say, prompt with predefined colors
76              
77             =head1 SYNOPSIS
78              
79             use Print::Colored;
80             use Print::Colored ':all';
81              
82             # color
83             use Print::Colored ':color';
84              
85             $colored_text = color_error $text; # bright red
86             $colored_text = color_info $text; # bright blue
87             $colored_text = color_input $text; # bright cyan
88             $colored_text = color_ok $text; # bright green
89             $colored_text = color_warn $text; # bright magenta
90              
91             # print
92             use Print::Colored ':print';
93              
94             print_error $text;
95             print_info $text;
96             print_input $text;
97             print_ok $text;
98             print_warn $text;
99              
100             # prompt
101             use Print::Colored ':prompt';
102              
103             $input = prompt_error $text, @params;
104             $input = prompt_info $text, @params;
105             $input = prompt_input $text, @params;
106             $input = prompt_ok $text, @params;
107             $input = prompt_warn $text, @params;
108              
109             $password = password_error $text, @params;
110             $password = password_info $text, @params;
111             $password = password_input $text, @params;
112             $password = password_ok $text, @params;
113             $password = password_warn $text, @params;
114              
115             # say
116             use Print::Colored ':say';
117              
118             say_error $text;
119             say_info $text;
120             say_input $text;
121             say_ok $text;
122             say_warn $text;
123              
124             =head1 DESCRIPTION
125              
126             L provides functions to print, say, prompt with predefined colors.
127              
128             =over
129              
130             =item C bright red
131              
132             =item C bright blue
133              
134             =item C bright cyan
135              
136             =item C bright green
137              
138             =item C bright magenta
139              
140             =back
141              
142             We should use colors all the time we write sripts that run in the terminal.
143             Read L
144             by L to get some more ideas about it.
145              
146             But experience shows that the more commands and constants we have to use the less colors our
147             scripts have. This was the reason to build this rather simple module.
148              
149             =head2 Limitations
150              
151             Because the colors are predefined, there isn't much to configure. If you don't like them (and quite
152             sure you don't) and until we come up with a better solution, you can use L
153             to modify them.
154              
155             use Term::ANSIColor 'coloralias';
156              
157             coloralias('error', 'yellow'); # default: bright_red
158             coloralias('info', 'white'); # default: bright_blue
159             coloralias('input', 'bright_white'); # default: bright_cyan
160             coloralias('ok', 'black'); # default: bright_green
161             coloralias('warn', 'red'); # default: bright_blue
162              
163             All the commands except L write directly to C.
164              
165             print_ok $filehandle 'Everything okay.'; # ✗ no
166             say_ok $filehandle 'Everything okay.'; # ✗ no
167              
168             You can't L and L to filehandles.
169              
170             print $filehandle color_ok 'Everything okay.'; # ✓
171             say $filehandle color_ok 'Everything okay.'; # ✓
172              
173             Instead you have to use one of the L functions.
174              
175             =head1 color
176              
177             use Print::Colored ':color';
178              
179             Imports the functions L, L, L, L, and L.
180              
181             =head2 color_error
182              
183             $colored_text = color_error 'There was an error';
184              
185             Returns a text colored as C.
186              
187             =head2 color_info
188              
189             $colored_text = color_info 'This is an info';
190              
191             Returns a text colored as C.
192              
193             =head2 color_input
194              
195             $colored_text = color_input 'Waiting for an input...';
196              
197             Returns a text colored as C.
198              
199             =head2 color_ok
200              
201             $colored_text = color_ok 'Everything okay';
202              
203             Returns a text colored as C.
204              
205             =head2 color_warn
206              
207             $colored_text = color_warn 'Last warning';
208              
209             Returns a text colored as C.
210              
211             =head1 print
212              
213             use Print::Colored ':print';
214              
215             Imports the functions L, L, L, L, and L.
216              
217             =head2 print_error
218              
219             print_error 'There was an error';
220              
221             Prints a text colored as C.
222              
223             =head2 print_info
224              
225             print_info 'This is an info';
226              
227             Prints a text colored as C.
228              
229             =head2 print_input
230              
231             print_input 'Waiting for an input...';
232              
233             Prints a text colored as C.
234              
235             =head2 print_ok
236              
237             print_ok 'Everything okay';
238              
239             Prints a text colored as C.
240              
241             =head2 print_warn
242              
243             print_warn 'Last warning';
244              
245             Prints a text colored as C.
246              
247             =head1 prompt
248              
249             use Print::Colored ':prompt';
250              
251             Imports the functions L, L, L, L, L,
252             L, L, L, L, and L.
253             Internally they call L.
254              
255             $password = prompt_input 'Enter your password: ', -echo => '*';
256             $password = password_input 'Enter your password: ';
257              
258             C functions ask for a password and are identical to C with parameter C<<-echo => '*'>>.
259              
260             =head2 prompt_error
261              
262             $input = prompt_error 'Enter your data: ';
263              
264             Prompts colored as C and returns the input.
265              
266             =head2 prompt_info
267              
268             $input = prompt_info 'Enter your data: ';
269              
270             Prompts colored as C and returns the input.
271              
272             =head2 prompt_input
273              
274             $input = prompt_input 'Enter your data: ';
275              
276             Prompts colored as C and returns the input.
277              
278             =head2 prompt_ok
279              
280             $input = prompt_ok 'Enter your data: ';
281              
282             Prompts colored as C and returns the input.
283              
284             =head2 prompt_warn
285              
286             $input = prompt_warn 'Enter your data: ';
287              
288             Prompts colored as C and returns the input.
289              
290             =head2 password_error
291              
292             $password = password_error 'Enter your password: ';
293              
294             Prompts colored as C for a password and returns the input.
295              
296             =head2 password_info
297              
298             $password = password_info 'Enter your password: ';
299              
300             Prompts colored as C for a password and returns the input.
301              
302             =head2 password_input
303              
304             $password = password_input 'Enter your password: ';
305              
306             Prompts colored as C for a password and returns the input.
307              
308             =head2 password_ok
309              
310             $password = password_ok 'Enter your password: ';
311              
312             Prompts colored as C for a password and returns the input.
313              
314             =head2 password_warn
315              
316             $password = password_warn 'Enter your password: ';
317              
318             Prompts colored as C for a password and returns the input.
319              
320             =head1 say
321              
322             use Print::Colored ':say';
323              
324             Imports the functions L, L, L, L, and L.
325              
326             =head2 say_error
327              
328             say_error 'There was an error';
329              
330             Prints a text with appended newline colored as C.
331              
332             =head2 say_info
333              
334             say_info 'This is an info';
335              
336             Prints a text with appended newline colored as C.
337              
338             =head2 say_input
339              
340             say_input 'Waiting for an input...';
341              
342             Prints a text with appended newline colored as C.
343              
344             =head2 say_ok
345              
346             say_ok 'Everything okay';
347              
348             Prints a text with appended newline colored as C.
349              
350             =head2 say_warn
351              
352             say_warn 'Last warning';
353              
354             Prints a text with appended newline colored as C.
355              
356             =head1 AUTHOR & COPYRIGHT
357              
358             © 2019-2022 by Tekki (Rolf Stöckli).
359              
360             This program is free software, you can redistribute it and/or modify it under the terms of the Artistic License version 2.0.
361              
362             =head1 SEE ALSO
363              
364             L, L.
365              
366             =cut