blib/lib/DB/Color.pm

filter: Statementstmt 74.7% 74 / 99 Branchbran 21.4% 6 / 28 Conditioncond 21.0% 4 / 19 MC/DCmcdc 0.0% 0 / 11 Subroutinesub 91.6% 22 / 24 Podpod 0.0% 0 / 3 totaltotal 57.6% 106 / 184 SCAR 36.7
CC
17
Cov
58%
__ANON__
42.4
__ANON__
15.5
afterinit
6.0
CRAP 39.1
« blib/lib/DB/Color/Config.pm ↑ Summary blib/lib/DB/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.10
21  
22 =cut
23  
24 1 our $VERSION = '0.10';
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 CONFIGURATION
72  
73 You can optionally configure C<DB::Color> by creating a
74 F<$HOME/.perldbcolorrc> configuration file. It looks like this:
75  
76 [core]
77
78 # the class that will highlight the code
79 highlighter = DB::Color::Highlight
80
81 # Any cache file not accessed after this number of days is purged
82 cache_max_age = 30
83
84 # where to put the cache dir
85 cache_dir = /users/ovid/.perldbcolor
86
87 The above values are more or less the defaults for this module. They are all
88 optional.
89  
90 =head1 ALPHA
91  
92 This is only a proof of concept. In fact, it's fair to say that this code
93 sucks. It's not very configurable and has bugs. It's also going to possibly be
94 a memory hog, as if the debugger wasn't bad enough already.
95  
96 =cut
97  
98 1 my $config = DB::Color::Config->read( default_rcfile() );
99  
100 1 my %COLORED;
101 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
102  
103 1 my $DB_LOG = catfile( $DB_BASE_DIR, 'debug.log' );
104 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
105 1 my $DEBUG;
106  
107 # Not documenting this because I don't guarantee stability, but you can play
108 # with it if you want.
109 1 ▶if ( $ENV{DB_COLOR_DEBUG} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"DB_COLOR_DEBUG"}) 0 1 1
110 0 ▶ open $DEBUG, '>>', $DB_LOG
Branch: 1 branch
BranchTrueFalseTotal
open $DEBUG, ">>", $DB_LOG or die "Cannot open $DB_LOG for appending: $!" 0 0 0
111 or die "Cannot open $DB_LOG for appending: $!";
112 0 $DEBUG->autoflush(1);
113 }
114  
115 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
116 1 ▶eval "use $HIGHLIGHTER_CLASS";
Subroutine BEGIN: called CC=1 SCAR=0.0
117 1 ▶die $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 1 1
118  
119 1 my $HIGHLIGHTER = $HIGHLIGHTER_CLASS->new(
120 {
121 cache_dir => $DB_BASE_DIR,
122 debug_fh => $DEBUG,
123 }
124 );
125  
126 sub DB::afterinit {
127 1 ▶ no warnings 'once';
Subroutine BEGIN: called CC=1 SCAR=0.0
128 0 ▶ push @DB::typeahead => "{{v"
Subroutine afterinit: not called CC=2 SCAR=17.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
unless $DB::already_curly_curly_v++ 0 0 0
129 unless $DB::already_curly_curly_v++;
130 }
131  
132 1 ▶sub default_rcfile { catfile( $ENV{HOME}, '.perldbcolorrc' ) }
Subroutine default_rcfile: called CC=1 SCAR=0.0
Pod undocumented
133 1 ▶sub default_base_dir { catfile( $ENV{HOME}, '.perldbcolor' ) }
Subroutine default_base_dir: called CC=1 SCAR=0.0
Pod undocumented
134  
135 sub import {
136 1 ▶ return if $ENV{NO_DB_COLOR};
Subroutine import: called CC=3 SCAR=16.4
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"NO_DB_COLOR"} 0 1 1
137 1 ▶ if ( 'MSWin32' eq $^O ) {
Branch: 1 branch
BranchTrueFalseTotal
if ("MSWin32" eq $^O) 0 1 1
138 0 warn <<"END";
139 DB::Color does not run under Windows because the Windows terminal is too
140 broken to understand terminal color code.
141  
142 DB::Color does not use Win32::Console because the debugger is too broken to be
143 properly extensible.
144 END
145 0 return;
146 }
147 1 my $old_db = \&DB::DB;
148  
149 my $new_DB = sub {
150 0 ▶ my $lvl = 0;
Subroutine __ANON__: not called CC=9 SCAR=37.5
151 0 ▶ while ( my ($pkg) = caller( $lvl++ ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my($pkg) = caller $lvl++) 0 0 0
152 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
153 }
154 0 my ( $package, $filename ) = caller;
155 0 ▶ if ($DEBUG) {
Branch: 1 branch
BranchTrueFalseTotal
if ($DEBUG) 0 0 0
156 0 print $DEBUG "In package '$package', filename '$filename'\n";
157 }
158  
159 # syntax highlight everything and cache it
160 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
161 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
162 1 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
163 [
164 split /(?<=\n)/ =>
165 0 $HIGHLIGHTER->highlight_text( join "" => @{"::_<$filename"} )
166 ];
167 };
168  
169 {
170  
171 # lie to the debugger about what the lines of code are
172 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.2
173 0 my $line_num = 0;
174 0 foreach ( @{"::_<$filename"} ) {
175  
176 # uncomment these to blow your f'in mind
177 #if ( not defined ) {
178 # use Devel::Peek;
179 # warn "line number is $line_num";
180 # Dump($_);
181 #}
182 # The debugger special cases the first value in ::_<$filename.
183 # It's "undef" but sometimes contains some data about the
184 # program. I don't know entirely what it is, but this solves
185 # the "off by one" bug.
186 0 ▶ next unless defined; # thanks Liz! (why does this work?)
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_ 0 0 0
187 0 my $line = $lines->[ $line_num++ ];
188 0 ▶ next unless defined $line; # happens when $_ = "\n"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $line 0 0 0
189  
190 # XXX Cheap hack to fix
191 # Argument "{\n" isn't numeric in addition (+) at DB/Color.pm line 189.
192 1 ▶ no warnings 'numeric';
Subroutine BEGIN: called CC=1 SCAR=0.0
193 0 my $numeric_value = 0 + $_;
194  
195 # Internally, the debugger uses dualvars for each line of
196 # code. If it's numeric value is 0, then the line is not
197 # breakable. If we don't include this, no lines in the
198 # debugger are breakable.
199 0 $_ = dualvar $numeric_value, $line;
200 }
201 }
202 0 goto $old_db;
203 1 };
204  
205 {
206 1 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
207 1 *DB::DB = $new_DB;
208 }
209  
210 1 return;
211 }
212  
213 END {
214 find(
215 sub {
216  
217 # delete empty files or files > $CACHE_MAX_AGE days old
218 1 ▶ if ( -f $_ && ( -z _ || -M _ > $CACHE_MAX_AGE ) ) {
Subroutine __ANON__: called CC=5 SCAR=27.4
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
219 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
220 }
221 },
222 1 ▶ $DB_BASE_DIR,
Subroutine END: called CC=1 SCAR=0.1
223 );
224 # we're not testing for failure as this is a cheap hack to delete empty
225 # directories
226 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
227 }
228  
229 1 1;
230  
231 =head1 AUTHOR
232  
233 Curtis "Ovid" Poe, C<< <ovid at cpan.org> >>
234  
235 =head1 BUGS
236  
237 Please report any bugs or feature requests to C<bug-db-color at rt.cpan.org>,
238 or through the web interface at
239 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=DB-Color>. I will be
240 notified, and then you'll automatically be notified of progress on your bug as
241 I make changes.
242  
243 =head1 SUPPORT
244  
245 You can find documentation for this module with the perldoc command.
246  
247 perldoc DB::Color
248  
249 You can also look for information at:
250  
251 =over 4
252  
253 =item * RT: CPAN's request tracker (report bugs here)
254  
255 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=DB-Color>
256  
257 =item * AnnoCPAN: Annotated CPAN documentation
258  
259 L<http://annocpan.org/dist/DB-Color>
260  
261 =item * CPAN Ratings
262  
263 L<http://cpanratings.perl.org/d/DB-Color>
264  
265 =item * Search CPAN
266  
267 L<http://search.cpan.org/dist/DB-Color/>
268  
269 =back
270  
271 =head1 ACKNOWLEDGEMENTS
272  
273 Thanks to Nick Perez, Liz, and the 2012 Perl Hackathon for helping to overcome
274 some major hurdles with this module.
275  
276 =head1 LICENSE AND COPYRIGHT
277  
278 Copyright 2011 Curtis "Ovid" Poe.
279  
280 This program is free software; you can redistribute it and/or modify it
281 under the terms of either: the GNU General Public License as published
282 by the Free Software Foundation; or the Artistic License.
283  
284 See http://dev.perl.org/licenses/ for more information.
285  
286  
287 =cut
288  
289 1 1; # End of DB::Color
« blib/lib/DB/Color/Config.pm ↑ Summary blib/lib/DB/Color/Highlight.pm »