| line | stmt | bran | cond | sub | pod | time | code | 
| 1 | 3 |  |  | 3 |  | 82222 | use strict; | 
|  | 3 |  |  |  |  | 19 |  | 
|  | 3 |  |  |  |  | 94 |  | 
| 2 | 3 |  |  | 3 |  | 16 | use warnings; | 
|  | 3 |  |  |  |  | 7 |  | 
|  | 3 |  |  |  |  | 151 |  | 
| 3 |  |  |  |  |  |  | package App::Nopaste::Command; | 
| 4 |  |  |  |  |  |  | # ABSTRACT: command-line utility for L | 
| 5 |  |  |  |  |  |  |  | 
| 6 |  |  |  |  |  |  | our $VERSION = '1.013'; | 
| 7 |  |  |  |  |  |  |  | 
| 8 | 3 |  |  | 3 |  | 1699 | use Getopt::Long::Descriptive (); | 
|  | 3 |  |  |  |  | 116407 |  | 
|  | 3 |  |  |  |  | 85 |  | 
| 9 |  |  |  |  |  |  |  | 
| 10 | 3 |  |  | 3 |  | 1355 | use App::Nopaste; | 
|  | 3 |  |  |  |  | 10 |  | 
|  | 3 |  |  |  |  | 177 |  | 
| 11 | 3 |  |  | 3 |  | 23 | use Module::Runtime 'use_module'; | 
|  | 3 |  |  |  |  | 7 |  | 
|  | 3 |  |  |  |  | 32 |  | 
| 12 | 3 |  |  | 3 |  | 149 | use namespace::clean 0.19; | 
|  | 3 |  |  |  |  | 43 |  | 
|  | 3 |  |  |  |  | 15 |  | 
| 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 | 25970 | my $class = shift; | 
| 61 | 17 |  |  |  |  | 31 | my $self; | 
| 62 | 17 | 100 |  |  |  | 52 | if (!ref $_[0]) { | 
| 63 | 2 |  |  |  |  | 6 | $self = { @_ }; | 
| 64 |  |  |  |  |  |  | } else { | 
| 65 | 15 |  |  |  |  | 23 | $self = $_[0] | 
| 66 |  |  |  |  |  |  | } | 
| 67 |  |  |  |  |  |  |  | 
| 68 |  |  |  |  |  |  | $self->{services} = [ split /\s+/, $self->{services} ] | 
| 69 | 17 | 50 | 66 |  |  | 74 | if defined $self->{services} && !ref $self->{services}; | 
| 70 |  |  |  |  |  |  |  | 
| 71 | 17 |  |  |  |  | 32 | bless $self, $class; | 
| 72 |  |  |  |  |  |  |  | 
| 73 | 17 |  |  |  |  | 64 | 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 |  | 1482 | no strict 'refs'; | 
|  | 3 |  |  |  |  | 7 |  | 
|  | 3 |  |  |  |  | 1821 |  | 
| 81 |  |  |  |  |  |  |  | 
| 82 | 56 |  |  | 56 |  | 197 | *{__PACKAGE__ . '::' . $a } = sub { $_[0]->{$a} } | 
| 83 |  |  |  |  |  |  | } | 
| 84 |  |  |  |  |  |  | sub filename { | 
| 85 | 4 |  |  | 4 | 0 | 9 | my $self  = shift; | 
| 86 | 4 |  |  |  |  | 9 | my @files = @{ $self->extra_argv }; | 
|  | 4 |  |  |  |  | 10 |  | 
| 87 |  |  |  |  |  |  |  | 
| 88 | 4 | 100 |  |  |  | 29 | return undef unless @files; | 
| 89 | 2 | 50 | 33 |  |  | 5 | return undef if $self->paste or $files[0] eq '-'; | 
| 90 | 2 |  |  |  |  | 9 | return $files[0]; | 
| 91 |  |  |  |  |  |  | } | 
| 92 |  |  |  |  |  |  |  | 
| 93 |  |  |  |  |  |  | sub run { | 
| 94 | 3 |  |  | 3 | 0 | 974 | 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 |  |  |  |  | 12 | my $text = $self->read_text; | 
| 105 | 2 |  |  |  |  | 14 | utf8::decode($text); | 
| 106 |  |  |  |  |  |  |  | 
| 107 | 2 |  |  |  |  | 6 | my %args = map { $_ => $self->$_ } @acc, qw(filename); | 
|  | 28 |  |  |  |  | 110 |  | 
| 108 |  |  |  |  |  |  |  | 
| 109 | 2 |  | 33 |  |  | 16 | $args{text} ||= $text; | 
| 110 |  |  |  |  |  |  |  | 
| 111 |  |  |  | 0 |  |  | $args{error_handler} = $args{warn_handler} = sub { } | 
| 112 | 2 | 50 |  |  |  | 6 | if $self->quiet; | 
| 113 |  |  |  |  |  |  |  | 
| 114 | 2 |  |  |  |  | 19 | my $url = App::Nopaste->nopaste(%args); | 
| 115 |  |  |  |  |  |  |  | 
| 116 | 2 | 50 |  |  |  | 11 | if ($self->copy) { | 
| 117 | 0 |  |  |  |  | 0 | use_module('Clipboard')->import; | 
| 118 | 0 | 0 |  |  |  | 0 | eval { | 
| 119 | 0 |  |  |  |  | 0 | Clipboard->copy_to_all_selections($url); | 
| 120 | 0 |  |  |  |  | 0 | 1; | 
| 121 |  |  |  |  |  |  | } or Clipboard->copy($url); | 
| 122 |  |  |  |  |  |  | } | 
| 123 |  |  |  |  |  |  |  | 
| 124 | 2 | 50 |  |  |  | 7 | if ($self->open_url) { | 
| 125 | 0 |  |  |  |  | 0 | use_module('Browser::Open'); | 
| 126 | 0 |  |  |  |  | 0 | Browser::Open::open_browser($url); | 
| 127 |  |  |  |  |  |  | } | 
| 128 |  |  |  |  |  |  |  | 
| 129 | 2 |  |  |  |  | 12 | return $url; | 
| 130 |  |  |  |  |  |  | } | 
| 131 |  |  |  |  |  |  |  | 
| 132 |  |  |  |  |  |  | sub read_text { | 
| 133 | 2 |  |  | 2 | 0 | 3 | my $self = shift; | 
| 134 |  |  |  |  |  |  |  | 
| 135 | 2 | 100 | 66 |  |  | 6 | if ($self->paste && @{ $self->extra_argv }) { | 
|  | 1 |  |  |  |  | 4 |  | 
| 136 | 1 |  |  |  |  | 6 | die "You may not specify --paste and files simultaneously.\n"; | 
| 137 |  |  |  |  |  |  | } | 
| 138 |  |  |  |  |  |  |  | 
| 139 | 1 | 50 |  |  |  | 4 | if ($self->paste) { | 
| 140 | 0 |  |  |  |  | 0 | use_module('Clipboard')->import; | 
| 141 | 0 |  |  |  |  | 0 | return Clipboard->paste; | 
| 142 |  |  |  |  |  |  | } | 
| 143 |  |  |  |  |  |  |  | 
| 144 | 1 |  |  |  |  | 2 | local @ARGV = @{ $self->extra_argv }; | 
|  | 1 |  |  |  |  | 3 |  | 
| 145 | 0 |  |  |  |  |  | local $/; | 
| 146 | 0 |  |  |  |  |  | return <>; | 
| 147 |  |  |  |  |  |  | } | 
| 148 |  |  |  |  |  |  |  | 
| 149 |  |  |  |  |  |  | 1; | 
| 150 |  |  |  |  |  |  |  | 
| 151 |  |  |  |  |  |  | __END__ |