i3-dmenu-desktop: List filenames of .desktop files

In addition to 'name' and 'command', add a third entry type 'filename'
to list the filenames of the .desktop files (e.g., 'firefox.desktop'
would be display as 'firefox').

Command line option '--entry-type' can be specified multiple times.

fixes #930
This commit is contained in:
Mats 2013-01-28 17:43:42 +01:00 committed by Michael Stapelberg
parent bbede97966
commit 23078f6ded
1 changed files with 24 additions and 8 deletions

View File

@ -39,11 +39,11 @@ sub slurp {
} }
} }
my $entry_type = 'both'; my @entry_types;
my $dmenu_cmd = 'dmenu -i'; my $dmenu_cmd = 'dmenu -i';
my $result = GetOptions( my $result = GetOptions(
'dmenu=s' => \$dmenu_cmd, 'dmenu=s' => \$dmenu_cmd,
'entry-type=s' => \$entry_type, 'entry-type=s' => \@entry_types,
'version' => sub { 'version' => sub {
say "dmenu-desktop 1.3 © 2012 Michael Stapelberg"; say "dmenu-desktop 1.3 © 2012 Michael Stapelberg";
exit 0; exit 0;
@ -54,6 +54,11 @@ my $result = GetOptions(
die "Could not parse command line options" unless $result; die "Could not parse command line options" unless $result;
# Filter entry types and set default type(s) if none selected
my @valid_types = ('name', 'command', 'filename');
@entry_types = grep { $_ ~~ @valid_types } @entry_types;
@entry_types = ('name', 'command') unless @entry_types;
# ┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓ # ┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
# ┃ Convert LC_MESSAGES into an ordered list of suffixes to search for in the ┃ # ┃ Convert LC_MESSAGES into an ordered list of suffixes to search for in the ┃
# ┃ .desktop files (e.g. “Name[de_DE@euro]” for LC_MESSAGES=de_DE.UTF-8@euro ┃ # ┃ .desktop files (e.g. “Name[de_DE@euro]” for LC_MESSAGES=de_DE.UTF-8@euro ┃
@ -251,7 +256,7 @@ for my $app (keys %apps) {
} }
} }
if ($entry_type eq 'name' || $entry_type eq 'both') { if ('name' ~~ @entry_types) {
if (exists($choices{$name})) { if (exists($choices{$name})) {
# There are two .desktop files which contain the same “Name” value. # There are two .desktop files which contain the same “Name” value.
# Im not sure if that is allowed to happen, but we disambiguate the # Im not sure if that is allowed to happen, but we disambiguate the
@ -267,7 +272,7 @@ for my $app (keys %apps) {
$choices{$name} = $app; $choices{$name} = $app;
} }
if ($entry_type eq 'command' || $entry_type eq 'both') { if ('command' ~~ @entry_types) {
my ($command) = split(' ', $apps{$app}->{Exec}); my ($command) = split(' ', $apps{$app}->{Exec});
# Dont add “geany” if “Geany” is already present. # Dont add “geany” if “Geany” is already present.
@ -276,6 +281,16 @@ for my $app (keys %apps) {
$choices{basename($command)} = $app; $choices{basename($command)} = $app;
} }
if ('filename' ~~ @entry_types) {
my $filename = basename($app, '.desktop');
# Dont add “geany” if “Geany” is already present.
my @keys = map { lc } keys %choices;
next if lc($filename) ~~ @keys;
$choices{$filename} = $app;
}
} }
# %choices now looks like this: # %choices now looks like this:
@ -418,7 +433,7 @@ system('i3-msg', $cmd) == 0 or die "Could not launch i3-msg: $?";
=head1 SYNOPSIS =head1 SYNOPSIS
i3-dmenu-desktop [--dmenu='dmenu -i'] [--entry-type=both] i3-dmenu-desktop [--dmenu='dmenu -i'] [--entry-type=name]
=head1 DESCRIPTION =head1 DESCRIPTION
@ -467,11 +482,12 @@ version of dmenu.
=item B<--entry-type=type> =item B<--entry-type=type>
Display the (localized) "Name" (type = name) or the command (type = command) or Display the (localized) "Name" (type = name), the command (type = command) or
both (type = both) in dmenu. the (*.desktop) filename (type = filename) in dmenu. This option can be
specified multiple times.
Examples are "GNU Image Manipulation Program" (type = name), "gimp" (type = Examples are "GNU Image Manipulation Program" (type = name), "gimp" (type =
command) and both (type = both). command), and "libreoffice-writer" (type = filename).
=back =back