| 1 | #============================================================================= | ||||||||||
| 2 | # | ||||||||||
| 3 | # Module: Term::CLI::Argument::Filename | ||||||||||
| 4 | # | ||||||||||
| 5 | # Description: Class for file name arguments in Term::CLI | ||||||||||
| 6 | # | ||||||||||
| 7 | # Author: Steven Bakker (SBAKKER), <sbakker@cpan.org> | ||||||||||
| 8 | # Created: 23/01/18 | ||||||||||
| 9 | # | ||||||||||
| 10 | # Copyright (c) 2022 Steven Bakker | ||||||||||
| 11 | # | ||||||||||
| 12 | # This module is free software; you can redistribute it and/or modify | ||||||||||
| 13 | # it under the same terms as Perl itself. See "perldoc perlartistic." | ||||||||||
| 14 | # | ||||||||||
| 15 | # This software is distributed in the hope that it will be useful, | ||||||||||
| 16 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||||||||
| 17 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | ||||||||||
| 18 | # | ||||||||||
| 19 | #============================================================================= | ||||||||||
| 20 | |||||||||||
| 21 | package Term::CLI::Argument::Filename 0.061000; | ||||||||||
| 22 | |||||||||||
| 23 | 7 | ▶ | use 5.014; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 24 | 7 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 25 | |||||||||||
| 26 | 7 | ▶ | use Moo 1.000001; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 27 | 7 | ▶ | use namespace::clean 0.25; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 28 | 7 | extends 'Term::CLI::Argument'; | |||||||||
| 29 | |||||||||||
| 30 | 7 | ▶ | use File::Glob 'bsd_glob'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 31 | 7 | ▶ | use Fcntl ':mode'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 32 | |||||||||||
| 33 | 7 | ▶ | use namespace::clean; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 34 | |||||||||||
| 35 | sub complete { | ||||||||||
| 36 | 5 | ▶ | my ( $self, $text ) = @_; | ||||||||
|
Subroutine
complete: called CC=4 SCAR=25.0
Pod
documented
| |||||||||||
| 37 | |||||||||||
| 38 | my $func_ref = $self->term->Attribs->{filename_completion_function} | ||||||||||
| 39 | 5 | ▶ | or return $self->glob_complete($text); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 40 | |||||||||||
| 41 | 0 | ▶ | if ($func_ref) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 42 | 0 | my $state = 0; | |||||||||
| 43 | 0 | my @list; | |||||||||
| 44 | 0 | ▶ | while ( my $f = $func_ref->( $text, $state ) ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 45 | 0 | push @list, $f; | |||||||||
| 46 | 0 | $state = 1; | |||||||||
| 47 | } | ||||||||||
| 48 | 0 | return @list; | |||||||||
| 49 | } | ||||||||||
| 50 | } | ||||||||||
| 51 | |||||||||||
| 52 | sub glob_complete { | ||||||||||
| 53 | 10 | ▶ | my ( $self, $text ) = @_; | ||||||||
|
Subroutine
glob_complete: called CC=12 SCAR=26.3
Pod
documented
| |||||||||||
| 54 | 10 | my @list = bsd_glob("$text*"); | |||||||||
| 55 | |||||||||||
| 56 | 10 | ▶ | return if @list == 0; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 57 | |||||||||||
| 58 | 8 | ▶ | if (@list == 1) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 59 | 4 | ▶ | if (-d $list[0]) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 60 | # Dumb trick to get readline to expand a directory | ||||||||||
| 61 | # with a trailing "/", but *not* add a space. | ||||||||||
| 62 | # Simulates the way GNU readline does it. | ||||||||||
| 63 | 2 | return ("$list[0]/", "$list[0]//"); | |||||||||
| 64 | } | ||||||||||
| 65 | 2 | return @list; | |||||||||
| 66 | } | ||||||||||
| 67 | |||||||||||
| 68 | # Add filetype suffixes if there is more than one possible completion. | ||||||||||
| 69 | 4 | foreach (@list) { | |||||||||
| 70 | 14 | lstat; | |||||||||
| 71 | 14 | ▶ | if ( -l _ ) { $_ .= q{@}; next } | ||||||||
|
Branch: 1 branch
| |||||||||||
| 72 | 12 | ▶ | if ( -d _ ) { $_ .= q{/}; next } | ||||||||
|
Branch: 1 branch
| |||||||||||
| 73 | 10 | ▶ | if ( -c _ ) { $_ .= q{%}; next } | ||||||||
|
Branch: 1 branch
| |||||||||||
| 74 | 10 | ▶ | if ( -b _ ) { $_ .= q{#}; next } | ||||||||
|
Branch: 1 branch
| |||||||||||
| 75 | 10 | ▶ | if ( -S _ ) { $_ .= q{=}; next } | ||||||||
|
Branch: 1 branch
| |||||||||||
| 76 | 10 | ▶ | if ( -p _ ) { $_ .= q{=}; next } | ||||||||
|
Branch: 1 branch
| |||||||||||
| 77 | 10 | ▶ | if ( -x _ ) { $_ .= q{*}; next } | ||||||||
|
Branch: 1 branch
| |||||||||||
| 78 | } | ||||||||||
| 79 | 4 | return @list; | |||||||||
| 80 | } | ||||||||||
| 81 | |||||||||||
| 82 | 7 | 1; | |||||||||
| 83 | |||||||||||
| 84 | __END__ | ||||||||||
| 85 | |||||||||||
| 86 | =pod | ||||||||||
| 87 | |||||||||||
| 88 | =head1 NAME | ||||||||||
| 89 | |||||||||||
| 90 | Term::CLI::Argument::Filename - class for file name arguments in Term::CLI | ||||||||||
| 91 | |||||||||||
| 92 | =head1 VERSION | ||||||||||
| 93 | |||||||||||
| 94 | version 0.061000 | ||||||||||
| 95 | |||||||||||
| 96 | =head1 SYNOPSIS | ||||||||||
| 97 | |||||||||||
| 98 | use Term::CLI::Argument::Filename; | ||||||||||
| 99 | |||||||||||
| 100 | my $arg = Term::CLI::Argument::Filename->new(name => 'arg1'); | ||||||||||
| 101 | |||||||||||
| 102 | =head1 DESCRIPTION | ||||||||||
| 103 | |||||||||||
| 104 | Class for file name arguments in L<Term::CLI>(3p). Inherits from | ||||||||||
| 105 | the L<Term::CLI::Argument>(3p) class. | ||||||||||
| 106 | |||||||||||
| 107 | =head1 CLASS STRUCTURE | ||||||||||
| 108 | |||||||||||
| 109 | =head2 Inherits from: | ||||||||||
| 110 | |||||||||||
| 111 | L<Term::CLI::Argument>(3p). | ||||||||||
| 112 | |||||||||||
| 113 | =head2 Consumes: | ||||||||||
| 114 | |||||||||||
| 115 | None. | ||||||||||
| 116 | |||||||||||
| 117 | =head1 CONSTRUCTORS | ||||||||||
| 118 | |||||||||||
| 119 | See L<Term::CLI::Argument>(3p). | ||||||||||
| 120 | |||||||||||
| 121 | =head1 ACCESSORS | ||||||||||
| 122 | |||||||||||
| 123 | See L<Term::CLI::Argument>(3p). | ||||||||||
| 124 | |||||||||||
| 125 | =head1 METHODS | ||||||||||
| 126 | |||||||||||
| 127 | See L<Term::CLI::Argument>(3p). Additionally: | ||||||||||
| 128 | |||||||||||
| 129 | =over | ||||||||||
| 130 | |||||||||||
| 131 | =item B<complete> ( I<TEXT> ) | ||||||||||
| 132 | |||||||||||
| 133 | =item B<complete> ( I<TEXT>, I<STATE> ) | ||||||||||
| 134 | |||||||||||
| 135 | Complete the (partial) filename in I<TEXT>. The I<STATE> HashRef is | ||||||||||
| 136 | ignored. | ||||||||||
| 137 | |||||||||||
| 138 | If present, this uses the C<filename_completion_function> function listed | ||||||||||
| 139 | in L<Term::ReadLine>'s C<Attribs>, otherwise it will use the | ||||||||||
| 140 | L<glob_complete|/glob_complete> method. | ||||||||||
| 141 | |||||||||||
| 142 | Not every C<Term::ReadLine> implementation implements its own | ||||||||||
| 143 | filename completion function. The ones that do will have the | ||||||||||
| 144 | C<Attrib-E<gt>{filename_completion_function}> attribute set. | ||||||||||
| 145 | L<Term::ReadLine::Gnu> does this, while L<Term::ReadLine::Perl> doesn't. | ||||||||||
| 146 | |||||||||||
| 147 | =item B<glob_complete> ( I<TEXT> ) | ||||||||||
| 148 | |||||||||||
| 149 | =item B<glob_complete> ( I<TEXT>, I<STATE> ) | ||||||||||
| 150 | |||||||||||
| 151 | Complete the (partial) filename in I<TEXT>. The I<STATE> HashRef is | ||||||||||
| 152 | ignored. | ||||||||||
| 153 | |||||||||||
| 154 | Implementation uses L<bsd_glob from File::Glob|File::Glob/bsd_glob> and | ||||||||||
| 155 | applies some decorations on the filenames based on their type, similar | ||||||||||
| 156 | to what L<Term::ReadLine::Gnu|Term::ReadLine::Gnu> does. | ||||||||||
| 157 | |||||||||||
| 158 | =back | ||||||||||
| 159 | |||||||||||
| 160 | =head1 SEE ALSO | ||||||||||
| 161 | |||||||||||
| 162 | L<Term::CLI::Argument|Term::CLI::Argument>(3p), | ||||||||||
| 163 | L<Term::ReadLine::Gnu|Term::ReadLine::Gnu>(3p), | ||||||||||
| 164 | L<File::Glob|File::Glob>(3p), | ||||||||||
| 165 | L<Term::CLI|Term::CLI>(3p). | ||||||||||
| 166 | |||||||||||
| 167 | =head1 AUTHOR | ||||||||||
| 168 | |||||||||||
| 169 | Steven Bakker E<lt>sbakker@cpan.orgE<gt>, 2022. | ||||||||||
| 170 | |||||||||||
| 171 | =head1 COPYRIGHT AND LICENSE | ||||||||||
| 172 | |||||||||||
| 173 | Copyright (c) 2022 Steven Bakker | ||||||||||
| 174 | |||||||||||
| 175 | This module is free software; you can redistribute it and/or modify | ||||||||||
| 176 | it under the same terms as Perl itself. See "perldoc perlartistic." | ||||||||||
| 177 | |||||||||||
| 178 | This software is distributed in the hope that it will be useful, | ||||||||||
| 179 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||||||||
| 180 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | ||||||||||
| 181 | |||||||||||
| 182 | =cut | ||||||||||