make getpeername() return the original socket address which before it was intercepted
[hband-tools.git] / user-tools / paths2indent
blob19e9568c539e6986da761e0af631b93c3f0ed96e
1 #!/usr/bin/env perl
3 use Getopt::Long;
4 use Pod::Usage;
5 use Data::Dumper;
7 my $do_sort = 0;
8 my $stop_pattern = undef;
9 my $DIRSEP = '/';
11 GetOptions(
12 's|sort!' => \$do_sort,
13 'd|separator|dir-separator=s' => \$DIRSEP,
14 't|stop=s' => \$stop_pattern,
15 ) or pod2usage(-exitval=>2, -verbose=>99);
17 my %parsed;
19 while(my $line = <STDIN>)
21 chomp $line;
22 my $ref = \%parsed;
23 my ($path, $sep, $suffix);
25 if(defined $stop_pattern)
27 ($path, $sep, $suffix) = split /($stop_pattern)/, $line, 2;
29 else
31 $path = $line;
32 $sep = $suffix = '';
35 my @elements = split /\Q$DIRSEP\E/, $path;
36 $elements[$#elements] .= $sep . $suffix if @elements;
38 for my $element (@elements)
40 next if $element eq '';
41 $ref->{'children'}->{$element} = {
42 'index' => (scalar keys %{$ref->{'children'}}),
43 'name' => $element,
44 'children' => {},
45 } if not exists $ref->{'children'}->{$element};
46 $ref = $ref->{'children'}->{$element};
50 sub output
52 my $indent = shift;
53 my $ref = shift;
54 for my $child (sort {$do_sort ? ($a->{'name'} cmp $b->{'name'}) : ($a->{'index'} <=> $b->{'index'})} values %{$ref->{'children'}})
56 printf "%s%s\n", "\t"x$indent, $child->{'name'};
57 output($indent+1, $child);
61 output(0, \%parsed);
64 __END__
66 =pod
68 =head1 NAME
70 paths2indent - Transform list of filesystem paths to an indented list of the leaf elements
72 =head1 SYNOPSIS
74 paths2indent [I<OPTIONS>]
76 =head1 DESCRIPTION
78 Input: list of file paths line-by-line
80 Output: leaf file names indented by as many tabs as deep the file is on the tree
82 =head1 OPTIONS
84 =over 4
86 =item -d, --separator I<CHAR>
88 =item -t, --stop I<PATTERN>
90 =item -s, --sort
92 =back
94 =head1 LIMITATIONS
96 Can not have empty path elements (ie. consecutive slashes).
98 =head1 SEE ALSO
100 =over 4
102 =item https://github.com/jez/as-tree
104 =back
106 =cut