From 23078f6dedd051cb7df23854271b0fcccd51d59d Mon Sep 17 00:00:00 2001 From: Mats Date: Mon, 28 Jan 2013 17:43:42 +0100 Subject: [PATCH] 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 --- i3-dmenu-desktop | 32 ++++++++++++++++++++++++-------- 1 file changed, 24 insertions(+), 8 deletions(-) diff --git a/i3-dmenu-desktop b/i3-dmenu-desktop index 3a52faa5..79412035 100755 --- a/i3-dmenu-desktop +++ b/i3-dmenu-desktop @@ -39,11 +39,11 @@ sub slurp { } } -my $entry_type = 'both'; +my @entry_types; my $dmenu_cmd = 'dmenu -i'; my $result = GetOptions( 'dmenu=s' => \$dmenu_cmd, - 'entry-type=s' => \$entry_type, + 'entry-type=s' => \@entry_types, 'version' => sub { say "dmenu-desktop 1.3 © 2012 Michael Stapelberg"; exit 0; @@ -54,6 +54,11 @@ my $result = GetOptions( 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 ┃ # ┃ .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})) { # There are two .desktop files which contain the same “Name” value. # I’m not sure if that is allowed to happen, but we disambiguate the @@ -267,7 +272,7 @@ for my $app (keys %apps) { $choices{$name} = $app; } - if ($entry_type eq 'command' || $entry_type eq 'both') { + if ('command' ~~ @entry_types) { my ($command) = split(' ', $apps{$app}->{Exec}); # Don’t add “geany” if “Geany” is already present. @@ -276,6 +281,16 @@ for my $app (keys %apps) { $choices{basename($command)} = $app; } + + if ('filename' ~~ @entry_types) { + my $filename = basename($app, '.desktop'); + + # Don’t 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: @@ -418,7 +433,7 @@ system('i3-msg', $cmd) == 0 or die "Could not launch i3-msg: $?"; =head1 SYNOPSIS - i3-dmenu-desktop [--dmenu='dmenu -i'] [--entry-type=both] + i3-dmenu-desktop [--dmenu='dmenu -i'] [--entry-type=name] =head1 DESCRIPTION @@ -467,11 +482,12 @@ version of dmenu. =item B<--entry-type=type> -Display the (localized) "Name" (type = name) or the command (type = command) or -both (type = both) in dmenu. +Display the (localized) "Name" (type = name), the command (type = command) or +the (*.desktop) filename (type = filename) in dmenu. This option can be +specified multiple times. Examples are "GNU Image Manipulation Program" (type = name), "gimp" (type = -command) and both (type = both). +command), and "libreoffice-writer" (type = filename). =back