From 69e58440abde5015e831c12e95440e0c61e79af4 Mon Sep 17 00:00:00 2001 From: Thiago Kenji Okada Date: Sat, 15 May 2021 17:10:52 -0300 Subject: [PATCH] Improve `clojure.java.browse` (#846) * Remove xdg-open hardcoded path in impl.clojure.java.browse Binaries in Linux are not guarantee to be located on /usr/bin. For example, NixOS the binaries are located in /run/current-system/sw/bin/xdg-open (that is actually just a symlink to /nix/store). The distro may also not have xdg-open installed at /, instead the user may put it somewhere else like $HOME/bin. This commit fixes this by using not hardcoding the PATH of xdg-open, instead relying on shell path to search it. * Implement clojure.java.browse/*open-url-script* This allows usage of a custom URL script, like: ```clojure (binding [*open-url-script* (atom "my-browse-url-script")] (browse-url "https://google.com")) ``` *open-url-script* is an atom just to keep compatibility with Clojure. Described here: https://clojuredocs.org/clojure.java.browse/*open-url-script* --- src/babashka/impl/clojure/java/browse.clj | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/src/babashka/impl/clojure/java/browse.clj b/src/babashka/impl/clojure/java/browse.clj index 13f053ef..0c51da74 100644 --- a/src/babashka/impl/clojure/java/browse.clj +++ b/src/babashka/impl/clojure/java/browse.clj @@ -1,7 +1,10 @@ (ns babashka.impl.clojure.java.browse {:no-doc true} (:require [clojure.java.shell :refer [sh]] - [clojure.string :as str])) + [clojure.string :as str] + [sci.core :as sci])) + +(def open-url-script (sci/new-dynamic-var '*open-url-script* (atom nil))) (def os (let [os-name (System/getProperty "os.name") @@ -15,11 +18,13 @@ (defn browse-url [url] (let [url (str url)] - (case os - :mac (sh "/usr/bin/open" url) - :linux (sh "/usr/bin/xdg-open" url) - :windows (sh "cmd" "/C" "start" url)))) + (if-let [script (-> open-url-script deref deref)] + (sh script url) + (case os + :mac (sh "/usr/bin/open" url) + :linux (sh "xdg-open" url) + :windows (sh "cmd" "/C" "start" url))))) (def browse-namespace - {'browse-url browse-url}) - + {'*open-url-script* open-url-script + 'browse-url browse-url})