zoukankan      html  css  js  c++  java
  • perl 打印目录结构

    #!/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', "
        	$ atree [dir=./] [--ignores=./.atree] [--depath=0]
        	$ atree
        	$ atree ./lib
        	$ atree ./lib --depath 1
        	or 
        	$ atree ./lib -d 1
        	$ 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
    
  • 相关阅读:
    dubbo 学习
    JSTL 实现 为Select赋多个值
    Spring MVC 单元测试Demo
    IDEA git commit push revert
    高并发处理
    Redis Expire TTL命令
    Redis 原子操作INCR
    Redis 安装
    慢日志查询
    angularJs 处理多选框(checkbox)
  • 原文地址:https://www.cnblogs.com/ajanuw/p/12157049.html
Copyright © 2011-2022 走看看