Skip to content

Commit

Permalink
support for core booleans
Browse files Browse the repository at this point in the history
  • Loading branch information
garu committed Jan 17, 2024
1 parent 306dbef commit d490da5
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 4 deletions.
2 changes: 2 additions & 0 deletions examples/try_me.pl
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,14 @@ package main;
baz => 789,
},
readonly => \2,
boolean => [1 == 1, 1 == 2],
regexp => qr/foo.*bar/i,
glob => \*STDOUT,
code => sub { return 42 },
class => $obj,
};


$sample->{weakref} = $sample;
weaken $sample->{weakref};

Expand Down
15 changes: 15 additions & 0 deletions lib/Data/Printer/Filter/SCALAR.pm
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ use warnings;
use Data::Printer::Filter;
use Scalar::Util;

my $HAS_BOOLEAN = $] ge '5.036000' && eval 'use builtin; 1';

filter 'SCALAR' => \&parse;
filter 'LVALUE' => sub {
my ($scalar_ref, $ddp) = @_;
Expand All @@ -23,6 +25,13 @@ sub parse {
if (not defined $value) {
$ret = $ddp->maybe_colorize('undef', 'undef');
}
elsif ($HAS_BOOLEAN && _is_bool($value)) {
if ($value) {
$ret = $ddp->maybe_colorize('true', 'true');
} else {
$ret = $ddp->maybe_colorize('false', 'false');
}
}
elsif ( $ddp->show_dualvar ne 'off' ) {
my $numified;
$numified = do { no warnings 'numeric'; 0+ $value } if defined $value;
Expand Down Expand Up @@ -122,4 +131,10 @@ sub _is_number {
return $is_number;
}

sub _is_bool {
no warnings 'experimental::builtin';
return builtin::is_bool($_[0]);
}


1;
2 changes: 2 additions & 0 deletions lib/Data/Printer/Theme.pm
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,8 @@ Data::Printer::Theme - create your own color themes for DDP!
vstring => '#aabbcc', # version strings (v5.30.1, etc)
lvalue => '#aabbcc', # lvalue label
format => '#aabbcc', # format type
true => '#aabbcc', # boolean type (true)
false => '#aabbcc', # boolean type (false)
repeated => '#aabbcc', # references to seen values
caller_info => '#aabbcc', # details on what's being printed
weak => '#aabbcc', # weak references flag
Expand Down
2 changes: 2 additions & 0 deletions lib/Data/Printer/Theme/Classic.pm
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ sub colors {
vstring => 'bright_blue', # version strings (v5.16.0, etc)
lvalue => '', # lvalue label
format => '', # format type
true => 'bright_cyan', # boolean type (true)
false => 'bright_cyan', # boolean type (false)
repeated => 'white on_red', # references to seen values
caller_info => 'bright_cyan', # details on what's being printed
weak => 'cyan', # weak references
Expand Down
4 changes: 3 additions & 1 deletion lib/Data/Printer/Theme/Material.pm
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package Data::Printer::Theme::Material;
# inspired by Mattia Astorino's Material theme:
# https://github.com/equinusocio/vsc-material-theme
# https://github.com/material-theme/vsc-material-theme
use strict;
use warnings;

Expand Down Expand Up @@ -37,6 +37,8 @@ sub colors {
vstring => $code_for{strong_orange}, # version strings (v5.16.0, etc)
lvalue => $code_for{strong_orange}, # lvalue label
format => $code_for{strong_orange}, # format type
true => $code_for{blue}, # boolean type (true)
false => $code_for{blue}, # boolean type (false)
repeated => $code_for{red}, # references to seen values
caller_info => $code_for{gray}, # details on what's being printed
weak => $code_for{green}, # weak references flag
Expand Down
2 changes: 2 additions & 0 deletions lib/Data/Printer/Theme/Monokai.pm
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ sub colors {
vstring => $code_for{cyan}, # version strings (v5.16.0, etc)
lvalue => $code_for{green}, # lvalue label
format => $code_for{violet}, # format type
true => $code_for{violet}, # boolean type (true)
false => $code_for{violet}, # boolean type (false)
repeated => $code_for{pink}, # references to seen values
caller_info => $code_for{grey}, # details on what's being printed
weak => $code_for{green}, # weak references flag
Expand Down
6 changes: 4 additions & 2 deletions lib/Data/Printer/Theme/Solarized.pm
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,10 @@ sub colors {
vstring => $code_for{base1}, # version strings (v5.16.0, etc)
lvalue => $code_for{green}, # lvalue label
format => $code_for{green}, # format type
repeated => $code_for{red}, # references to seen values
caller_info => $code_for{cyan}, # details on what's being printed
true => $code_for{blue}, # boolean type (true)
false => $code_for{blue}, # boolean type (false)
repeated => $code_for{red}, # references to seen values
caller_info => $code_for{cyan}, # details on what's being printed
weak => $code_for{violet}, # weak references flag
tainted => $code_for{violet}, # tainted flag
unicode => $code_for{magenta}, # utf8 flag
Expand Down
20 changes: 19 additions & 1 deletion t/002-scalar.t
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@
# ^^ taint mode must be on for taint checking.
use strict;
use warnings;
use Test::More tests => 67;
use Test::More tests => 72;
use Data::Printer::Object;
use Scalar::Util;

test_basic_values();
test_boolean_values();
test_tainted_values();
test_unicode_string();
test_escape_chars();
Expand Down Expand Up @@ -57,6 +58,23 @@ sub test_basic_values {
is $object->parse(\$var), '123', 'integer 123 in variable';
}

sub test_boolean_values {
SKIP: {
skip 'booleans only exist after 5.36', 5 unless $] ge '5.036000';
my $object = Data::Printer::Object->new( colored => 0 );
my $var = 1 == 1;
is $object->parse(\$var), 'true', 'boolean true is "true"';
$var = 1 == 2;
is $object->parse(\$var), 'false', 'boolean false is "false"';
$var = 1;
is $object->parse(\$var), '1', '1 is 1, not "true"';
$var = '';
is $object->parse(\$var), '""', 'empty string is "", not "false"';
$var = 0;
is $object->parse(\$var), '0', '0 is 0, not "false"';
};
}

sub test_tainted_values {
SKIP: {
# only use 1 char substring to avoid leaking
Expand Down

0 comments on commit d490da5

Please sign in to comment.