make getpeername() return the original socket address which before it was intercepted
[hband-tools.git] / user-tools / indent2tree
blob56f6ca77522b7785a72f127a84c6d684cffbfc41
1 #!/usr/bin/env perl
3 use Getopt::Long qw/:config no_ignore_case bundling no_getopt_compat/;
4 use Pod::Usage;
6 $char_vert = '│';
7 $char_hori = '──';
8 $char_chld = '├';
9 $char_last = '└';
11 GetOptions(
12 'v|vertical=s' => \$char_vert,
13 'h|horizontal=s' => \$char_hori,
14 'c|child=s' => \$char_chld,
15 'l|last=s' => \$char_last,
16 'a|ascii!' => sub {
17 $char_vert = '|';
18 $char_hori = '--';
19 $char_chld = '+';
20 $char_last = '`';
22 ) or pod2usage(-exitval=>2, -verbose=>99);
25 $indent_prev = 0;
26 $level = 0;
27 %level_indent = (0=>0);
28 $Tree = { subtree=>[], };
29 $ForkPoint = $Tree;
31 while(<STDIN>)
33 s/^(\t*)//;
34 $indent = length $1;
35 chomp;
36 $data = $_;
37 while(/\\$/)
39 $data =~ s/\\$/\n/;
40 $_ = <STDIN>;
41 s/^\t{$indent}//;
42 chomp;
43 $data .= $_;
46 if($indent > $indent_prev)
48 $level++;
49 $level_indent{$level} = $indent;
50 $ForkPoint = $ForkPoint->{subtree}->[$#{$ForkPoint->{subtree}}];
52 elsif($indent < $indent_prev)
54 while($indent < $level_indent{$level})
56 $level--;
57 $ForkPoint = $ForkPoint->{parent};
61 push @{$ForkPoint->{subtree}}, {data=>$data, parent=>$ForkPoint, subtree=>[]};
63 $indent_prev = $indent;
67 sub print_subtree
69 my $subtree = shift;
70 my $level = shift;
71 my $sidebranches = shift;
72 my $pos = 0;
74 for my $node (@$subtree)
76 my $last = $pos == $#$subtree;
78 if($level == 0)
80 printf "%s\n", $node->{data};
82 print_subtree($node->{subtree}, $level+1, '');
84 else
86 my $childindex = 0;
88 for my $data (split /\n/, $node->{data})
90 printf "%s%s %s\n",
91 $sidebranches,
92 $childindex == 0
93 ? ($last ? $char_last : $char_chld).$char_hori
94 : ($last ? ' ' : $char_vert).' ',
95 $data;
96 $childindex++;
99 print_subtree($node->{subtree}, $level+1, $sidebranches . ($last ? ' ' : $char_vert.' '));
102 $pos++;
106 print_subtree($Tree->{subtree}, 0, '');
109 __END__
111 =pod
113 =head1 NAME
115 indent2tree - Makes TAB-indented text into ascii tree chart
117 =head1 OPTIONS
119 =over 4
121 =item -a, --ascii
123 =item -v, --vertical I<CHAR>
125 =item -h, --horizontal I<CHAR>
127 =item -c, --child I<CHAR>
129 =item -l, --last I<CHAR>
131 =back
133 =head1 DESCRIPTION
135 Input: lines with leading tabs representing the depth in the tree.
136 Multiline records are supported by terminating lines (all but the last one) by backslash.
138 Output: tree diagramm with (ascii or unicode) drawing chars.
139 Set custom drawing chars by B<-v>, B<-h>, B<-c>, and B<-l> options.
141 =head1 SEE ALSO
143 =over 4
145 =item https://github.com/jez/as-tree
147 =back
149 =cut