diff options
author | Patrick Spek <p.spek@tyil.nl> | 2020-02-17 18:41:43 +0100 |
---|---|---|
committer | Patrick Spek <p.spek@tyil.nl> | 2020-02-17 18:41:43 +0100 |
commit | f71721fa68684c9adf2ae1d2d2d5c95ad6824afb (patch) | |
tree | 5e4d8318b73add679035e0d6f99760ad45b06eb7 /.local | |
parent | 7c2e4473a4e474cdbaec560f9a97b333c873fd58 (diff) |
Rewrite open command
Diffstat (limited to '.local')
-rwxr-xr-x | .local/bin/open | 77 | ||||
l--------- | .local/bin/xdg-open | 1 |
2 files changed, 58 insertions, 20 deletions
diff --git a/.local/bin/open b/.local/bin/open index f3391d7..381bd25 100755 --- a/.local/bin/open +++ b/.local/bin/open @@ -2,28 +2,65 @@ use URL; -sub MAIN ($target) -{ - my URL $url .= new($target); +subset HttpUrl of URL where *.scheme ∈ < http https >; - if (!$url.scheme) { - run « xdg-open "$target" »; +#| 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; } - given $url.scheme.fc { - when 'http'|'https' { - given $url.hostname.fc { - when *.ends-with('aws.amazon.com') { run « chromium-browser "$url" » } - when *.ends-with('google.com') { run « chromium-browser "$url" » } - when *.ends-with('slack.com') { run « chromium-browser "$url" » } - when *.ends-with('zaaksysteem.net') { run « chromium-browser "$url" » } - when *.ends-with('zaaksysteem.nl') { run « chromium-browser "$url" » } - default { run « "%*ENV<BROWSER>" "$url" » } - } - } - default { - run « xdg-open "$url" »; - } - } + 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('aws.amazon.com')) { "chromium-browser '$t'" } +multi sub cmd (HttpUrl $t where *.hostname.ends-with('google.com')) { "chromium-browser '$t'" } +multi sub cmd (HttpUrl $t where *.hostname.ends-with('slack.com')) { "chromium-browser '$t'" } +multi sub cmd (HttpUrl $t where *.hostname.ends-with('zaaksysteem.net')) { "chromium-browser '$t'" } +multi sub cmd (HttpUrl $t where *.hostname.ends-with('zaaksysteem.nl')) { "chromium-browser '$t'" } + +# 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 }) { "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'" } diff --git a/.local/bin/xdg-open b/.local/bin/xdg-open new file mode 120000 index 0000000..ce4a72b --- /dev/null +++ b/.local/bin/xdg-open @@ -0,0 +1 @@ +open
\ No newline at end of file |