]> nmode's Git Repositories - Fey/blobdiff - lib/App/Fey.pm
Add option to group files/URIs
[Fey] / lib / App / Fey.pm
index c0778ab2ede342c99b79f96f124388ab303f3310..5b9206cf02fc978f7f0bf2e413616318e80ec7ab 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, @_);
@@ -57,6 +59,37 @@ sub _launch {
     }
 }
 
+sub _launch_group {
+    my $self = shift;
+    my $options = shift;
+
+    my ($groups, $handlers) = ({}, {});
+    for my $file_or_uri (@_) {
+        my $handler = $self->_get_handler($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;