aboutsummaryrefslogtreecommitdiff
path: root/.local/bin/open
blob: 9c78c0cdf86e16041cb10c4eb7345fd9ea2a0463 (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
#!/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<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) { "xdg-open '$t'" }