Created
September 21, 2020 08:32
-
-
Save xtetsuji/f45de577b9ef53ab971463be26b759f5 to your computer and use it in GitHub Desktop.
URL filter and open from STDIN
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
# urlopen - open (macOS) URL from STDIN | |
# SYNOPSIS: | |
# tail url_include_log | urlopen | |
# stdout-url-command | urlopen | |
# OPTIONS: | |
# urlopen [--pass-through|-p] : pass through STDOUT (default off) | |
# urlopen [--verbose|-v] : outout opend URL (default off) | |
use v5.12; | |
use Getopt::Long qw(:config posix_default no_ignore_case bundling auto_help); | |
GetOptions( | |
\my %opt, | |
"pass-through|p", "verbose|v" | |
); | |
while(<>) { | |
my @urls = m{\b(https?://\S+)}g; | |
for my $url (only_first_seen(@urls)) { | |
open_url($url); | |
} | |
print if $opt{"pass-through"}; | |
} | |
sub open_url { | |
my $url = shift; | |
print "urlopen> open $url\n" if $opt{verbose}; | |
system "open", $url; | |
} | |
sub only_first_seen { | |
state %seen; | |
return grep { !$seen{$_}++ } @_; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment