rwxrob-dot/scripts/open
2022-02-19 00:50:21 +00:00

58 lines
1.5 KiB
Perl
Executable File

#!/usr/bin/env perl
# Opens what you would expect. If there is no argument passed to it
# checks if this is a GitHub repo and if so opens it with the gh tool in
# your browser. Otherwise, it checks if the argument is a file in the
# current directory and opens it with the default application
# (xdg-open). Finally, it assumes it is a URL, ensures begins with
# https:// and opens it in local web browser.
#
use v5.14;
$_ = shift;
$| = 1;
# markdown file
if (/\.md$/) {
say "markdown file detected";
if (not `which pandoc`) {
say "pandoc not found";
exit 1;
}
if (not `which lynx`) {
say "lynx not found";
exit 1;
}
`pandoc -s -o /tmp/index.html $_ 2>/dev/null`;
system 'lynx', '/tmp/index.html';
exit
}
# file
if (-r $_) {
`which xdg-open` && exec 'xdg-open', $_;
`which /usr/bin/open` && exec '/usr/bin/open', $_;
`which explorer.exe` && exec 'explorer.exe', $_;
say "unable to open file: $_";
exit 1;
}
# git repo
if ($_) {
my $gh = (grep {/github/ && s,git@,, && s,:,/, && s/\.git$//} qx{git remote get-url --all origin})[0];
if ($gh) {
system 'gh', 'repo', 'view','--web';
exit;
}
}
# bare url
m,^http, or s,^,https://,;
`which lynx` && exec 'lynx', $_;
`which /usr/bin/open` && exec '/usr/bin/open', $_;
`which explorer.exe` && exec 'explorer.exe', $_;
`which google-chrome` && exec 'google-chrome', $_;
my $chrome = '/System/Applications/Google\ Chrome.app';
`which $chrome` && exec "$chrome", "$_";
say "unable to determine how to open $_";