File Coverage

blib/lib/App/Nopaste/Command.pm
Criterion Covered Total %
statement 55 72 76.3
branch 13 22 59.0
condition 6 12 50.0
subroutine 12 14 85.7
pod 0 5 0.0
total 86 125 68.8


line stmt bran cond sub pod time code
1 3     3   70769 use strict;
  3         15  
  3         82  
2 3     3   13 use warnings;
  3         5  
  3         133  
3             package App::Nopaste::Command;
4             # ABSTRACT: command-line utility for L
5              
6             our $VERSION = '1.012';
7              
8 3     3   1429 use Getopt::Long::Descriptive ();
  3         97076  
  3         83  
9              
10 3     3   1236 use App::Nopaste;
  3         10  
  3         146  
11 3     3   20 use Module::Runtime 'use_module';
  3         6  
  3         22  
12 3     3   131 use namespace::clean 0.19;
  3         37  
  3         13  
13              
14             sub new_with_options {
15 0     0 0 0 my $class = shift;
16 0         0 my ($opt, $usage) = Getopt::Long::Descriptive::describe_options(
17             "$0 %o",
18             ['help|usage|?|h', 'Prints this usage information' ],
19             ['desc|description|d=s',
20             'The one line description of your paste. The default is usually the first few characters of your text.'
21             ],
22             ['nick|nickname|name|n=s', 'Your nickname, usually displayed with the paste.'],
23              
24             ['lang|language|l=s', 'The language of the nopaste. Default: perl.',
25             { default => 'perl' },
26             ],
27              
28             ['chan|channel|c=s',
29             'The channel for the nopaste, not always relevant. Usually tied to a pastebot in that channel which will announce your paste.',
30             ],
31              
32             ['services|service|s=s',
33             'The nopaste services to try, in order. You may also specify this in the env var NOPASTE_SERVICES.',
34             ],
35              
36             ['list_services|list|L', 'List available nopaste services'],
37              
38             ['copy|x', 'If specified, automatically copy the URL to your clipboard.'],
39              
40             ['paste|p', 'If specified, use only the clipboard as input.'],
41              
42             ['open_url|open|o', 'If specified, automatically open the URL using Browser::Open.'],
43              
44             ['quiet|q', 'If specified, do not warn or complain about broken services.'],
45              
46             ['private', 'If specified, paste privately to services where possible.'],
47             );
48              
49 0 0       0 print($usage->text), exit if $opt->help;
50              
51             my $self = $class->new({
52             extra_argv => [@ARGV],
53 0         0 map { $_ => $opt->$_ } qw(
  0         0  
54             desc nick lang chan list_services copy paste open_url quiet private services
55             ),
56             });
57             }
58              
59             sub new {
60 17     17 0 20816 my $class = shift;
61 17         29 my $self;
62 17 100       41 if (!ref $_[0]) {
63 2         5 $self = { @_ };
64             } else {
65 15         23 $self = $_[0]
66             }
67              
68             $self->{services} = [ split /\s+/, $self->{services} ]
69 17 50 66     57 if defined $self->{services} && !ref $self->{services};
70              
71 17         28 bless $self, $class;
72              
73 17         54 return $self
74             }
75             my @acc = qw(
76             desc nick lang chan list_services copy paste open_url quiet private usage
77             extra_argv services
78             );
79             for my $a (@acc) {
80 3     3   1264 no strict 'refs';
  3         14  
  3         1547  
81              
82 56     56   167 *{__PACKAGE__ . '::' . $a } = sub { $_[0]->{$a} }
83             }
84             sub filename {
85 4     4 0 7 my $self = shift;
86 4         7 my @files = @{ $self->extra_argv };
  4         9  
87              
88 4 100       21 return undef unless @files;
89 2 50 33     4 return undef if $self->paste or $files[0] eq '-';
90 2         8 return $files[0];
91             }
92              
93             sub run {
94 3     3 0 829 my $self = shift;
95              
96 3 50       16 if ($self->list_services) {
97 0         0 for (sort App::Nopaste->plugins) {
98 0         0 s/App::Nopaste::Service::(\w+)$/$1/;
99 0         0 print $_, "\n";
100             }
101 0         0 exit 0;
102             }
103              
104 3         14 my $text = $self->read_text;
105 2         13 utf8::decode($text);
106              
107 2         6 my %args = map { $_ => $self->$_ } @acc, qw(filename);
  28         86  
108              
109 2   33     14 $args{text} ||= $text;
110              
111       0     $args{error_handler} = $args{warn_handler} = sub { }
112 2 50       5 if $self->quiet;
113              
114 2         18 my $url = App::Nopaste->nopaste(%args);
115              
116 2 50       10 if ($self->copy) {
117 0         0 use_module('Clipboard')->import;
118 0         0 Clipboard->copy($url);
119             }
120              
121 2 50       8 if ($self->open_url) {
122 0         0 use_module('Browser::Open');
123 0         0 Browser::Open::open_browser($url);
124             }
125              
126 2         9 return $url;
127             }
128              
129             sub read_text {
130 2     2 0 2 my $self = shift;
131              
132 2 100 66     5 if ($self->paste && @{ $self->extra_argv }) {
  1         3  
133 1         5 die "You may not specify --paste and files simultaneously.\n";
134             }
135              
136 1 50       3 if ($self->paste) {
137 0         0 use_module('Clipboard')->import;
138 0         0 return Clipboard->paste;
139             }
140              
141 1         2 local @ARGV = @{ $self->extra_argv };
  1         3  
142 0           local $/;
143 0           return <>;
144             }
145              
146             1;
147              
148             __END__