diff options
author | John <john@ankarstrom.se> | 2019-06-04 20:40:51 +0200 |
---|---|---|
committer | John <john@ankarstrom.se> | 2019-06-04 20:40:51 +0200 |
commit | 47b775e21bfd158268eb8dcba066489484ac4900 (patch) | |
tree | f3269dc0a417f9fd6caedf42ae072d16645f756a | |
parent | 49aa32c46a6f0375581492cde45a55d7c3331a07 (diff) | |
download | dwim-47b775e21bfd158268eb8dcba066489484ac4900.tar.gz |
run() subroutine
`run` is basically an alias for `exec`, except it prints the command
being executed to STDERR when the -d option is specified.
-rwxr-xr-x | dwim | 26 |
1 files changed, 18 insertions, 8 deletions
@@ -6,7 +6,7 @@ use v5.24; use warnings; use strict; use Path::ExpandTilde; -use subs qw/path env handle fail arguments/; +use subs qw/path env handle fail run arguments/; my ($phrase, %o) = arguments "usage: $0 parse\n", 1, d => 0; $phrase = `xsel -o` if not defined $phrase; @@ -24,30 +24,30 @@ our $handler; for ($phrase) { if (/^(https?:\/?\/?.+)$/) { handle "web address"; - exec "firefox", "$1" + run @BROWSER, "$1" } if (/^(mailto:.+)$/ or /^(.+@.+\.\w+)$/) { handle "e-mail address"; - exec @MAILER, "$1" + run @MAILER, "$1" } if (/^(.+):(\d+)(:.*?)?$/) { handle "{file}:{line} (like grep -n)"; my $p = path $1; - exec @LAUNCHER, @EDITOR, "-c", ":$2", "$p" + run @LAUNCHER, @EDITOR, "-c", ":$2", "$p" } if (/^(.+) line (\d+)\.?$/) { handle "{file} line {line} (like perl)"; my $p = path $1; - exec @LAUNCHER, @EDITOR, "-c", ":$2", "$p" + run @LAUNCHER, @EDITOR, "-c", ":$2", "$p" } if (/^(.+):(.+)$/) { handle "{file}:{query} (like grep)"; my $p = path $1; - exec @LAUNCHER, @EDITOR, "-c", "/$2", "$p" if -e $p; + run @LAUNCHER, @EDITOR, "-c", "/$2", "$p" if -e $p; fail "file not found" if $o{d}; # otherwise fall through } @@ -55,8 +55,9 @@ for ($phrase) { if (/^([^\s]+)$/) { handle "maildir / directory / file"; my $p = path $1; - exec @LAUNCHER, @MAILDIR_VIEWER, "$p" if $p =~ /^$MAILROOT/; # maildir - exec @LAUNCHER, @EDITOR, "$p" if -e $p; #file + run @LAUNCHER, @MAILDIR_VIEWER, "$p" if $p =~ /^$MAILROOT/; # maildir + run @LAUNCHER, @FILE_BROWSER, "$p" if -d $p; # directory + run @LAUNCHER, @EDITOR, "$p" if -e $p; # file fail "file not found" if $o{d}; # otherwise fall through } @@ -93,6 +94,15 @@ sub fail { print STDERR "$handler FAILED: $msg\n"; } +sub run { + if ($o{d}) { + my @argv = @_; + s/(\s)/\\$1/g for @argv; # escape whitespace + print STDERR "@argv\n"; + } + exec @_; +} + # parse ARGV and return list of positionals and hash of option values sub arguments { my $usage = shift; # usage string |