aboutsummaryrefslogtreecommitdiff
path: root/.local/bin/open
blob: 068af100b8de24b850d8b5f5f7dddda3d086e865 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#!/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 <http://www.gnu.org/licenses/>.

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('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 = <
	nitter.42l.fr
	nitter.eu
	nitter.fdn.fr
	nitter.namazso.eu
	nitter.nixnet.services
	nitter.pussthecat.org
	nitter.tedomum.net
	nitter.unixfox.eu
	twitter.lurkmore.com
>;

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<BROWSER> '$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"'] }