X-Git-Url: https://git.nmode.ca/Fey/blobdiff_plain/9c8c23c673e12cc18a0fbd61642c54aecc355d89..c148fbed0ac58a145abe1be864ebbf60cd8b96e9:/lib/App/Fey.pm?ds=inline diff --git a/lib/App/Fey.pm b/lib/App/Fey.pm index 4400e48..89f6535 100644 --- a/lib/App/Fey.pm +++ b/lib/App/Fey.pm @@ -8,13 +8,16 @@ our @EXPORT_OK = qw(fey); our $version = '0.01'; sub new { - my ($class, $args) = @_; + my ($class, $options) = @_; my $config = do ($ENV{XDG_CONFIG_HOME} // "$ENV{HOME}/.config") . '/fey/config.pl'; my $self = { - mime_query => $args->{mime_query} // $config->{mime_query} // sub { `file --brief --mime-type "$_[0]"` }, - contexts => $args->{contexts} // $config->{contexts} // { default => sub { 1 } }, - targets => $args->{targets} // $config->{targets} // {} + mime_query => $options->{mime_query} // $config->{mime_query} // sub { + open my $mime_type, '-|', 'file', '--brief', '--mime-type', $_[0]; + <$mime_type>; + }, + contexts => $options->{contexts} // $config->{contexts} // { default => sub { 1 } }, + targets => $options->{targets} // $config->{targets} // {} }; bless $self, $class; @@ -22,8 +25,16 @@ sub new { sub launch { my $self = shift; + my $options = ref $_[0] ? shift : {}; + + die "No files or URIs specified.\n" unless @_; ARG: for my $file_or_uri (@_) { + if ($options->{fork} && !$options->{single}) { + my $pid = fork; + next ARG if ($pid); + } + if ($file_or_uri =~ m|^file://(.+)|) { $file_or_uri = $1; } @@ -41,7 +52,12 @@ sub launch { 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; } } @@ -52,5 +68,5 @@ sub launch { } sub fey { - App::Fey->new(ref $_[0] ? shift : {})->launch(@_ ? @_ : die 'Error: No files or URIs specified.'); + App::Fey->new(ref $_[0] ? $_[0] : {})->launch(@_); }