1#! /usr/bin/perl
2
3use strict ;
4use warnings ;
5use Carp ;
6
7use Data::TreeDumper ;
8
9our $s ;
10do "s" ;
11
12$Data::TreeDumper::Useascii = 1 ;
13
14print DumpTree($s, 'Unaltered data structure') ;
15
16#-------------------------------------------------------------------------------
17# Tree Coloring example
18#-------------------------------------------------------------------------------
19
20use Term::ANSIColor qw(:constants) ;
21
22my @colors = map
23		{
24		Term::ANSIColor::color($_) ;
25		}
26		(
27		  'red'
28		, 'green'
29		, 'yellow'
30		, 'blue'
31		, 'magenta'
32		, 'cyan'
33		) ;
34
35#-------------------------------------------------------------------------------
36# level coloring
37#-------------------------------------------------------------------------------
38
39sub ColorLevels
40{
41my $level   = shift ;
42my $index   = $level % @colors ;
43
44return($colors[$index], '') ;
45}
46
47print Data::TreeDumper::DumpTree($s, "Level coloring using a sub", COLOR_LEVELS => \&ColorLevels, NUMBER_LEVELS => 2) ;
48
49print Term::ANSIColor::color('reset') ;
50
51sub ColorLevelsGlyphs
52{
53my $level   = shift ;
54my $index   = $level % @colors ;
55
56return($colors[$index], Term::ANSIColor::color('reset')) ;
57}
58
59print Data::TreeDumper::DumpTree($s, "Level glyph coloring using a sub", COLOR_LEVELS => \&ColorLevelsGlyphs) ;
60
61print Data::TreeDumper::DumpTree($s, "Level coloring using an array", COLOR_LEVELS => [\@colors, '']) ;
62print Term::ANSIColor::color('reset') ;
63
64print Data::TreeDumper::DumpTree($s, "Level glyph coloring using an array", COLOR_LEVELS => [\@colors, Term::ANSIColor::color('reset')]) ;
65
66#-------------------------------------------------------------------------------
67# label coloring
68#-------------------------------------------------------------------------------
69
70sub ColorLabel
71{
72my ($tree, $level, $path, $nodes_to_display, $setup) = @_ ;
73
74if('HASH' eq ref $tree)
75	{
76	my @keys_to_dump ;
77
78	for my $key_name (keys %$tree)
79		{
80		my $index = ord(substr($key_name, 0, 1)) % @colors ;
81		my $reset_color = $setup->{__ANSI_COLOR_RESET} || Term::ANSIColor::color('reset') ;
82
83		$key_name =
84			[
85			  $key_name
86			, $colors[$index] . $key_name . $reset_color
87			] ;
88
89		push @keys_to_dump, $key_name ;
90		}
91
92	return ('HASH', undef, @keys_to_dump) ;
93	}
94
95return (Data::TreeDumper::DefaultNodesToDisplay($tree)) ;
96}
97
98print Data::TreeDumper::DumpTree($s, "Colored labels (using a filter)", FILTER => \&ColorLabel) ;
99
100#allowing for a tree color
101
102print $colors[3] ;
103print Data::TreeDumper::DumpTree($s, "Colored tree and labels", FILTER => \&ColorLabel, __ANSI_COLOR_RESET => $colors[3]) ;
104print Term::ANSIColor::color('reset') ;
105
106