File Coverage

blib/lib/Debug/Fork/Tmux/Config.pm
Criterion Covered Total %
statement 56 58 96.5
branch 8 12 66.6
condition 2 2 100.0
subroutine 13 13 100.0
pod 2 2 100.0
total 81 87 93.1


line stmt bran cond sub pod time code
1             #
2             # This file is part of Debug-Fork-Tmux
3             #
4             # This software is Copyright (c) 2013 by Peter Vereshagin.
5             #
6             # This is free software, licensed under:
7             #
8             # The (three-clause) BSD License
9             #
10             # ABSTRACT: Configuration system for Debug::Fork::Tmux
11             package Debug::Fork::Tmux::Config;
12              
13             # Helps you to behave
14 5     5   3275 use strict;
  5         8  
  5         181  
15 5     5   25 use warnings;
  5         10  
  5         253  
16              
17             our $VERSION = '1.000012'; # VERSION
18             #
19             ### MODULES ###
20             #
21             # Glues up path components
22 5     5   23 use File::Spec;
  5         9  
  5         118  
23              
24             # Resolves up symlinks
25 5     5   24 use Cwd;
  5         8  
  5         351  
26              
27             # Dioes in a nicer way
28 5     5   25 use Carp;
  5         16  
  5         296  
29              
30             # Makes constants possible
31 5     5   1713 use Const::Fast;
  5         5571  
  5         44  
32              
33             # Withholds the Perl interpreter path
34             require Config;
35              
36             # Rips directory name from fully-qualified file name (fqfn)
37 5     5   379 use File::Basename;
  5         9  
  5         376  
38              
39             # Reads PATH environment variable into the array
40 5     5   4468 use Env::Path;
  5         12716  
  5         30  
41              
42             ### CONSTANTS ###
43             #
44             # Paths to search the 'tmux' binary
45             # Depends : On 'PATH' environment variable
46             const my @_DEFAULT_TMUX_PATHS => _default_tmux_path( Env::Path->PATH->List );
47              
48             # Default 'tmux' binary fqfn
49             const my $_DEFAULT_TMUX_FQFN =>
50                 _default_tmux_fqfn( \@_DEFAULT_TMUX_PATHS => [ '' => '.exe' ], );
51              
52             # Keep the configuration variables
53             my %_CONF;
54              
55             # Tmux file name with full path
56             $_CONF{'tmux_fqfn'} = $_DEFAULT_TMUX_FQFN;
57              
58             # Tmux 'neww' parameter for a system/shell command
59             $_CONF{'tmux_cmd_neww_exec'} = 'sleep 1000000';
60              
61             # Tmux 'neww' command paraneters to be sprintf()'d with 'tmux_fqfn' and
62             # pushed after split by spaces the 'tmux_cmd_neww_exec' into list of
63             # parameters
64             $_CONF{'tmux_cmd_neww'} = "neww -P";
65              
66             # Tmux command parameters to get a tty name
67             $_CONF{'tmux_cmd_tty'} = 'lsp -F #{pane_tty} -t';
68              
69             # Takes deprecated SPUNGE_* environment variables into the account, too
70             _env_to_conf(
71                 \%_CONF => "SPUNGE_",
72                 sub {
73                     warn sprintf( "%s is deprecated and will be unsupported" => shift );
74                 }
75             );
76              
77             # Take config override from %ENV
78             # Depends : On %ENV global of the main::
79             _env_to_conf( \%_CONF => "DF" );
80              
81             # Make configuration unchangeable
82             const %_CONF => %_CONF;
83              
84             ### ATTRIBUTES ###
85             #
86              
87             ### SUBS ###
88             #
89             # Function
90             # Reads environment to config
91             # Takes : HashRef[Str] configuration to read;
92             # Str environment variables' prefix to read config from;
93             # Optional CodeRef to evaluate with environment variable name
94             # as an argument.
95             # Depends : On configuration HashRef's keys and the corresponding
96             # environment variables
97             # Changes : Configuration HashRef supplied as an argument
98             # Outputs : From CodeRef if supplied to warn to STDOUT about SPUNGE_*
99             # deprecation
100             # Returns : n/a
101             sub _env_to_conf {
102 12     12   14797     my $conf = shift;
103 12         19     my $prefix = shift;
104 12   100     59     my $cref = shift || undef;
105              
106 12         38     foreach my $key ( keys %$conf ) {
107              
108             # Key for %ENV
109 44         84         my $env_key = $prefix . uc $key;
110              
111             # For no key in environment do nothing
112 44 100       127         next unless defined $ENV{$env_key};
113              
114             # Sub warns about deprecation
115 8 100       40         if ( defined $cref ) { $cref->($env_key); }
  2         8  
116              
117             # Real config change
118 8         173         $conf->{$key} = $ENV{$env_key};
119                 }
120             }
121              
122             # Function
123             # Finds default 'tmux' binary fully qualified fila name
124             # Takes : ArrayRef[Str] paths to search for 'tmux' binary
125             # ArrayRef[Str] suffixes of the binaries to search
126             # Depends : On 'tmux' binaries found in the system
127             # Requires : File::Spec module
128             # Returns : Str fully qualified file name of the 'tmux' binary, or just
129             # 'tmux' if no such binary was found
130             sub _default_tmux_fqfn {
131 5     5   12     my ( $paths => $suffixes ) = @_;
132 5         12     my $fqfn;
133              
134 5         14     foreach my $path (@$paths) {
135 5         9         my $fname;
136              
137             # Binary without prefix
138 5         12         foreach my $suffix (@$suffixes) {
139 10         169             $fname = File::Spec->catfile( $path, "tmux$suffix" );
140 10 50       302             if ( -x $fname ) {
141 0         0                 $fqfn = $fname;
142 0         0                 last; # foreach my $suffix
143                         }
144                     }
145              
146             # Fall back if no binary found in the default paths
147 5 50       26         $fqfn = 'tmux' unless defined $fqfn;
148              
149 5 50       38         last if defined $fqfn; # foreach my @$paths
150                 }
151              
152 5         24     return $fqfn;
153             }
154              
155             # Function
156             # Paths to search the 'tmux' binary in
157             # Takes : Array[Str] contents of the PATH environment variable
158             # Depends : On the current directory and Perl interpreter path
159             # Requires : Cwd, File::Basename, Config modules
160             # Returns : Array[Str] ordered unique path to search for 'tmux' binary
161             # except that was configured with environment variable
162             sub _default_tmux_path {
163 6     6   2339     my @paths = @_;
164              
165             # Additional paths to search for Tmux
166                 my @paths_add
167 6         3976         = map { Cwd::realpath($_) }
  12         13241  
168                     File::Basename::dirname( $Config::Config{'perlpath'} ),
169                     '.';
170 6         24     push @paths, @paths_add;
171              
172             # Filter out dupes
173 6         19     my %seen = ();
174 6         14     @paths = grep { !$seen{$_}++ } @paths;
  33         109  
175              
176 6         54     return @paths;
177             }
178              
179             # Static method
180             # Returns Str argument configured as a key supplied as an argument
181             # Takes : Str argument to read config for
182             # Depends : On %_CONF package lexical
183             # Requires : Carp
184             # Throws : If no configuration found for an argument
185             # Returns : %_CONF element for an argument
186             sub get_config {
187 9     9 1 11728     shift;
188 9         16     my $key = shift;
189              
190 9 50       38     croak("Undefined in a configuration: $key") unless defined $_CONF{$key};
191              
192 9         59     return $_CONF{$key};
193             }
194              
195             # Static method
196             # Takes : n/a
197             # Depends : On %_CONF package lexical
198             # Returns : Array keys of %_CONF package lexical
199 2     2 1 1856 sub get_all_config_keys { return keys %_CONF }
200              
201             # Returns true to require()
202             1;
203              
204             __END__
205            
206             =pod
207            
208             =head1 NAME
209            
210             Debug::Fork::Tmux::Config - Configuration system for Debug::Fork::Tmux
211            
212             =head1 VERSION
213            
214             This documentation refers to the module contained in the distribution C<Debug-Fork-Tmux> version 1.000012.
215            
216             =head1 SYNOPSIS
217            
218             use Debug::Fork::Tmux;
219            
220             my $tmux_fqfn = Debug::Fork::Tmux->config( 'tmux_fqfn' );
221            
222             =head1 DESCRIPTION
223            
224             This module reads description from environment variables and use defaults if
225             those are not set.
226            
227             For example C<tmux_fqfn> can be overridden with C<DFTMUX_FQFN>
228             variable, and so on.
229            
230             The C<SPUNGE_*> variables are supported yet but deprecated and will be
231             removed.
232            
233             =head1 SUBROUTINES/METHODS
234            
235             All of the following are static methods:
236            
237             =head2 PUBLIC
238            
239             =head3 C<get_config( Str the name of the option )>
240            
241             Retrieves configuration stored in an internal C<Debug::Fork::Tmux::Config>
242             constants.
243            
244             Returns C<Str> value of the configuration parameter.
245            
246             =head2 PRIVATE
247            
248             =head3 C<get_all_config_keys()>
249            
250             Returns C<Array[Str]> names of all the configuration parameters.
251            
252             =head1 CONFIGURATION AND ENVIRONMENT
253            
254             See L<Debug::Fork::Tmux/CONFIGURATION AND ENVIRONMENT>.
255            
256             =head1 DIAGNOSTICS
257            
258             =over
259            
260             =item C<Undefined in a configuration: E<lt>keyE<gt>>
261            
262             Dies if no key asked was found in the configuration.
263            
264             =back
265            
266             =head1 BUGS AND LIMITATIONS
267            
268             You can make new bug reports, and view existing ones, through the
269             web interface at L<http://bugs.vereshagin.org/product/Debug-Fork-Tmux>.
270            
271             =for :stopwords cpan testmatrix url annocpan anno bugtracker rt cpants kwalitee diff irc mailto metadata placeholders metacpan
272            
273             =head1 SUPPORT
274            
275             =head2 Perldoc
276            
277             You can find documentation for this module with the perldoc command.
278            
279             perldoc Debug::Fork::Tmux
280            
281             =head2 Websites
282            
283             The following websites have more information about this module, and may be of help to you. As always,
284             in addition to those websites please use your favorite search engine to discover more resources.
285            
286             =over 4
287            
288             =item *
289            
290             MetaCPAN
291            
292             A modern, open-source CPAN search engine, useful to view POD in HTML format.
293            
294             L<http://metacpan.org/release/Debug-Fork-Tmux>
295            
296             =item *
297            
298             Search CPAN
299            
300             The default CPAN search engine, useful to view POD in HTML format.
301            
302             L<http://search.cpan.org/dist/Debug-Fork-Tmux>
303            
304             =item *
305            
306             RT: CPAN's Bug Tracker
307            
308             The RT ( Request Tracker ) website is the default bug/issue tracking system for CPAN.
309            
310             L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Debug-Fork-Tmux>
311            
312             =item *
313            
314             AnnoCPAN
315            
316             The AnnoCPAN is a website that allows community annotations of Perl module documentation.
317            
318             L<http://annocpan.org/dist/Debug-Fork-Tmux>
319            
320             =item *
321            
322             CPAN Ratings
323            
324             The CPAN Ratings is a website that allows community ratings and reviews of Perl modules.
325            
326             L<http://cpanratings.perl.org/d/Debug-Fork-Tmux>
327            
328             =item *
329            
330             CPAN Forum
331            
332             The CPAN Forum is a web forum for discussing Perl modules.
333            
334             L<http://cpanforum.com/dist/Debug-Fork-Tmux>
335            
336             =item *
337            
338             CPANTS
339            
340             The CPANTS is a website that analyzes the Kwalitee ( code metrics ) of a distribution.
341            
342             L<http://cpants.perl.org/dist/overview/Debug-Fork-Tmux>
343            
344             =item *
345            
346             CPAN Testers
347            
348             The CPAN Testers is a network of smokers who run automated tests on uploaded CPAN distributions.
349            
350             L<http://www.cpantesters.org/distro/D/Debug-Fork-Tmux>
351            
352             =item *
353            
354             CPAN Testers Matrix
355            
356             The CPAN Testers Matrix is a website that provides a visual overview of the test results for a distribution on various Perls/platforms.
357            
358             L<http://matrix.cpantesters.org/?dist=Debug-Fork-Tmux>
359            
360             =item *
361            
362             CPAN Testers Dependencies
363            
364             The CPAN Testers Dependencies is a website that shows a chart of the test results of all dependencies for a distribution.
365            
366             L<http://deps.cpantesters.org/?module=Debug::Fork::Tmux>
367            
368             =back
369            
370             =head2 Email
371            
372             You can email the author of this module at C<peter@vereshagin.org> asking for help with any problems you have.
373            
374             =head2 Bugs / Feature Requests
375            
376             Please report any bugs or feature requests by email to C<peter@vereshagin.org>, or through
377             the web interface at L<http://bugs.vereshagin.org/product/Debug-Fork-Tmux>. You will be automatically notified of any
378             progress on the request by the system.
379            
380             =head2 Source Code
381            
382             The code is open to the world, and available for you to hack on. Please feel free to browse it and play
383             with it, or whatever. If you want to contribute patches, please send me a diff or prod me to pull
384             from your repository :)
385            
386             L<http://gitweb.vereshagin.org/Debug-Fork-Tmux>
387            
388             git clone https://github.com/petr999/Debug-Fork-Tmux.git
389            
390             =head1 AUTHOR
391            
392             L<Peter Vereshagin|http://vereshagin.org> <peter@vereshagin.org>
393            
394             =head1 COPYRIGHT AND LICENSE
395            
396             This software is Copyright (c) 2013 by Peter Vereshagin.
397            
398             This is free software, licensed under:
399            
400             The (three-clause) BSD License
401            
402             =head1 SEE ALSO
403            
404             Please see those modules/websites for more information related to this module.
405            
406             =over 4
407            
408             =item *
409            
410             L<Debug::Fork::Tmux|Debug::Fork::Tmux>
411            
412             =item *
413            
414             L<Debug::Fork::Tmux::Config|Debug::Fork::Tmux::Config>
415            
416             =item *
417            
418             L<http://perlmonks.org/?node_id=128283|http://perlmonks.org/?node_id=128283>
419            
420             =item *
421            
422             L<nntp://nntp.perl.org/perl.debugger|nntp://nntp.perl.org/perl.debugger>
423            
424             =item *
425            
426             L<http://debugger.perl.org/|http://debugger.perl.org/>
427            
428             =back
429            
430             =head1 DISCLAIMER OF WARRANTY
431            
432             BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
433             FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT
434             WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER
435             PARTIES PROVIDE THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND,
436             EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE
437             IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
438             PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE
439             SOFTWARE IS WITH YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME
440             THE COST OF ALL NECESSARY SERVICING, REPAIR, OR CORRECTION.
441            
442             IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
443             WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
444             REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENCE, BE LIABLE
445             TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL, OR
446             CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE
447             SOFTWARE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING
448             RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A
449             FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF
450             SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
451             DAMAGES.
452            
453             =cut
454