]>
nmode's Git Repositories - Fey/blob - lib/App/Fey.pm
7 our @EXPORT_OK = qw(fey);
11 my ($class, $options) = @_;
12 my $config = do ($ENV{XDG_CONFIG_HOME
} // "$ENV{HOME}/.config") . '/fey/config.pl';
15 mime_query
=> $options->{mime_query
} // $config->{mime_query
} // sub {
16 open my $mime_type, '-|', 'file', '--brief', '--mime-type', $_[0];
19 contexts
=> $options->{contexts
} // $config->{contexts
} // { default => sub { 1 } },
20 targets
=> $options->{targets
} // $config->{targets
} // {}
28 my $options = ref $_[0] ? shift : {};
33 if (!@_ || $options->{interactive
}) {
34 open STDIN
, '<', '/dev/tty';
37 die "No files or URIs specified.\n" unless @_;
39 if ($options->{group
}) {
40 $self->_launch_group($options, @_);
41 } elsif ($options->{single
}) {
42 $self->_launch_single($options, @_);
44 $self->_launch($options, @_);
52 if ($options->{fork}) {
53 for my $file_or_uri (@_) {
57 my $handler = $self->_get_handler($options, $file_or_uri);
58 $handler->($file_or_uri) if $handler;
62 for my $file_or_uri (@_) {
63 my $handler = $self->_get_handler($options, $file_or_uri);
64 $handler->($file_or_uri) if $handler;
73 my ($groups, $handlers) = ({}, {});
74 for my $file_or_uri (@_) {
75 my $handler = $self->_get_handler($options, $file_or_uri);
77 $groups->{"$handler"} //= [];
78 push @{ $groups->{"$handler"} }, $file_or_uri;
79 $handlers->{"$handler"} = $handler;
83 if ($options->{fork}) {
84 for my $group (keys %{ $groups }) {
85 if ($options->{fork}) {
90 $handlers->{$group}->(@{ $groups->{$group} });
94 for my $group (keys %{ $groups }) {
95 $handlers->{$group}->(@{ $groups->{$group} });
104 if ($options->{fork}) {
109 my $handler = $self->_get_handler($options, $_[0]);
110 $handler->(@_) if $handler;
117 my $file_or_uri = $_[0] =~ m
|^file
://(.+)| ? $1 : $_[0];
118 my $mime_or_uri = -e
$file_or_uri ? $self->{mime_query
}->($file_or_uri) : $file_or_uri;
120 for my $target (@{ $self->{targets
} }) {
121 for my $pattern (@{ $target->{patterns
} }) {
122 if ($mime_or_uri =~ /$pattern/) {
123 my $associations = $target->{associations
};
125 my $context = $options->{context
};
126 my $handler = $associations->{$context} if $context;
127 return $handler if defined $handler;
129 for my $context (keys %{ $associations }) {
130 if ($self->{contexts
}->{$context}->()) {
131 return $associations->{$context};
140 for my $file_or_uri (<STDIN
>) {
142 push @{ $_[0] }, $file_or_uri;
147 App
::Fey-
>new(ref $_[0] ? $_[0] : {})->launch(@_);