Color.pm

filter: Statementstmt 74.0% 77 / 104 Branchbran 23.3% 7 / 30 Conditioncond 22.7% 5 / 22 MC/DCmcdc 0.0% 0 / 13 Subroutinesub 91.6% 22 / 24 Podpod 0.0% 0 / 3 totaltotal 56.6% 111 / 196 CC 19 SCAR 25.5
CC
19
Cov
57%
__ANON__
42.4
__ANON__
15.5
import
10.5
CRAP 12.9
« Color/Config.pm ↑ Summary Color/Highlight.pm »
1 package DB::Color;
2  
3 1 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use DB::Color::Highlight;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use DB::Color::Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 ▶use IO::Handle;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use File::Spec::Functions qw(catfile catdir);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Scalar::Util 'dualvar';
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use File::Find;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 =head1 NAME
15  
16 DB::Color - Colorize your debugger output
17  
18 =head1 VERSION
19  
20 Version 0.20
21  
22 =cut
23  
24 1 our $VERSION = '0.20';
25  
26 =head1 SYNOPSIS
27  
28 Put the following in your F<$HOME/.perldb> file:
29  
30 use DB::Color;
31  
32 Then use your debugger like normal:
33  
34 perl -d some_file.pl
35  
36 If you don't want a F<$HOME/.perldb> file, you can do this:
37  
38 perl -MDB::Color -d some_file.pl
39  
40 =head1 DISABLING COLOR
41  
42 If the NO_DB_COLOR environment variable is set to a true value, syntax
43 highlighting will be disabled.
44  
45 =head1 WINDOWS
46  
47 No, sorry. It's a combination of bad Windows support for ANSI escape sequences
48 and bad debugger design.
49  
50 =head1 PERFORMANCE
51  
52 When using the debugger and when you step into something, or continue to a
53 breakpoint in a new file, the debugger may appear to hang for a moment
54 (perhaps a long moment if the file is big) while the file is syntax
55 highlighted and cached. The next time the debugger enters this file, the
56 highlighting should be instantaneous.
57  
58 You can speed up the debugger by using the L<perldbsyntax> program which is
59 included in this distribution. It will pregenerate syntax files for you.
60  
61 Syntax highlighting the code is very slow. As a result, we cache the output
62 files in F<$HOME/.perldbcolor>. This is done by calculating the md5 sum of the
63 file contents. If the file is changed, we get a new sum. This means that
64 syntax highlighting is very slow at first, but every time you hit the same
65 file, assuming its unchanged, the cached version is served first.
66  
67 Note that the cache files are removed after they become 30 (but see config)
68 days old without being used. If you use the debugger regularly, commonly
69 debugged files will load very quickly (assuming they haven't changed).
70  
71 =head1 CONDITIONAL LOADING
72  
73 If you prefer, you may only want to have I<some> of your projects "colorized".
74 If so, you can do something like this:
75  
76 use DB::Color sentinel => '.colorize';
77  
78 If an if the C<.colorize> sentinel (or whatever you named it) does not exist,
79 C<DB::Color> will not be used.
80  
81 =head1 WORKFLOW
82  
83 To use C<DB::Color> effectively, I recommend the following:
84  
85 $ cpanm DB::Color
86 $ echo "use DB::Color sentinel => '.colorize'" >> ~/.perldb
87 # cd to project you want to colorize and create the sentinel
88 $ touch .colorize
89 # colorize the project. This will likely take a long time
90 $ PERL5LIB=lib:t/tests perldbsyntax
91  
92 At that point, you're almost good to go. However, as you're rapidly changing
93 files, the debugger will still probably be very slow. Instead, create a
94 watcher to watch your project directories and rehighlight any files which have
95 been created or modified. An example of a watcher program is the
96 F<examples/colorize> program included with this distribution.
97  
98 =head1 CONFIGURATION
99  
100 You can optionally configure C<DB::Color> by creating a
101 F<$HOME/.perldbcolorrc> configuration file. It looks like this:
102  
103 [core]
104
105 # the class that will highlight the code
106 highlighter = DB::Color::Highlight
107
108 # Any cache file not accessed after this number of days is purged
109 cache_max_age = 30
110
111 # where to put the cache dir
112 cache_dir = /users/ovid/.perldbcolor
113
114 The above values are more or less the defaults for this module. They are all
115 optional.
116  
117 =head1 ALPHA
118  
119 This is only a proof of concept. In fact, it's fair to say that this code
120 sucks. It's not very configurable and has bugs. It's also going to possibly be
121 a memory hog, as if the debugger wasn't bad enough already.
122  
123 =cut
124  
125 1 my $config = DB::Color::Config->read( default_rcfile() );
126  
127 1 my %COLORED;
128 1 ▶my $DB_BASE_DIR = $config->{core}{cache_dir} || default_base_dir();
Condition33%1/3
$config->{'core'}{'cache_dir'} || default_base_dir()
l!l&&r!l&&!r
010
MC/DC0%0/2
$config->{'core'}{'cache_dir'} || default_base_dir()
$config->{'core'}{'cache_dir'} default_base_dir()
Truth table33%1/3
A || B
A= $config->{'core'}{'cache_dir'}B= default_base_dir()
ABresult
1X1
011
000
129  
130 1 my $DB_LOG = catfile( $DB_BASE_DIR, 'debug.log' );
131 1 ▶my $CACHE_MAX_AGE = $config->{core}{cache_max_age} || 30;
Condition50%1/2
$config->{'core'}{'cache_max_age'} || 30
l!l
01
MC/DC0%0/1
$config->{'core'}{'cache_max_age'} || 30
$config->{'core'}{'cache_max_age'}
Truth table50%1/2
A
A= $config->{'core'}{'cache_max_age'}
Aresult
11
00
132 1 my $DEBUG;
133  
134 # Not documenting this because I don't guarantee stability, but you can play
135 # with it if you want.
136 1 ▶if ( $ENV{DB_COLOR_DEBUG} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"DB_COLOR_DEBUG"}) 0 1 1
137 0 ▶ open $DEBUG, '>>', $DB_LOG
Branch: 1 branch
BranchTrueFalseTotal
open $DEBUG, ">>", $DB_LOG or die "Cannot open $DB_LOG for appending: $!" 0 0 0
138 or die "Cannot open $DB_LOG for appending: $!";
139 0 $DEBUG->autoflush(1);
140 }
141  
142 1 ▶my $HIGHLIGHTER_CLASS = $config->{core}{highlighter} || 'DB::Color::Highlight';
Condition50%1/2
$config->{'core'}{'highlighter'} || 'DB::Color::Highlight'
l!l
01
MC/DC0%0/1
$config->{'core'}{'highlighter'} || 'DB::Color::Highlight'
$config->{'core'}{'highlighter'}
Truth table50%1/2
A
A= $config->{'core'}{'highlighter'}
Aresult
11
00
143 1 ▶eval "use $HIGHLIGHTER_CLASS";
Subroutine BEGIN: called CC=1 SCAR=0.0
144 1 ▶die $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 1 1
145  
146 1 my $HIGHLIGHTER = $HIGHLIGHTER_CLASS->new(
147 {
148 cache_dir => $DB_BASE_DIR,
149 debug_fh => $DEBUG,
150 }
151 );
152  
153 0 ▶sub DB::afterinit {
Subroutine afterinit: not called CC=2 SCAR=9.2
Pod undocumented
154 1 ▶ no warnings 'once';
Subroutine BEGIN: called CC=1 SCAR=0.0
155 0 ▶ push @DB::typeahead => "{{v"
Branch: 1 branch
BranchTrueFalseTotal
unless $DB::already_curly_curly_v++ 0 0 0
156 unless $DB::already_curly_curly_v++;
157 }
158  
159 1 ▶sub default_rcfile { catfile( $ENV{HOME}, '.perldbcolorrc' ) }
Subroutine default_rcfile: called CC=1 SCAR=0.0
Pod undocumented
160 1 ▶sub default_base_dir { catfile( $ENV{HOME}, '.perldbcolor' ) }
Subroutine default_base_dir: called CC=1 SCAR=0.0
Pod undocumented
161  
162 1 ▶sub import {
Subroutine import: called CC=5 SCAR=23.5
163 1 my ( $package, %arg_for ) = @_;
164 1 my $sentinel = $arg_for{sentinel};
165  
166 1 ▶ if ( defined $sentinel && !-e $sentinel ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $sentinel and not -e $sentinel) 0 1 1
Condition33%1/3
defined $sentinel and not -e $sentinel
!ll&&!rl&&r
100
MC/DC0%0/2
defined $sentinel and not -e $sentinel
defined $sentinel not -e $sentinel
Truth table33%1/3
A and not(B)
A= defined $sentinelB= not -e $sentinel
ABresult
0X0
100
111
167 0 warn "DB::Color not running because '$sentinel' was requested, but not found\n";
168 0 return;
169 }
170 1 ▶ return if $ENV{NO_DB_COLOR};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"NO_DB_COLOR"} 0 1 1
171 1 ▶ if ( 'MSWin32' eq $^O ) {
Branch: 1 branch
BranchTrueFalseTotal
if ("MSWin32" eq $^O) 0 1 1
172 0 warn <<"END";
173 DB::Color does not run under Windows because the Windows terminal is too
174 broken to understand terminal color code.
175  
176 DB::Color does not use Win32::Console because the debugger is too broken to be
177 properly extensible.
178 END
179 0 return;
180 }
181 1 my $old_db = \&DB::DB;
182  
183 0 ▶ my $new_DB = sub {
Subroutine __ANON__: not called CC=9 SCAR=37.5
184 0 my $lvl = 0;
185 0 ▶ while ( my ($pkg) = caller( $lvl++ ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my($pkg) = caller $lvl++) 0 0 0
186 0 ▶ return if $pkg eq "DB" or $pkg =~ /^DB::/;
Branch: 1 branch
BranchTrueFalseTotal
if $pkg eq "DB" or $pkg =~ /^DB::/ 0 0 0
Condition0%0/3
$pkg eq "DB" or $pkg =~ /^DB::/
l!l&&r!l&&!r
000
MC/DC0%0/2
$pkg eq "DB" or $pkg =~ /^DB::/
$pkg eq "DB" $pkg =~ /^DB::/
Truth table0%0/3
A or B
A= $pkg eq "DB"B= $pkg =~ /^DB::/
ABresult
1X1
011
000
187 }
188 0 my ( $package, $filename ) = caller;
189 0 ▶ if ($DEBUG) {
Branch: 1 branch
BranchTrueFalseTotal
if ($DEBUG) 0 0 0
190 0 print $DEBUG "In package '$package', filename '$filename'\n";
191 }
192  
193 # syntax highlight everything and cache it
194 0 ▶ my $lines = $COLORED{$filename} ||= do {
Condition0%0/3
$COLORED{$filename} ||= do { BEGIN {${^WARNING_BITS} = "\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x51\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55"} no strict 'refs'; [split(/(?<=\n)/, $HIGHLIGHTER->highlight_text(join("", @{"::_<$filename";})), 0)] }
l!l&&r!l&&!r
000
MC/DC0%0/2
$COLORED{$filename} ||= do { BEGIN {${^WARNING_BITS} = "\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x51\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55"} no strict 'refs'; [split(/(?<=\n)/, $HIGHLIGHTER->highlight_text(join("", @{"::_<$filename";})), 0)] }
$COLORED{$filename} do { BEGIN {${^WARNING_BITS} = "\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x51\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55"} no strict 'refs'; [split(/(?<=\n)/, $HIGHLIGHTER->highlight_text(join("", @{"::_<$filename";})), 0)] }
Truth table0%0/3
A ||= B
A= $COLORED{$filename}B= do { BEGIN {${^WARNING_BITS} = "\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x51\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55"} no strict 'refs'; [split(/(?<=\n)/, $HIGHLIGHTER->highlight_text(join("", @{"::_<$filename";})), 0)] }
ABresult
1X1
011
000
195 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
196 1 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
197 [
198 split /(?<=\n)/ =>
199 0 $HIGHLIGHTER->highlight_text( join "" => @{"::_<$filename"} )
200 ];
201 };
202  
203 {
204  
205 # lie to the debugger about what the lines of code are
206 0 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.2
207 0 my $line_num = 0;
208 0 foreach ( @{"::_<$filename"} ) {
209  
210 # uncomment these to blow your f'in mind
211 #if ( not defined ) {
212 # use Devel::Peek;
213 # warn "line number is $line_num";
214 # Dump($_);
215 #}
216 # The debugger special cases the first value in ::_<$filename.
217 # It's "undef" but sometimes contains some data about the
218 # program. I don't know entirely what it is, but this solves
219 # the "off by one" bug.
220 0 ▶ next unless defined; # thanks Liz! (why does this work?)
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_ 0 0 0
221 0 my $line = $lines->[ $line_num++ ];
222 0 ▶ next unless defined $line; # happens when $_ = "\n"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $line 0 0 0
223  
224 # XXX Cheap hack to fix
225 # Argument "{\n" isn't numeric in addition (+) at DB/Color.pm line 189.
226 1 ▶ no warnings 'numeric';
Subroutine BEGIN: called CC=1 SCAR=0.0
227 0 my $numeric_value = 0 + $_;
228  
229 # Internally, the debugger uses dualvars for each line of
230 # code. If it's numeric value is 0, then the line is not
231 # breakable. If we don't include this, no lines in the
232 # debugger are breakable.
233 0 $_ = dualvar $numeric_value, $line;
234 }
235 }
236 0 goto $old_db;
237 1 };
238  
239 {
240 1 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
241 1 *DB::DB = $new_DB;
242 }
243  
244 1 return;
245 }
246  
247 1 ▶END {
Subroutine END: called CC=1 SCAR=1.9
248 find(
249 1 ▶ sub {
Subroutine __ANON__: called CC=5 SCAR=27.4
250  
251 # delete empty files or files > $CACHE_MAX_AGE days old
252 1 ▶ if ( -f $_ && ( -z _ || -M _ > $CACHE_MAX_AGE ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $_ and -z _ || -M _ > $CACHE_MAX_AGE) 0 1 1
Condition16%1/6
-f $_ and -z _ || -M _ > $CACHE_MAX_AGE
!ll&&!rl&&r
100
-z _ or -M _ > $CACHE_MAX_AGE
l!l&&r!l&&!r
000
MC/DC0%0/3
-f $_ and -z _ || -M _ > $CACHE_MAX_AGE
-f $_ -z _ -M _ > $CACHE_MAX_AGE
Truth table25%1/4
A and B or C
A= -f $_B= -z _C= -M _ > $CACHE_MAX_AGE
ABCresult
0XX0
1000
11X1
1011
253 0 ▶ unlink($_) or die "Could not unlink '$File::Find::name': $!";
Branch: 1 branch
BranchTrueFalseTotal
unlink $_ or die "Could not unlink '$File::Find::name': $!" 0 0 0
254 }
255 },
256 1 $DB_BASE_DIR,
257 );
258 # we're not testing for failure as this is a cheap hack to delete empty
259 # directories
260 1 ▶ finddepth( sub { rmdir $_ if -d }, $DB_BASE_DIR );
Subroutine __ANON__: called CC=2 SCAR=7.2
Branch: 1 branch
BranchTrueFalseTotal
if -d $_ 1 0 1
261 }
262  
263 1 1;
264  
265 =head1 AUTHOR
266  
267 Curtis "Ovid" Poe, C<< <ovid at cpan.org> >>
268  
269 =head1 BUGS
270  
271 Please report any bugs or feature requests through the web interface at
272 L<https://github.com/Ovid/DB--Color/issues>. I will be notified, and then
273 you'll automatically be notified of progress on your bug as I make changes.
274  
275 =head1 SUPPORT
276  
277 You can find documentation for this module with the perldoc command.
278  
279 perldoc DB::Color
280  
281 You can also look for information at:
282  
283 =over 4
284  
285 =item * Bug tracker (report bugs here)
286  
287 L<https://github.com/Ovid/DB--Color/issues>
288  
289 =item * AnnoCPAN: Annotated CPAN documentation
290  
291 L<http://annocpan.org/dist/DB-Color>
292  
293 =item * CPAN Ratings
294  
295 L<http://cpanratings.perl.org/d/DB-Color>
296  
297 =item * Search CPAN
298  
299 L<http://search.cpan.org/dist/DB-Color/>
300  
301 =item * Github
302  
303 L<http://github.com/Ovid/DB--Color>
304  
305 =back
306  
307 =head1 ACKNOWLEDGEMENTS
308  
309 Thanks to Nick Perez, Liz, and the 2012 Perl Hackathon for helping to overcome
310 some major hurdles with this module.
311  
312 =head1 LICENSE AND COPYRIGHT
313  
314 Copyright 2011 Curtis "Ovid" Poe.
315  
316 This program is free software; you can redistribute it and/or modify it
317 under the terms of either: the GNU General Public License as published
318 by the Free Software Foundation; or the Artistic License.
319  
320 See http://dev.perl.org/licenses/ for more information.
321  
322  
323 =cut
324  
325 1 1; # End of DB::Color
« Color/Config.pm ↑ Summary Color/Highlight.pm »