#!/usr/bin/env raku # open # Copyright (C) 2021 Patrick "tyil" SPek # # This program is free software: you can redistribute it and/or modify it under # the terms of the GNU Affero General Public License, version 3, as published # by the Free Software Foundation. # # This program is distributed in the hope that it will be useful, but WITHOUT # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS # FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more # details. # # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see . 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 a Str to be executed in a shell context. 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('gitlab.com') && $_.path[0].?fc eq 'xxllnc' }) { "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('awsapps.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 = < nitter.mnus.de twitter.alt.tyil.nl twitter.lurkmore.com >; my @not-reddit = < reddit.alt.tyil.nl >; my @not-youtube = < youtube.alt.tyil.nl >; multi sub cmd (HttpUrl $t is copy where *.hostname eq "twitter.com") { cmd(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'))) } multi sub cmd (HttpUrl $t is copy where *.hostname.ends-with("reddit.com")) { callwith(URL.new(|$t.Hash, hostname => @not-reddit.pick())) } multi sub cmd (HttpUrl $t is copy where *.hostname.ends-with("youtube.com")) { callwith(URL.new(|$t.Hash, hostname => @not-youtube.pick())) } # 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) { qq[notify-send 'open' 'No association to open "$t"'] }