| 1 | package App::lookup; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 2 | ▶ | use File::Spec; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 2 | ▶ | use Getopt::Long qw(:config bundling no_ignore_case); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 2 | ▶ | use Text::Abbrev 'abbrev'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 2 | ▶ | use Text::Wrap 'wrap'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 2 | our $VERSION = '0.06'; | |||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 0 | ▶ | sub parse_command_line { | ||||||||||||||||||||||||||
|
Subroutine
parse_command_line: not called CC=8 SCAR=42.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 14 | 0 | ▶ | GetOptions(\my %opts, 'help|h|?', 'man|m', 'version|v', | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 15 | 'sites|s', 'abbrevs|a', 'config-file|c=s', 'web-browser|w=s') | ||||||||||||||||||||||||||||
| 16 | or print_usage(-verbose => 0, -exitval => 1); | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 0 | ▶ | if ($opts{help}) { | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 19 | 0 | print_usage( | |||||||||||||||||||||||||||
| 20 | -verbose => 99, | ||||||||||||||||||||||||||||
| 21 | -sections => [qw(USAGE EXAMPLES OPTIONS)], | ||||||||||||||||||||||||||||
| 22 | -exitval => 0, | ||||||||||||||||||||||||||||
| 23 | ); | ||||||||||||||||||||||||||||
| 24 | } | ||||||||||||||||||||||||||||
| 25 | elsif ($opts{man}) { | ||||||||||||||||||||||||||||
| 26 | 0 | print_usage(-verbose => 2, -exitval => 0); | |||||||||||||||||||||||||||
| 27 | } | ||||||||||||||||||||||||||||
| 28 | elsif ($opts{version}) { | ||||||||||||||||||||||||||||
| 29 | 0 | ▶ | print_version() and exit; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 30 | } | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | die "The config file '$opts{'config-file'}' does not exist\n" | ||||||||||||||||||||||||||||
| 33 | 0 | ▶ | if $opts{'config-file'} and not -f $opts{'config-file'}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$opts{"config-file"} and not -f $opts{"config-file"}
MC/DC0%0/2
$opts{"config-file"} and not -f $opts{"config-file"}
$opts{"config-file"}
not -f $opts{"config-file"}
Truth table0%0/3
A and not(B)
A= $opts{"config-file"}B= not -f $opts{"config-file"}
| |||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 0 | $opts{sitename} = shift @ARGV; | |||||||||||||||||||||||||||
| 36 | 0 | $opts{query} = join ' ', @ARGV; | |||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 0 | return \%opts; | |||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | # thin wrapper around Pod::Usage::pod2usage since Pod::Usage causes | ||||||||||||||||||||||||||||
| 42 | # insignificant-but-noticable-enough-to-be-annoying delay in startup time. | ||||||||||||||||||||||||||||
| 43 | 0 | ▶ | sub print_usage { | ||||||||||||||||||||||||||
|
Subroutine
print_usage: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 44 | 0 | require Pod::Usage; | |||||||||||||||||||||||||||
| 45 | 0 | Pod::Usage::pod2usage(@_); | |||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 0 | ▶ | sub print_version { | ||||||||||||||||||||||||||
|
Subroutine
print_version: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 49 | 0 | require File::Basename; | |||||||||||||||||||||||||||
| 50 | 0 | my $progname = File::Basename::basename($0); | |||||||||||||||||||||||||||
| 51 | 0 | print "This is $progname version $VERSION running on perl $]\n"; | |||||||||||||||||||||||||||
| 52 | 0 | return 1; | |||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | 4 | ▶ | sub read_config_file { | ||||||||||||||||||||||||||
|
Subroutine
read_config_file: called CC=2 SCAR=7.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 56 | 4 | my $config_file = shift; | |||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | 4 | require Config::Tiny; | |||||||||||||||||||||||||||
| 59 | 4 | ▶ | Config::Tiny->read($config_file) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 60 | or die wrap( | ||||||||||||||||||||||||||||
| 61 | '', '', | ||||||||||||||||||||||||||||
| 62 | sprintf( | ||||||||||||||||||||||||||||
| 63 | "Error when reading configuration file %s: %s\n", | ||||||||||||||||||||||||||||
| 64 | $config_file, Config::Tiny->errstr | ||||||||||||||||||||||||||||
| 65 | )); | ||||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 4 | ▶ | sub initialize_sites { | ||||||||||||||||||||||||||
|
Subroutine
initialize_sites: called CC=7 SCAR=20.7
Pod
undocumented
| |||||||||||||||||||||||||||||
| 69 | 4 | ▶ | my $config_file = shift || File::Spec->catfile($ENV{HOME}, '.lookuprc'); | ||||||||||||||||||||||||||
|
Condition33%1/3
shift() || 'File::Spec'->catfile($ENV{'HOME'}, '.lookuprc')
MC/DC0%0/2
shift() || 'File::Spec'->catfile($ENV{'HOME'}, '.lookuprc')
shift()
'File::Spec'->catfile($ENV{'HOME'}, '.lookuprc')
Truth table33%1/3
A || B
A= shift()B= 'File::Spec'->catfile($ENV{'HOME'}, '.lookuprc')
| |||||||||||||||||||||||||||||
| 70 | 4 | my %predefined_sites = (google => 'http://google.com/search?q=%(query)'); | |||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 4 | ▶ | return \%predefined_sites unless -f $config_file; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 4 | my $config = read_config_file($config_file); | |||||||||||||||||||||||||||
| 75 | 4 | ▶ | my $aliases = $config->{alias} || {}; | ||||||||||||||||||||||||||
|
Condition50%1/2
$config->{'alias'} || {}
MC/DC0%0/1
$config->{'alias'} || {}
$config->{'alias'}
Truth table50%1/2
A
A= $config->{'alias'}
| |||||||||||||||||||||||||||||
| 76 | 4 | ▶ | my $user_sites = $config->{sites} || {}; | ||||||||||||||||||||||||||
|
Condition50%1/2
$config->{'sites'} || {}
MC/DC0%0/1
$config->{'sites'} || {}
$config->{'sites'}
Truth table50%1/2
A
A= $config->{'sites'}
| |||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | # merge the hashes, user defined sites take precedence over the predefined | ||||||||||||||||||||||||||||
| 79 | # ones (we only have one predefined site anyway) | ||||||||||||||||||||||||||||
| 80 | 4 | my %sites = (%$user_sites, %predefined_sites); | |||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | 4 | ▶ | while (my ($alias, $original) = each %$aliases) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 83 | 4 | ▶ | if (exists $sites{$original}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 84 | 4 | $sites{$alias} = $sites{$original}; | |||||||||||||||||||||||||||
| 85 | 4 | delete $sites{$original}; | |||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | else { | ||||||||||||||||||||||||||||
| 88 | 0 | warn "The alias '$alias' doesn't match any site\n"; | |||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | 4 | return \%sites; | |||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | 6 | ▶ | sub find_max_length { | ||||||||||||||||||||||||||
|
Subroutine
find_max_length: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 95 | 6 | require List::Util; | |||||||||||||||||||||||||||
| 96 | 6 | List::Util::max(map { length $_ } @_); | |||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | 2 | ▶ | sub print_sites { | ||||||||||||||||||||||||||
|
Subroutine
print_sites: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 100 | 2 | my $sites = shift; | |||||||||||||||||||||||||||
| 101 | 2 | my $maxlen = find_max_length(keys %$sites); | |||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | 2 | for my $site (sort keys %$sites) { | |||||||||||||||||||||||||||
| 104 | 8 | printf "- %-${maxlen}s : %s\n", $site, $sites->{$site}; | |||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | 2 | return 1; | |||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | 1 | ▶ | sub print_abbrevs { | ||||||||||||||||||||||||||
|
Subroutine
print_abbrevs: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 111 | 1 | my ($abbrevs, $sites) = @_; | |||||||||||||||||||||||||||
| 112 | 1 | my $data; | |||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | 1 | ▶ | while (my ($abbr, $sitename) = each %$abbrevs) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 115 | 15 | ▶ | push @{ $data->{$sitename} }, $abbr if $sites->{$sitename}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | 1 | for my $sitename (sort keys %$data) { | |||||||||||||||||||||||||||
| 119 | 4 | my $site = "Name"; | |||||||||||||||||||||||||||
| 120 | 4 | my $url = "URL"; | |||||||||||||||||||||||||||
| 121 | 4 | my $abstr = "Abbrev(s)"; | |||||||||||||||||||||||||||
| 122 | 4 | my $maxlen = find_max_length($site, $url, $abstr); | |||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | 4 | printf "%-${maxlen}s : %s\n", $site, $sitename; | |||||||||||||||||||||||||||
| 125 | 4 | printf "%-${maxlen}s : %s\n", $url, $sites->{$sitename}; | |||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | my $joined_abbrevs = sprintf("%-${maxlen}s : %s", | ||||||||||||||||||||||||||||
| 128 | 4 | $abstr, join ', ', sort @{ $data->{$sitename} }); | |||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | # + 3 to adjust the addition of colon and whitespace | ||||||||||||||||||||||||||||
| 131 | 4 | print wrap('', ' ' x ($maxlen + 3), $joined_abbrevs); | |||||||||||||||||||||||||||
| 132 | 4 | print "\n\n"; | |||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | 1 | return 1; | |||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | 0 | ▶ | sub browse_url { | ||||||||||||||||||||||||||
|
Subroutine
browse_url: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 139 | 0 | my ($url, $browser) = @_; | |||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | 0 | require URI::Encode; | |||||||||||||||||||||||||||
| 142 | 0 | $url = URI::Encode::uri_encode($url); | |||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | 0 | ▶ | if ($browser) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 145 | 0 | require IPC::System::Simple; | |||||||||||||||||||||||||||
| 146 | 0 | IPC::System::Simple::systemx(split(/\s/, $browser), $url); | |||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||
| 148 | else { | ||||||||||||||||||||||||||||
| 149 | 0 | require Browser::Open; | |||||||||||||||||||||||||||
| 150 | 0 | ▶ | if (defined(my $status = Browser::Open::open_browser($url))) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 151 | 0 | ▶ | if ($status != 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 152 | 0 | die "Error when opening $url with web browser\n"; | |||||||||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||||||||
| 154 | } | ||||||||||||||||||||||||||||
| 155 | else { | ||||||||||||||||||||||||||||
| 156 | 0 | die "Could not found the command to execute the web browser\n"; | |||||||||||||||||||||||||||
| 157 | } | ||||||||||||||||||||||||||||
| 158 | } | ||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | } | ||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | 0 | ▶ | sub run { | ||||||||||||||||||||||||||
|
Subroutine
run: not called CC=9 SCAR=45.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 163 | 0 | my $opts = parse_command_line(); | |||||||||||||||||||||||||||
| 164 | 0 | my $sites = initialize_sites($opts->{'config-file'}); | |||||||||||||||||||||||||||
| 165 | 0 | my $abbrevs = abbrev(keys %$sites); | |||||||||||||||||||||||||||
| 166 | 0 | my $sitename = $opts->{sitename}; | |||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | 0 | ▶ | if ($opts->{sites}) { | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 169 | 0 | ▶ | print_sites($sites) and exit; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 170 | } | ||||||||||||||||||||||||||||
| 171 | elsif ($opts->{abbrevs}) { | ||||||||||||||||||||||||||||
| 172 | 0 | ▶ | print_abbrevs($abbrevs, $sites) and exit; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 173 | } | ||||||||||||||||||||||||||||
| 174 | ▶ | elsif (not $sitename or not $opts->{query}) { | |||||||||||||||||||||||||||
|
Condition0%0/3
$sitename && $opts->{'query'}
MC/DC0%0/2
$sitename && $opts->{'query'}
$sitename
$opts->{'query'}
Truth table0%0/3
A && B
A= $sitenameB= $opts->{'query'}
| |||||||||||||||||||||||||||||
| 175 | 0 | print_usage('Too few arguments'); | |||||||||||||||||||||||||||
| 176 | } | ||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | 0 | ▶ | if (my $valid_site = $abbrevs->{$sitename}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 179 | 0 | my $url = $sites->{$valid_site}; | |||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | 0 | $url =~ s{%\(query\)}{$opts->{query}}; | |||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | 0 | browse_url($url, $opts->{'web-browser'}); | |||||||||||||||||||||||||||
| 184 | } | ||||||||||||||||||||||||||||
| 185 | else { | ||||||||||||||||||||||||||||
| 186 | 0 | ▶ | if (my @ambiguous = grep { /^$sitename/ } sort keys %$abbrevs) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 187 | 0 | print wrap( | |||||||||||||||||||||||||||
| 188 | '', '', | ||||||||||||||||||||||||||||
| 189 | sprintf( | ||||||||||||||||||||||||||||
| 190 | "Ambiguous site name: '$sitename'. Did you mean one of " | ||||||||||||||||||||||||||||
| 191 | . "the following sites/abbreviations: %s?\n", | ||||||||||||||||||||||||||||
| 192 | join(', ', @ambiguous))); | ||||||||||||||||||||||||||||
| 193 | } | ||||||||||||||||||||||||||||
| 194 | else { | ||||||||||||||||||||||||||||
| 195 | 0 | print "Could not find sites that match '$sitename'\n"; | |||||||||||||||||||||||||||
| 196 | } | ||||||||||||||||||||||||||||
| 197 | 0 | exit 1; | |||||||||||||||||||||||||||
| 198 | } | ||||||||||||||||||||||||||||
| 199 | } | ||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||
| 201 | 2 | 1; | |||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||
| 203 | __END__ | ||||||||||||||||||||||||||||