Advertisement
Guest User

tree.pl

a guest
Feb 27th, 2015
296
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 2.44 KB | None | 0 0
  1.  
  2. $no_of_args = @ARGV;
  3. $expand_size = 5;
  4. $max_width = 10;
  5.  
  6. if(($no_of_args == 0) || ($no_of_args == 2))
  7. {
  8.   print "ERROR: Insufficient argument\n";
  9.   goto help;
  10. }
  11. elsif($no_of_args == 3)
  12. {
  13.   if($ARGV[1] !~ "-width")
  14.   {
  15.     print "ERROR: Invalid argument\n";
  16.     goto help;
  17.   }
  18.   elsif(($ARGV[2] > $max_width) || ($ARGV[2] < 2))
  19.   {
  20.     print "ERROR: Width should not lie outside 2 to $max_width.\n";
  21.     goto help;
  22.   }
  23.   else
  24.   {
  25.     $expand_size = $ARGV[2];
  26.   }
  27. }
  28. $dir_path = $ARGV[0];
  29. if(!(-d $dir_path))
  30. {
  31.   print "ERROR: $dir_path doesn't exist\n";
  32.   goto help;
  33. }
  34.  
  35. if(($dir_path =~ /^\.$/) || ($dir_path =~ /^\.\/$/))
  36. {
  37.   $string = `pwd`;
  38.   $dir_name = `basename $string`;
  39. }
  40. else
  41. {
  42.   $dir_name = `basename $dir_path`;
  43. }
  44.  
  45. @array = `find $dir_path/*`;
  46. $array_size = @array;
  47. $line_count = 0;
  48.  
  49. print " ----$dir_name";
  50. while($line_count < $array_size)
  51. {
  52.   $array[$line_count] =~ s/^$dir_path/  /;
  53.   $array[$line_count] =~ s/.[^\/]*\//--/g;
  54.   $line_count  ;
  55. }
  56.  
  57. $line_count = 0;
  58. while($line_count < $array_size)
  59. {
  60.   $array[$line_count] = "\|\-".$array[$line_count];
  61.   $string = " " x ($expand_size - 1);
  62.   while($array[$line_count] =~ /--/)
  63.   {
  64.     $array[$line_count] =~ s/--//;
  65.     $array[$line_count] =~ s/\|\-/|$string|-/;
  66.   }
  67.   $string = "-" x ($expand_size - 1);
  68.   $array[$line_count] =~ s/\-/$string/;
  69.   $array[$line_count] =~ s/\|$string/\ $string/;
  70.   $line_count  ;
  71. }
  72.  
  73. @cross = 0;
  74. $line_count = 0;
  75. while($line_count < $array_size)
  76. {
  77.   $line = $array[$array_size - $line_count - 1];
  78.   @line_array = split(//, $line);
  79.   $no_of_char = @line_array;
  80.   $char_count = 0;
  81.   while($char_count < $no_of_char)
  82.   {
  83.     if($line_array[$char_count] =~ /\ /)
  84.     {
  85.       $cross[$char_count] = 1;
  86.     }
  87.     elsif($line_array[$char_count] =~ /\|/)
  88.     {
  89.       if($cross[$char_count] == 0)
  90.       {
  91.         $line_array[$char_count] = " ";
  92.       }
  93.     }
  94.     else
  95.     {
  96.       $cross[$char_count] = 0;
  97.     }
  98.     $char_count = $char_count   $expand_size;
  99.   }
  100.  
  101.   $array[$array_size - $line_count - 1] = join("", @line_array);
  102.   $line_count  ;
  103. }
  104.  
  105. print @array;
  106. exit;
  107.  
  108. help:
  109. printf("#####################################################################\n");
  110. printf("# To run the script,\n");
  111. printf("# perl tree.pl <PATH> -width <width_no>\n");
  112. printf("#    e.g. \"perl tree.pl . \" displays tree view of current dir\n");
  113. printf("# \"-width\" expand or compress the tree. Max = $max_width, Min = 2\n");
  114. printf("# Default width = 5\n");
  115. printf("#####################################################################\n");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement