File Coverage

blib/lib/CMD/Colors.pm
Criterion Covered Total %
statement 6 19 31.5
branch 0 10 0.0
condition n/a
subroutine 2 4 50.0
pod 0 2 0.0
total 8 35 22.8


line stmt bran cond sub pod time code
1             package CMD::Colors;
2              
3             =head1 NAME
4              
5             CMD::Colors - Generate Colorfull text on commandline
6              
7             =head1 SYNOPSIS
8              
9             use CMD::Colors;
10              
11             ##### Example Usage #####
12            
13             ## Prints text with 'RED' color & default background
14             Cprint('hello, This is RED text', 'red');
15              
16             ## Prints text with 'RED' color & 'white' background
17             Cprint('hello, This is RED text', 'red', 'white');
18              
19             ## Prints text with 'RED' color & 'default' background & BOLD text
20             Cprint('hello, This is RED text', 'red', 'default', 'bold');
21              
22             ## Prints text with 'RED' color & 'default' background & 'half_bright' text
23             Cprint('hello, This is RED text', 'red', undef, 'half_bright');
24              
25              
26             ##### Show all available 'foreground' & 'background' colors - DEMO #####
27             foreach my $color (keys %{$COLOR_CODES{'foreground'}}) {
28             Cprint("This is $color text", $color);
29             print "\n";
30             foreach my $bgcolor(keys %{$COLOR_CODES{'background'}}) {
31             Cprint("This is $color text with $bgcolor background", $color, $bgcolor);
32             print "\n";
33             }
34             }
35              
36              
37             =head1 DESCRIPTION
38              
39             This module provides functions for generating colorfull text on commandline with perl
40             programs. It can be used to make PERL "CMD" programs more interesting.
41              
42             *Cprint() function be used for all "print" calls.
43              
44             Syntax -
45             Cprint("TEXT TO BE Printed", "ForegroundCOLORName", "BackgroundColorName", "TEXT Property");
46              
47              
48             Supported Colors ::
49             Foreground - black, red, green, brown, blue, magenta, cyan, white
50             Background - black, red, green, brown, blue, magenta, cyan, white
51              
52              
53             Supported Properties ::
54             'bold' ## Set bold
55             'half_bright' ## Set half-bright (simulated with color on a color display)
56             'underscore' ## Set underscore (simulated with color on a color display)
57             ## (the colors used to simulate dim or underline are set
58             'blink' ## Set blink
59             'reverse_video' ## Set reverse video
60             'reset_mapping' ## Reset selected mapping, display control flag, and toggle
61             ## meta flag (ECMA-48 says "primary font").
62             'null_mapping' ## Select null mapping, set display control flag, reset
63             ## toggle meta flag (ECMA-48 says "first alternate font").
64             'null-mapping ' ## Select null mapping, set display control flag, set toggle
65             ## meta flag (ECMA-48 says "second alternate font"). The
66             ## toggle meta flag causes the high bit of a byte to be
67             ## toggled before the mapping table translation is done.
68             'nd_intensity' ## Set normal intensity (ECMA-48 says "doubly underlined")
69             'n_intensity' ## Set normal intensity
70             'underline_off' ## Set underline off
71             'blink_off' ## Set blink off
72             'reverse_video_off' ## Set reverse video off
73             'default' ## Set default
74              
75              
76             ** If color/property specified is not supported, default color/property would be used for printing text **
77              
78             Techinal Details::
79             This module uses "Linux" console escape and control sequences for generating colorfull text
80             with background colors, It utilizes the "ECMA-48 SGR" sequenceof the SHELL to generate colored text.
81              
82              
83             =head1 AUTHOR
84             Utsav Handa
85            
86            
87             =head1 COPYRIGHT
88             (c) 2009 Utsav Handa.
89              
90            
91             All rights reserved. This program is free software; you can redistribute it
92             and/or modify it under the same terms as Perl itself.
93              
94            
95            
96             =cut
97              
98              
99 1     1   7139 use strict;
  1         3  
  1         39  
100 1     1   4 use Exporter;
  1         2  
  1         426  
101              
102             our $VERSION = '0.1';
103             our @ISA = qw/ Exporter /;
104             our @EXPORT = qw(Cprint %COLOR_CODES);
105              
106              
107              
108             #########################
109             #### Color Code Hash ####
110             #########################
111             our %COLOR_CODES = (
112             'foreground' => {
113             'black' => 30, ## Set black foreground
114             'red' => 31, ## Set red foreground
115             'green' => 32, ## Set green foreground
116             'brown' => 33, ## Set brown foreground
117             'blue' => 34, ## Set blue foreground
118             'magenta' => 35, ## Set magenta foreground
119             'cyan' => 36, ## Set cyan foreground
120             'white' => 37, ## Set white foreground
121             'default' => 49, ## Set default background color
122             },
123             'background' => {
124             'black' => 40, ## Set black background
125             'red' => 41, ## Set red background
126             'green' => 42, ## Set green background
127             'brown' => 43, ## Set brown background
128             'blue' => 44, ## Set blue background
129             'magenta' => 45, ## Set magenta background
130             'cyan' => 46, ## Set cyan background
131             'white' => 47, ## Set white background
132             'default' => 49, ## Set default background color
133             },
134             'other' => {
135             'bold' => ';1', ## Set bold
136             'half_bright' => ';2', ## Set half-bright (simulated with color on a color display)
137             'underscore' => ';4', ## Set underscore (simulated with color on a color display)
138             ## (the colors used to simulate dim or underline are set
139             'blink' => ';5', ## Set blink
140             'reverse_video' => ';7', ## Set reverse video
141             'reset_mapping' => ';10', ## Reset selected mapping, display control flag, and toggle
142             ## meta flag (ECMA-48 says "primary font").
143             'null_mapping' => ';11', ## Select null mapping, set display control flag, reset
144             ## toggle meta flag (ECMA-48 says "first alternate font").
145             'null-mapping ' => ';12', ## Select null mapping, set display control flag, set toggle
146             ## meta flag (ECMA-48 says "second alternate font"). The
147             ## toggle meta flag causes the high bit of a byte to be
148             ## toggled before the mapping table translation is done.
149             'nd_intensity' => ';21', ## Set normal intensity (ECMA-48 says "doubly underlined")
150             'n_intensity' => ';22', ## Set normal intensity
151             'underline_off' => ';24', ## Set underline off
152             'blink_off' => ';25', ## Set blink off
153             'reverse_video_off' => ';27', ## Set reverse video off
154             'default' => '', ## Set default
155             }
156              
157             );
158              
159              
160             sub Cprint {
161             ## This sub-routine actually makes call to 'print' statemtn with ESC characters
162             ## and prepares statemtn for printing specified text
163 0     0 0   my ($text, $foreground_color, $background_color, $other_color, $garb) = @_;
164              
165             ## Default Variable(s)
166 0 0         $foreground_color = 'default' if (!$foreground_color);
167 0 0         $background_color = 'default' if (!$background_color);
168 0 0         $other_color = ( $other_color ? getCodeForColor($other_color, 'other') : '' );
169              
170             ## Building string to print
171 0           my $string = "\033[";
172 0           $string .= getCodeForColor($foreground_color, 'foreground').';'.getCodeForColor($background_color, 'background');
173 0           $string .= $other_color."m".$text."\033[0m";
174              
175 0           return print $string;
176             }
177              
178              
179             sub getCodeForColor {
180             ## This sub-routine returns actualt ESC Code for property and color specified
181 0     0 0   my ($color, $type, $garb) = @_;
182              
183             ## Default Type
184 0 0         $type = 'foreground' if (!$type);
185              
186             ## Sanitize Arguments
187 0           $color = lc $color;
188 0           $type = lc $type;
189              
190              
191 0 0         return ( $COLOR_CODES{$type}{$color} ? $COLOR_CODES{$type}{$color} : $COLOR_CODES{$type}{'default'} );
192             }
193              
194              
195              
196              
197