X-Git-Url: https://git.nmode.ca/Fey/blobdiff_plain/5646894a3a7d5c653d400f9e311438761dcf218e..4e138f7a2baf0a4565bd82a3757ed5da97f56549:/lib/App/Fey.pm diff --git a/lib/App/Fey.pm b/lib/App/Fey.pm index c137002..8b802a0 100644 --- a/lib/App/Fey.pm +++ b/lib/App/Fey.pm @@ -27,31 +27,108 @@ sub launch { my $self = shift; my $options = ref $_[0] ? shift : {}; + if (!(-t STDIN)) { + _read_stdin(\@_); + } + if (!@_ || $options->{interactive}) { + open STDIN, '<', '/dev/tty'; + _read_stdin(\@_); + } die "No files or URIs specified.\n" unless @_; - ARG: for my $file_or_uri (@_) { - if ($options->{fork} && !$options->{single}) { + if ($options->{group}) { + $self->_launch_group($options, @_); + } elsif ($options->{single}) { + $self->_launch_single($options, @_); + } else { + $self->_launch($options, @_); + } +} + +sub _launch { + my $self = shift; + my $options = shift; + + if ($options->{fork}) { + for my $file_or_uri (@_) { my $pid = fork; - next ARG if ($pid); + next if $pid; + + my $handler = $self->_get_handler($options, $file_or_uri); + $handler->($file_or_uri) if $handler; + return; + } + } else { + for my $file_or_uri (@_) { + my $handler = $self->_get_handler($options, $file_or_uri); + $handler->($file_or_uri) if $handler; + } + } +} + +sub _launch_group { + my $self = shift; + my $options = shift; + + my ($groups, $handlers) = ({}, {}); + for my $file_or_uri (@_) { + my $handler = $self->_get_handler($options, $file_or_uri); + if ($handler) { + $groups->{"$handler"} //= []; + push @{ $groups->{"$handler"} }, $file_or_uri; + $handlers->{"$handler"} = $handler; + } + } + + if ($options->{fork}) { + for my $group (keys %{ $groups }) { + if ($options->{fork}) { + my $pid = fork; + next if $pid; + } + + $handlers->{$group}->(@{ $groups->{$group} }); + return; + } + } else { + for my $group (keys %{ $groups }) { + $handlers->{$group}->(@{ $groups->{$group} }); } + } +} + +sub _launch_single { + my $self = shift; + my $options = shift; + + if ($options->{fork}) { + my $pid = fork; + return if $pid; + } - $file_or_uri = $1 if ($file_or_uri =~ m|^file://(.+)|); - 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); - return if ($options->{fork}); - next ARG; - } + my $handler = $self->_get_handler($options, $_[0]); + $handler->(@_) if $handler; +} + +sub _get_handler { + my $self = shift; + my $options = 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}; + + my $context = $options->{context}; + my $handler = $associations->{$context} if $context; + return $handler if defined $handler; + + for my $context (keys %{ $associations }) { + if ($self->{contexts}->{$context}->()) { + return $associations->{$context}; } } } @@ -59,6 +136,13 @@ sub launch { } } +sub _read_stdin { + for my $file_or_uri () { + chomp $file_or_uri; + push @{ $_[0] }, $file_or_uri; + } +} + sub fey { App::Fey->new(ref $_[0] ? $_[0] : {})->launch(@_); }