]> nmode's Git Repositories - Fey/commitdiff
Add option to force context
authorNaeem Model <me@nmode.ca>
Wed, 16 Jul 2025 02:27:56 +0000 (02:27 +0000)
committerNaeem Model <me@nmode.ca>
Wed, 16 Jul 2025 02:27:56 +0000 (02:27 +0000)
lib/App/Fey.pm
script/fey

index 5b9206cf02fc978f7f0bf2e413616318e80ec7ab..7b287f07e185cfeeb2ca1d70adcbbfa923842e7a 100644 (file)
@@ -47,13 +47,13 @@ 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;
         }
     }
@@ -65,7 +65,7 @@ sub _launch_group {
 
     my ($groups, $handlers) = ({}, {});
     for my $file_or_uri (@_) {
-        my $handler = $self->_get_handler($file_or_uri);
+        my $handler = $self->_get_handler($options, $file_or_uri);
         if ($handler) {
             $groups->{"$handler"} //= [];
             push @{ $groups->{"$handler"} }, $file_or_uri;
@@ -99,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;
@@ -113,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};
index a612187d71871fc81b592b00dab9056573c2a36f..69eca68a6bef2e525c7e213f65b5c423b7cf0edf 100644 (file)
@@ -7,12 +7,14 @@ use Getopt::Long;
 use App::Fey qw(fey);
 
 my $options = {
+    context => undef,
     fork => 0,
     group => 0,
     single => 0
 };
 
 GetOptions(
+    'c|context:s' => \$options->{context},
     'f|fork' => \$options->{fork},
     'g|group' => \$options->{group},
     's|single' => \$options->{single}