App-lsplusplus

 view release on metacpan or  search on metacpan

ls++  view on Meta::CPAN

      next;
    }
    elsif($opt->{perm_time_file}) {
      perm_time_file($perm, $rel, $file);
      next;
    }
    elsif($opt->{perm_size_file}) {
      perm_size_file($perm, $size, $file);
      next;
    }
    elsif($opt->{perm_owner_time_size_file}) {
      perm_owner_time_size_file($perm, $user, $rel, $size, $file);
      next;
    }
    else {
      perm_time_size_file($perm, $rel, $size, $file);
      next;
    }
  }
  }
}

sub get_max_col_len {
  my($out) = @_;
  my($perm, $size, $max_size, $cur_size, $max_perm, $cur_perm);
  $max_size = 0;
  $max_perm = 0;
  foreach my $line (split(/^/, $out)) {
    ($perm, undef, undef, undef, $size) = split(/\s+/, $line);
    $cur_size = length($size);
    $cur_perm = length($perm);
    #special files show between 4 and 7 characters + 1 space: 255, 255
    if ($line =~ /\d+,\s+\d+/){
        $cur_size += 4;
    }
    $max_size = ($max_size, $cur_size)[$max_size < $cur_size];
    $max_perm = ($max_perm, $cur_perm)[$max_perm < $cur_perm];
  }
  return $max_size, $max_perm;
}

sub add_ls_color {
  my $file = shift;

  for my $pattern(keys(%ls_colors)) {
    my $no_ls_color_file = uncolor($file);

    # check if file is a directory and add a / suffix for simple
    # matching in ls++.conf
    if(-d $no_ls_color_file) {
      $no_ls_color_file .= '/' unless $no_ls_color_file =~ m{/$};
    }

    if($no_ls_color_file =~ m{$pattern}) {
      $file = $no_ls_color_file;
      if($ls_colors{$pattern} eq 'IGNORE') {
        return undef;
      }

#      # add attributes
#      # 'README$' => 'bold italic 220 underline',
      for my $attr(split(/\s+/, $ls_colors{$pattern})) {
        $file = fg($attr, $file);
        # if several attributes are specified, the END sequence is added
        # more than once. let's get rid of it so we can remove match and
        # remove the trailing /
        $file =~ s{(\e\[0?m)+}{$1}g;
      }
    }
  }
  # remove the trailing slash again
  $file =~ s{/(\e\[0?m)$}{$1};
#  print Dumper $file;
  return $file;
}

sub perm_file {
  my ($perm, $file) = @_;
  printf("%s%s%s%s\n", $d[0], $perm, $d[3], $file);
}


sub perm_time_size_file {
  my ($perm, $rel, $size, $file) = @_;
  if(get_os() eq 'darwin') {
    printf("%s%s%s%s%s%6s%s%s\n",
      $d[0],
      $perm,
      $d[3],
      $rel,
      $d[2],
      $size,
      $d[2],
      $file,
    );
  } else {
    printf("%s %s%s%s%s%s%s%s\n",
      $d[0],
      $perm,
      $d[3],
      $rel,
      $d[2],
      $size,
      $d[2],
      $file,
    );
  }

}

sub perm_owner_time_size_file {
  my ($perm, $user, $rel, $size, $file) = @_;
  if(get_os() eq 'darwin') {
    printf("%s%s%s%s%s%s%6s%s%s\n",
      $d[0],
      $perm,
      $d[3],
      $user,
      $d[2],
      $rel,
      $d[2],
      $size,
      $d[2],
      $file,
    );
  } 
  else {
    printf("%s %s%s%s%s%s%s%s%s%s\n",
      $d[0],
      $perm,
      $d[3],
      $user,
      $d[2],
      $rel,
      $d[2],
      $size,
      $d[2],
      $file,
    );
  }
}

sub perm_size_file {
  my ($perm, $size, $file) = @_;
  if(get_os() eq 'darwin') {
    if(length($size) == 38) {
      #$size = " $size ";
    }
    elsif(length($size) == 37) {
      $size .= ' ';
    }
    elsif(length($size) == 24) {
      $size = "            $size            ";
    }
    else {
    }
    printf("%s%s%s%s%s%s%s\n", $d[0], $perm, $d[3], $size, $d[2], $file);
  }
  else {
    printf("%s%s%s%*s%s%s\n", $d[0], $perm, $d[3], $stringlen, $size, $d[2], $file);
  }
}




sub perm {
  my ($perm) = @_;
  if (length($perm) < $permlen){
    # adding an extra space to align with occasional +
    $perm .= " ";
  }

  $perm =~ s/-/$d[1]/g;
  $perm =~ s/(r)/fg($c[2], $1)/eg;
  $perm =~ s/(w)/fg($c[7], $1)/eg;
  $perm =~ s/(x)/fg($c[1], $1)/eg;

  $perm =~ s/(d)/fg($c[16],   fg('bold', $1))/eg;
  $perm =~ s/(l)/fg($c[8],  fg('bold', $1))/eg;
  $perm =~ s/(s)/fg($c[11], $1)/eg;
  $perm =~ s/(S)/fg($c[8],  $1)/eg;
  $perm =~ s/(t)/fg($c[8],  $1)/eg;
  $perm =~ s/(T)/fg($c[8],  fg('bold', $1))/eg;

  return $perm;
}

sub owner {
  my ($user, $group) = @_;

  $user  = fg($c[7], $user);
  $group = fg($c[8], $group);
  return $user . ":". $group;
}

sub size {
  my ($size) = @_;
  if ($size =~ /\d+,\s*\d+$/){ # not size but major/minor values for special files
    $size = sprintf("%*s", $sizelen, $size);
    return $size;
  }
  $size =~ s/,/./;
  #FIXME
  if($colors > 16) {
    #$size =~ s/(\S+)(K)/$c[2]$1\e[0m$c[4]$2\e[0m/gi;# and print "AA\n";
    if($sizelen < 4){ # constraining size length between 4 and 8 characters max
        $stringlen = 4;
    }
    elsif($sizelen >= 8){
        $stringlen = 7;
    }
    else
    {
        $stringlen = $sizelen;
    }
    if($size =~ m/^(\S+)(K)/) {
      $size = sprintf("%27s",
        fg($c[7], sprintf("%*g", $stringlen, $1))
          . fg($c[2], fg('bold', $2))
        );
    }
    elsif($size =~ m/^(\S+)(M)/) {
      $size = sprintf("%29s",
        fg($c[7], sprintf("%*g", $stringlen, $1))
          . fg($c[9], fg('bold', $2))
        );
    }
    elsif($size =~ m/^(\S+)(G)/) {
      $size = sprintf("%27s",
        fg($c[7], sprintf("%*g", $stringlen, $1))
          . fg($c[3], fg('bold', $2))
        );
    }
    elsif($size =~ m/^(\d+)/) {
      $size = sprintf("%27s",
        fg($c[7], sprintf("%*d", $stringlen, $1))
          . fg($c[14], fg('bold', 'B'))
        );
    }
      return $size;
  }

  # ANSI
  else {
    return sprintf("% 26s", $size)
      if ($size =~ s/(.*)(K)/$c[2]$1$c[2]$c[17]$2$c[16]/);
    return sprintf("% 23s", $size)
      if ($size =~ s/(.*)(M)/$c[7]$1$c[17]$2$c[16]/);
    return sprintf("% 23s", $size)
      if ($size =~ s/(.*)(G)/$c[3]$1$c[17]$2$c[16]/);
    return sprintf("% 21s", $size) # 1012B
      if ($size =~ s/(\d+)/$c[14]$1$c[17]B$c[16]/);
  }
}



sub relative_time_format {
  my ($color, $string, $unit) = @_;
  return sprintf("%s%-3s%-3s", $color, $string, $unit);
}



sub relative_time {
  my ($cur, $sec) = @_;
  my $delta = $cur - $sec;
  my ($unit, $ret);

  ## 0 < sec < 60
  $unit = "sec ";
  $ret = $delta;

  return relative_time_format(fg($c[3]), "<", $unit)
    if $delta < 10;
  return relative_time_format(fg($c[3]), $ret, $unit)
    if $delta < 60;

  ## 1 < min < 45
  $unit = "min ";
  $ret = int($ret/60);

  return relative_time_format(fg($c[15]), "<", $unit)
    if $delta < 2 * 60;
  return relative_time_format(fg($c[15]), $ret, $unit)
    if $delta < 45 * 60;

  ## 0.75 < hour < 42
  $unit = "hour";
  $ret = int($ret/60);

  return relative_time_format(fg($c[9]), "<", $unit)
    if $delta < 90 * 60;
  return relative_time_format(fg($c[9]), $ret, $unit)
    if $delta < 24 * 60 * 60;
  return relative_time_format(fg($c[9]), $ret, $unit)
    if $delta < 30 * 60 * 60;



( run in 0.617 second using v1.01-cache-2.11-cpan-cdf2f3d4e48 )