File Coverage

blib/lib/Print/Colored.pm
Criterion Covered Total %
statement 28 28 100.0
branch n/a
condition n/a
subroutine 12 12 100.0
pod n/a
total 40 40 100.0


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