#!/usr/bin/perl
# 递归打印目录结构
use v5.26;
use strict;
use utf8;
use autodie;
use warnings;
use Encode qw(decode encode);
use File::Spec::Functions;
use File::Basename;
use experimental 'smartmatch'; # 忽略智能匹配的错误警告
use Getopt::Long qw(GetOptions);
use Term::ANSIColor;
# 递归目录结构的次数, 0全部递归
my $depath = 0;
my $help;
my $ignores;
GetOptions (
"depath=i" => \$depath,
"ignores=s" => \$ignores,
"help" => \$help
);
if(defined($help)){
print color('green');
say encode('utf-8', "
\t\$ atree [dir=./] [--ignores=./.atree] [--depath=0]
\t\$ atree
\t\$ atree ./lib
\t\$ atree ./lib --depath 1
\tor
\t\$ atree ./lib -d 1
\t\$ atree -i ~/.atree -d 3
");
exit;
}
my $dirPath = $ARGV[0] // "./";
unless(-d -e $dirPath){
print color("red");
say encode('utf-8', "$dirPath 不是目录,或则不存在!!");
exit;
}
my $ignoresFileName = ".atree";
my $ignoresPath = $ignores // catfile($dirPath, $ignoresFileName);
# 默认忽视这些目录
my @ignores = ();
# 读取忽略文件
sub readIgnores {
if(defined($ignoresPath) && -e $ignoresPath){
my $fh;
open $fh, "<", $ignoresPath;
while(<$fh>){
chomp;
$_ =~ s/^\s+|\s+$//g;
next if /^#/;
push @ignores, $_;
}
close $fh;
}
}
readIgnores();
sub scan {
my ($dir, $dep, $depathCount) = @_;
if($dir && -d -e $dir) {
my @files = <$dir/* $dir/.[!.]*>;
$depathCount++;
for(@files){
# next if /\.{1,2}$/g;
my $p = catfile($_);
if(-d $p){
my($filename) = fileparse($p);
unless($filename ~~ @ignores){
say "$dep$filename/";
if($depath eq 0 or $depathCount lt $depath){
scan($p, "| ".$dep."", $depathCount);
}
}
}else {
my($filename) = fileparse($p);
say "$dep$filename";
}
}
};
}
scan($dirPath, "|-- ", 0);
λ atree -i ~/.atree -d 3
|-- analysis_options.yaml
|-- bin/
| |-- main.dart
|-- CHANGELOG.md
|-- lib/
| |-- dart_demo.dart
|-- pubspec.lock
|-- pubspec.yaml
|-- README.md
|-- result.html
|-- test/
| |-- dart_demo_test.dart
|-- .dart_tool/
| |-- build/
| | |-- 098d3ee73e6cc294616d3a2e2c3c81ad/
| | |-- entrypoint/
| | |-- generated/
| |-- pub/
| | |-- bin/
|-- .gitignore
|-- .local-chromium/
| |-- 672088/
| | |-- chrome-win/
|-- .packages
λ cat ~/.atree
.git
node_modules