#!/usr/bin/env raku use URL; subset HttpUrl of URL where *.scheme ∈ < http https >; #| Wrapper around xdg-open, which I find easier to adapt. sub MAIN ( #| The target to open. $target, #| When True, will only print the command that was intended to be run. Bool:D :$dry-run = False, ) { my $command = cmd(objectify-target($target)); if ($dry-run) { say $command; exit 0; } exit shell($command).exitcode; } #| Turn a plain' ol target into a nice object. This will be useful for using #| multi-dispatch. sub objectify-target ( $target ) { with (URL.new($target)) { return $_ if $_.scheme; } IO::Path.new($target); } #| The cmd sub returns an iterable containing the command and all arguments. proto cmd ($ --> Str) { * } # # HTTP(s) URLs # # URLs only used in a work setting, which should open in the browser used for # working purposes. multi sub cmd (HttpUrl $t where { $_.hostname.ends-with('gitlab.com') && $_.path[0].?fc eq 'mintlab' }) { "chromium '$t'" } multi sub cmd (HttpUrl $t where *.hostname.ends-with('atlassian.net')) { "chromium '$t'" } multi sub cmd (HttpUrl $t where *.hostname.ends-with('aws.amazon.com')) { "chromium '$t'" } multi sub cmd (HttpUrl $t where *.hostname.ends-with('google.com')) { "chromium '$t'" } multi sub cmd (HttpUrl $t where *.hostname.ends-with('slack.com')) { "chromium '$t'" } multi sub cmd (HttpUrl $t where *.hostname.ends-with('zaaksysteem.net')) { "chromium '$t'" } multi sub cmd (HttpUrl $t where *.hostname.ends-with('zaaksysteem.nl')) { "chromium '$t'" } # Rewrite bad site to good site my @not-twitter = < prvy.top nitter.42l.fr nitter.eu nitter.fdn.fr nitter.namazso.eu nitter.nixnet.services nitter.pussthecat.org nitter.tedomum.net nitter.unixfox.eu >; multi sub cmd (HttpUrl $t is copy where *.hostname eq "twitter.com") { callwith(URL.new(|$t.Hash, hostname => @not-twitter.pick())) } multi sub cmd (HttpUrl $t is copy where *.hostname eq "instagram.com") { callwith(URL.new(|$t.Hash, hostname => 'brap.top', path => $t.path.unshift('u'))) } multi sub cmd (HttpUrl $t is copy where *.hostname eq "www.instagram.com") { callwith(URL.new(|$t.Hash, hostname => 'brap.top', path => $t.path.unshift('u'))) } # All other URLs should be opened with the preferred browser. multi sub cmd (HttpUrl $t) { "%*ENV '$t'" } # # Local files # multi sub cmd (IO::Path $t where { $_.f && $_.extension eq 'pdf' }) { "zathura '$t'" } multi sub cmd (IO::Path $t where { $_.f && $_.extension }) { "pygmentize '$t' | less -R +k" } multi sub cmd (IO::Path $t where { $_.f }) { "less -R +k '$t'" } # # Fallback command # multi sub cmd ($t) { "xdg-open '$t'" }