-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract fzf args from map to arg for thread-last
- To allow for threading (thread-last) of fzf input args, move the args from `in`-key in opt map to a single arg - Validate args with spec
- Loading branch information
Showing
4 changed files
with
82 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,38 @@ | ||
(ns fzf.impl | ||
"Implementation of fzf-wrapper" | ||
(:require [babashka.process :as p] | ||
[babashka.fs :as fs] | ||
(:require [babashka.fs :as fs] | ||
[babashka.process :as p] | ||
[clojure.string :as str])) | ||
|
||
(defn parse-opts | ||
"Parse fzf and process options" | ||
([] (parse-opts {})) | ||
([opts] | ||
(let [{:keys [in dir multi preview tac case-insensitive exact reverse height] | ||
{:keys [header-str header-lines header-first]} :header} opts] | ||
{:cmd (cond-> ["fzf"] | ||
multi (conj "--multi") | ||
reverse (conj "--reverse") | ||
tac (conj "--tac") | ||
case-insensitive (conj "-i") | ||
exact (conj "--exact") | ||
preview (conj "--preview" preview) | ||
header-str (conj "--header" header-str) | ||
header-lines (conj "--header-lines" header-lines) | ||
header-first (conj "--header-first") | ||
height (conj "--height" height)) | ||
:opts (cond-> {:in :inherit | ||
:out :string | ||
:err :inherit} | ||
in (assoc :in (str/join "\n" in)) | ||
dir (assoc :dir (-> dir fs/expand-home str)))}))) | ||
[opts args] | ||
(let [{:keys [dir multi preview tac case-insensitive exact reverse height] | ||
{:keys [header-str header-lines header-first]} :header} opts] | ||
{:cmd (cond-> ["fzf"] | ||
multi (conj "--multi") | ||
reverse (conj "--reverse") | ||
tac (conj "--tac") | ||
case-insensitive (conj "-i") | ||
exact (conj "--exact") | ||
preview (conj "--preview" preview) | ||
header-str (conj "--header" header-str) | ||
header-lines (conj "--header-lines" header-lines) | ||
header-first (conj "--header-first") | ||
height (conj "--height" height)) | ||
:opts (cond-> {:in :inherit | ||
:out :string | ||
:err :inherit} | ||
(not-empty args) (assoc :in (str/join "\n" args)) | ||
dir (assoc :dir (-> dir fs/expand-home str)))})) | ||
|
||
(defn fzf | ||
"Internal interface to fzf" | ||
([] (fzf {})) | ||
([opts] (let [multi (:multi opts) | ||
{:keys [cmd opts]} (parse-opts opts) | ||
{:keys [out exit]} @(p/process cmd opts)] | ||
(if (zero? exit) | ||
(cond-> (str/trim out) | ||
multi str/split-lines) | ||
nil)))) | ||
[opts args] | ||
(let [multi (:multi opts) | ||
{:keys [cmd opts]} (parse-opts opts args) | ||
{:keys [out exit]} @(p/process cmd opts)] | ||
(if (zero? exit) | ||
(cond-> (str/trim out) | ||
multi str/split-lines) | ||
nil))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters