File Coverage

blib/lib/Complete/Fish.pm
Criterion Covered Total %
statement 23 25 92.0
branch 5 8 62.5
condition 1 4 25.0
subroutine 4 4 100.0
pod 1 1 100.0
total 34 42 80.9


line stmt bran cond sub pod time code
1             package Complete::Fish;
2              
3             our $DATE = '2016-10-21'; # DATE
4             our $VERSION = '0.05'; # VERSION
5              
6 1     1   494 use 5.010001;
  1         2  
7 1     1   3 use strict;
  1         1  
  1         13  
8 1     1   3 use warnings;
  1         1  
  1         283  
9              
10             require Exporter;
11             our @ISA = qw(Exporter);
12             our @EXPORT_OK = qw(
13             format_completion
14             );
15              
16             require Complete::Bash;
17              
18             our %SPEC;
19              
20             $SPEC{':package'} = {
21             v => 1.1,
22             summary => 'Completion module for fish shell',
23             };
24              
25             $SPEC{format_completion} = {
26             v => 1.1,
27             summary => 'Format completion for output (for shell)',
28             description => <<'_',
29              
30             fish accepts completion reply in the form of one entry per line to STDOUT.
31             Description can be added to each entry, prefixed by tab character.
32              
33             _
34             args_as => 'array',
35             args => {
36             completion => {
37             summary => 'Completion answer structure',
38             description => <<'_',
39              
40             Either an array or hash, as described in `Complete`.
41              
42             _
43             schema=>['any*' => of => ['hash*', 'array*']],
44             req=>1,
45             pos=>0,
46             },
47             },
48             result => {
49             summary => 'Formatted string (or array, if `as` key is set to `array`)',
50             schema => ['any*' => of => ['str*', 'array*']],
51             },
52             result_naked => 1,
53             };
54             sub format_completion {
55 2     2 1 2560 my $comp = shift;
56              
57 2         2 my $as;
58             my $entries;
59              
60             # we currently use Complete::Bash's rule because i haven't done a read up on
61             # how exactly fish escaping rules are.
62 2 50       5 if (ref($comp) eq 'HASH') {
63 0   0     0 $as = $comp->{as} // 'string';
64 0         0 $entries = Complete::Bash::format_completion({%$comp, as=>'array'});
65             } else {
66 2         2 $as = 'string';
67 2         7 $entries = Complete::Bash::format_completion({
68             words=>$comp, as=>'array',
69             });
70             }
71              
72             # insert description
73             {
74 2 50       51 my $compary = ref($comp) eq 'HASH' ? $comp->{words} : $comp;
  2         4  
75 2         5 for (my $i=0; $i<@$compary; $i++) {
76              
77             my $desc = (ref($compary->[$i]) eq 'HASH' ?
78 6 100 50     14 $compary->[$i]{description} : '' ) // '';
79 6         5 $desc =~ s/\R/ /g;
80 6         13 $entries->[$i] .= "\t$desc";
81             }
82             }
83              
84             # turn back to string if that's what the user wants
85 2 50       5 if ($as eq 'string') {
86 2         3 $entries = join("", map{"$_\n"} @$entries);
  6         9  
87             }
88 2         10 $entries;
89             }
90              
91             1;
92             # ABSTRACT: Completion module for fish shell
93              
94             __END__
95              
96             =pod
97              
98             =encoding UTF-8
99              
100             =head1 NAME
101              
102             Complete::Fish - Completion module for fish shell
103              
104             =head1 VERSION
105              
106             This document describes version 0.05 of Complete::Fish (from Perl distribution Complete-Fish), released on 2016-10-21.
107              
108             =head1 DESCRIPTION
109              
110             fish allows completion of option arguments to come from an external command,
111             e.g.:
112              
113             % complete -c deluser -l user -d Username -a "(cat /etc/passwd|cut -d : -f 1)"
114              
115             The command is supposed to return completion entries one in a separate line.
116             Description for each entry can be added, prefixed with a tab character. The
117             provided function C<format_completion()> accept a completion answer structure
118             and format it for fish. Example:
119              
120             format_completion(["a", "b", {word=>"c", description=>"Another letter"}])
121              
122             will result in:
123              
124             a
125             b
126             c Another letter
127              
128             =head1 FUNCTIONS
129              
130              
131             =head2 format_completion($completion) -> str|array
132              
133             Format completion for output (for shell).
134              
135             fish accepts completion reply in the form of one entry per line to STDOUT.
136             Description can be added to each entry, prefixed by tab character.
137              
138             This function is not exported by default, but exportable.
139              
140             Arguments ('*' denotes required arguments):
141              
142             =over 4
143              
144             =item * B<$completion>* => I<hash|array>
145              
146             Completion answer structure.
147              
148             Either an array or hash, as described in C<Complete>.
149              
150             =back
151              
152             Return value: Formatted string (or array, if `as` key is set to `array`) (str|array)
153              
154             =head1 HOMEPAGE
155              
156             Please visit the project's homepage at L<https://metacpan.org/release/Complete-Fish>.
157              
158             =head1 SOURCE
159              
160             Source repository is at L<https://github.com/perlancar/perl-Complete-Fish>.
161              
162             =head1 BUGS
163              
164             Please report any bugs or feature requests on the bugtracker website L<https://rt.cpan.org/Public/Dist/Display.html?Name=Complete-Fish>
165              
166             When submitting a bug or request, please include a test-file or a
167             patch to an existing test-file that illustrates the bug or desired
168             feature.
169              
170             =head1 SEE ALSO
171              
172             L<Complete>
173              
174             L<Complete::Bash>
175              
176             Fish manual.
177              
178             =head1 AUTHOR
179              
180             perlancar <perlancar@cpan.org>
181              
182             =head1 COPYRIGHT AND LICENSE
183              
184             This software is copyright (c) 2016 by perlancar@cpan.org.
185              
186             This is free software; you can redistribute it and/or modify it under
187             the same terms as the Perl 5 programming language system itself.
188              
189             =cut