X-Git-Url: https://git.nmode.ca/Fey/blobdiff_plain/9b3457a8dba8fccac63591eef54812994e1178e0..2e2b3d1671a5695e0c39c9c1203488ab032c05fd:/lib/App/Fey.pm diff --git a/lib/App/Fey.pm b/lib/App/Fey.pm index 9939a5c..c0778ab 100644 --- a/lib/App/Fey.pm +++ b/lib/App/Fey.pm @@ -29,31 +29,60 @@ sub launch { die "No files or URIs specified.\n" unless @_; - ARG: for my $file_or_uri (@_) { - if ($file_or_uri =~ m|^file://(.+)|) { - $file_or_uri = $1; - } + if ($options->{single}) { + $self->_launch_single($options, @_); + } else { + $self->_launch($options, @_); + } +} + +sub _launch { + my $self = shift; + my $options = shift; - my ($mime_or_uri, $targets); - if (-e $file_or_uri) { - $mime_or_uri = $self->{mime_query}->($file_or_uri) - } else { - $mime_or_uri = $file_or_uri; + if ($options->{fork}) { + for my $file_or_uri (@_) { + my $pid = fork; + next if $pid; + + my $handler = $self->_get_handler($file_or_uri); + $handler->($file_or_uri) if $handler; + return; + } + } else { + for my $file_or_uri (@_) { + my $handler = $self->_get_handler($file_or_uri); + $handler->($file_or_uri) if $handler; } + } +} + +sub _launch_single { + my $self = shift; + my $options = shift; + + if ($options->{fork}) { + my $pid = fork; + return if $pid; + } + + my $handler = $self->_get_handler($_[0]); + $handler->(@_) if $handler; +} + +sub _get_handler { + my $self = shift; + + my $file_or_uri = $_[0] =~ m|^file://(.+)| ? $1 : $_[0]; + my $mime_or_uri = -e $file_or_uri ? $self->{mime_query}->($file_or_uri) : $file_or_uri; - for my $target (@{ $self->{targets} }) { - for my $pattern (@{ $target->{patterns} }) { - if ($mime_or_uri =~ /$pattern/) { - my $associations = $target->{associations}; - for my $context (keys %{ $associations }) { - if ($self->{contexts}->{$context}->()) { - if ($options->{single}) { - $associations->{$context}->(@_); - return; - } - $associations->{$context}->($file_or_uri); - next ARG; - } + for my $target (@{ $self->{targets} }) { + for my $pattern (@{ $target->{patterns} }) { + if ($mime_or_uri =~ /$pattern/) { + my $associations = $target->{associations}; + for my $context (keys %{ $associations }) { + if ($self->{contexts}->{$context}->()) { + return $associations->{$context}; } } }