From f71721fa68684c9adf2ae1d2d2d5c95ad6824afb Mon Sep 17 00:00:00 2001 From: Patrick Spek Date: Mon, 17 Feb 2020 18:41:43 +0100 Subject: Rewrite open command --- .local/bin/open | 77 +++++++++++++++++++++++++++++++++++++++-------------- .local/bin/xdg-open | 1 + 2 files changed, 58 insertions(+), 20 deletions(-) create mode 120000 .local/bin/xdg-open (limited to '.local/bin') 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" "$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 '$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 -- cgit v1.1