File Coverage

blib/lib/App/TemplateCMD/Command/List.pm
Criterion Covered Total %
statement 24 56 42.8
branch 0 10 0.0
condition 0 4 0.0
subroutine 8 10 80.0
pod 2 2 100.0
total 34 82 41.4


line stmt bran cond sub pod time code
1             package App::TemplateCMD::Command::List;
2              
3             # Created on: 2008-03-26 13:44:05
4             # Create by: ivanw
5             # $Id$
6             # $Revision$, $HeadURL$, $Date$
7             # $Revision$, $Source$, $Date$
8              
9 2     2   1932 use strict;
  2         5  
  2         78  
10 2     2   11 use warnings;
  2         4  
  2         48  
11 2     2   451 use version;
  2         2090  
  2         11  
12 2     2   142 use Carp;
  2         4  
  2         149  
13 2     2   716 use List::MoreUtils qw/uniq/;
  2         14017  
  2         13  
14 2     2   2582 use Data::Dumper qw/Dumper/;
  2         8207  
  2         140  
15 2     2   804 use English qw/ -no_match_vars /;
  2         2046  
  2         13  
16 2     2   888 use base qw/App::TemplateCMD::Command/;
  2         36  
  2         1449  
17              
18             our $VERSION = version->new('0.6.10');
19             our @EXPORT_OK = qw//;
20             our %EXPORT_TAGS = ();
21              
22             sub process {
23 0     0 1   my ($self, $cmd, %option) = @_;
24 0           my $out = '';
25              
26 0           my $filter = shift @{$option{files}};
  0            
27 0           my $path = $cmd->config->{path};
28 0           my @path = grep {-d $_} split /:/, $path;
  0            
29              
30 0           my @files = $cmd->list_templates();
31              
32 0           my @templates = uniq sort map {$_->{file}} @files;
  0            
33              
34 0 0         if ($filter) {
35 0           @templates = grep {/$filter/} @templates;
  0            
36             }
37              
38 0   0       $option{columns} ||= 6;
39              
40 0           my $files_per_col = @templates / $option{columns};
41 0 0         $files_per_col = $files_per_col == int $files_per_col ? $files_per_col : int $files_per_col + 1;
42 0           my @columns;
43             my @max;
44              
45 0           for my $row ( 0 .. $files_per_col - 1 ) {
46 0           for my $col ( 0 .. $option{columns} - 1 ) {
47 0 0         next if !$templates[$row * $option{columns} + $col];
48 0           push @{$columns[$col]}, $templates[$row * $option{columns} + $col];
  0            
49 0   0       $max[$col] ||= 1;
50 0 0         $max[$col] = length $templates[$row * $option{columns} + $col] if length $templates[$row * $option{columns} + $col] > $max[$col];
51             }
52             }
53              
54 0           for my $row ( 0 .. $files_per_col -1 ) {
55 0           for my $col ( 0 .. $option{columns} - 1 ) {
56 0 0         next if !$columns[$col][$row];
57 0           $out .= $columns[$col][$row] . ' ' x ($max[$col] + 1 - length $columns[$col][$row]);
58             }
59 0           $out .= "\n";
60             }
61              
62 0           return $out;
63             }
64              
65             sub help {
66 0     0 1   my ($self) = @_;
67              
68 0           return <<"HELP";
69             $0 list [filter]
70              
71             filter An optional regular expression to show only templates that match.
72              
73             Lists all the templates that can be found, optionally limited by the regular
74             expression filter.
75             HELP
76             }
77              
78             1;
79              
80             __END__