diff --git a/src/fzf/core.clj b/src/fzf/core.clj index 831d591..74d3dbd 100644 --- a/src/fzf/core.clj +++ b/src/fzf/core.clj @@ -23,6 +23,7 @@ (s/def :fzf/tac boolean?) (s/def :fzf/case-insensitive boolean?) (s/def :fzf/exact boolean?) +(s/def :fzf/throw boolean?) (s/def :fzf/opts (s/and (s/keys @@ -36,7 +37,8 @@ :fzf/height :fzf/tac :fzf/case-insensitive - :fzf/exact]) + :fzf/exact + :fzf/throw]) #(not (and (:preview %) (:preview-fn %))))) (s/def :fzf/args sequential?) @@ -61,6 +63,7 @@ - tac: Bool, reverse the order of the input - case-insensitive: Bool, toggle case-insensitive search (default: smart-case) - exact: Bool, toggle exact search (default: fuzzy) + - throw: Bool, throw when no candidates were selected (default: return nil) `args`: Input arguments to fzf (optional, list of strings) diff --git a/src/fzf/impl.clj b/src/fzf/impl.clj index 4d3eb50..2335bea 100644 --- a/src/fzf/impl.clj +++ b/src/fzf/impl.clj @@ -90,9 +90,11 @@ (start-preview-fn-server port-promise preview-fn) (ByteArrayOutputStream.))] ; any proper Closeable thing will do. (let [multi (:multi opts) + throw-on-nzec (:throw opts) {:keys [cmd opts]} (parse-opts opts args (when preview-fn @port-promise)) {:keys [out exit]} @(p/process cmd opts)] (if (zero? exit) (cond-> (str/trim out) - multi str/split-lines) - nil))))) + multi str/split-lines) + (if throw-on-nzec (throw (ex-info "No candidates were selected" {:babashka/exit 1})) + nil)))))) diff --git a/test/fzf/impl_test.clj b/test/fzf/impl_test.clj index 3bc8f96..915db3b 100644 --- a/test/fzf/impl_test.clj +++ b/test/fzf/impl_test.clj @@ -17,7 +17,8 @@ :header {:header-str "header-text" :header-lines 2 :header-first true} - :height "10%"} + :height "10%" + :throw true} []))))) (t/testing "adding :preview-fn produces command string with inline bb netcat script" (t/is (= ["fzf" "--preview" (i/bbnc-preview-command 12345)]