-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdmenu_run
executable file
·152 lines (123 loc) · 3.59 KB
/
dmenu_run
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#!/usr/bin/env perl
use warnings;
use strict;
# dmenu_run.pl - run dmenu, showing most used results first.
# use -r option to remove something from the database
# written by Rex Roof <[email protected]> - February 2013
# released for free under the creative commons CC BY license.
# http://creativecommons.org/licenses/by/3.0
#
# Install by copying to dmenu_run.pl in your path and chmod ugo+x
# then replace your dmenu_run execution with dmenu_run.pl
# path to dmenu
my $DMENU_EXEC = "/usr/bin/dmenu";
use Storable qw(nstore retrieve);
use IPC::Open2;
my $STO = $ENV{HOME} . "/.dmenu_cache.sto";
# if we have a cache dir, use it.
my $CACHEDIR = $ENV{XDG_CACHE_HOME} || $ENV{HOME} . "/.cache";
if ( -d $CACHEDIR ) {
$STO = "$CACHEDIR/dmenu_run.sto";
}
# process command line arguments
my @dmenu_opts = ();
my $remove_key = '';
foreach my $bit ( @ARGV ) {
if ( $remove_key eq '_CATCH_ME_' ) {
$remove_key = $bit;
next;
}
if ( $bit =~ m/^-r(.*)/ ) {
if ( $1 ) {
$remove_key = $1;
} else {
# catch the next command line arg as the remove key
$remove_key = '_CATCH_ME_';
}
} else {
push @dmenu_opts, $bit;
}
}
if ( $remove_key eq '_CATCH_ME_' ) {
die HELP_MESSAGE();
}
# searching $PATH environment
my @dirs = split /:/, $ENV{PATH};
my $count_ref = {};
if ( -f $STO ) {
$count_ref = retrieve($STO);
}
# store our last rebuild time in this special key
$count_ref->{'--last-rebuild'} = 1 unless ( $count_ref->{'--last-rebuild'} );
## this section checks each directory in our path, looking for modification
# times since our last check
#
# the gap is the time since our check, in days
my $gap = ( ( $^T - $count_ref->{'--last-rebuild'} ) / 86400 );
foreach my $dir (@dirs) {
next unless ( -d $dir );
if ( ( -M $dir ) <= $gap ) {
if ( opendir( my $dh, $dir ) ) {
F: while ( my $f = readdir $dh ) {
# only index executable files.
next F unless ($f);
next F unless ( -x "$dir/$f" );
next F unless ( -f "$dir/$f" );
$count_ref->{$f} = 1 unless ( $count_ref->{$f} );
}
# update our rebuild time
$count_ref->{'--last-rebuild'} = time();
nstore $count_ref, $STO;
}
else {
warn "opendir on $dir failed. $!";
}
}
}
# if we have a cmd line argument to delete key
if ( $remove_key ) {
if ( $remove_key ) {
if ( $count_ref->{ $remove_key } ) {
delete $count_ref->{ $remove_key };
}
nstore $count_ref, $STO;
print "purged \'$remove_key\' from database.\n";
} else {
HELP_MESSAGE();
}
exit;
}
# execute dmenu to prompt the user
my $pid = open2( my $fh_out, my $fh_in, $DMENU_EXEC, @dmenu_opts );
foreach # run fancy_sort on our keys
my $bit ( sort { fancy_sort( $a, $b, $count_ref ) } keys %{$count_ref} )
{
next if ( $bit =~ m/^--/ );
print $fh_in $bit;
print $fh_in $/;
}
close($fh_in) or warn $!;
my $cmd = (<$fh_out>);
exit unless ($cmd);
chomp($cmd);
close($fh_out) or warn $!;
# make sure our dmenu process exits.
waitpid( $pid, 0 );
# store away our Storable file before we exec
$count_ref->{$cmd}++;
nstore $count_ref, $STO;
# execute our chosen command
exec($cmd);
# exit;
sub fancy_sort {
my $a = shift;
my $b = shift;
my $ref = shift;
# this sorts by execution count, then alphabetically
return ( $ref->{$b} <=> $ref->{$a} || $a cmp $b );
}
sub HELP_MESSAGE {
print "usage: $0 [-r key]\n\t removes key from dmenu application index\n";
return;
}
sub VERSION_MESSAGE { return 1 };