scripts/sway/flatpak-run.pl

63 lines
1.4 KiB
Perl
Executable File

#!/usr/bin/env perl
# Simplify `flatpak run`
use strict;
use warnings;
use v5.36;
my $input = shift(@ARGV) || die("Requires at least 1 argument\n");
my @list = split(/\n/, `flatpak list`);
shift(@list);
my %apps;
for my $line (@list) {
my ($name, $id, $version, $branch) = split(/\t/, $line);
next if ($id =~ /\b(Platform|Codecs)\b/);
$apps{$name} = $id;
}
# Exact name match first
run($apps{$input}, @ARGV) if (defined($apps{$input}));
# Case-insensitive name match next
foreach (keys(%apps)) {
print("Case-insensitive name match\n") if ($_ =~ m/^$input$/i);
run($apps{$_}, @ARGV) if ($_ =~ m/^$input$/i);
}
# Exact final id stanza next
foreach (keys(%apps)) {
print("Exact id match\n") if ($apps{$_} =~ m/\.$input$/);
run($apps{$_}, @ARGV) if ($apps{$_} =~ m/\.$input$/);
}
# Case-insensitive final id stanza next
foreach (keys(%apps)) {
print("Case-insensitive id match\n") if ($apps{$_} =~ m/\.$input$/i);
run($apps{$_}, @ARGV) if ($apps{$_} =~ m/\.$input$/i);
}
# Fuzzy name match
foreach (keys(%apps)) {
print("Fuzzy name match\n") if ($_ =~ m/$input/i);
run($apps{$_}, @ARGV) if ($_ =~ m/$input/i);
}
# Fuzzy id match
foreach (keys(%apps)) {
print("Fuzzy id match\n") if ($apps{$_} =~ m/$input/i);
run($apps{$_}, @ARGV) if ($apps{$_} =~ m/$input/i);
}
sub run($id, @args)
{
printf("Running $id ".join(' ',@args)."\n");
my $pid = fork();
if ($pid) {
exit();
} else {
system("/usr/bin/flatpak run $id ".join(' ', @args));
exit();
}
}