]> nmode's Git Repositories - Fey/blobdiff - lib/App/Fey.pm
Add option to force context
[Fey] / lib / App / Fey.pm
index c0778ab2ede342c99b79f96f124388ab303f3310..7b287f07e185cfeeb2ca1d70adcbbfa923842e7a 100644 (file)
@@ -29,7 +29,9 @@ sub launch {
 
     die "No files or URIs specified.\n" unless @_;
 
-    if ($options->{single}) {
+    if ($options->{group}) {
+        $self->_launch_group($options, @_);
+    } elsif ($options->{single}) {
         $self->_launch_single($options, @_);
     } else {
         $self->_launch($options, @_);
@@ -45,18 +47,49 @@ sub _launch {
             my $pid = fork;
             next if $pid;
 
-            my $handler = $self->_get_handler($file_or_uri);
+            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($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;
@@ -66,12 +99,13 @@ sub _launch_single {
         return if $pid;
     }
 
-    my $handler = $self->_get_handler($_[0]);
+    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;
@@ -80,6 +114,11 @@ sub _get_handler {
         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};