Skip to content

Instantly share code, notes, and snippets.

@borkdude
Last active January 2, 2022 16:33
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save borkdude/82dcdd36a1e61ef36f19221876e7b1b6 to your computer and use it in GitHub Desktop.
vipe for babashka - see https://github.com/juliangruber/vipe
#!/usr/bin/env bash
# temp file
t=/tmp/bipe.$$.txt
touch $t
# read from stdin
if [ ! -t 0 ]; then
cat > $t
fi
# spawn babashka with stdio connected
rlwrap bb -e "
(def stdin (slurp \"$t\"))
(def res (atom stdin))
(.addShutdownHook (.getRuntime Runtime)
(Thread. (fn [] (spit \"$t\" @res))))
(binding [*1 *1
*2 *2
*3 *3]
(clojure.main/repl
:init (fn [] (println \"Evaluate 'stdin' to see piped input.\")
(println \"The last return value is piped to the next command.\")
(println \"Use :repl/quit to quit the REPL.\"))
:eval (fn [x] (let [x (eval x)]
(reset! res x)
;; this should not be part of the eval function and should be fixed in bb's clojure.main/repl
;; similar for *e
(set! *3 *2)
(set! *2 *1)
(set! *1 x)
x))))
(spit \"$t\" @res)
" < /dev/tty > /dev/tty || exit $?
# write to stdout
cat $t
# cleanup
rm $t
@borkdude
Copy link
Author

borkdude commented Nov 30, 2021

Example usage:

❯ brew info --json=v1 --installed | ./bipe | xargs which
Evaluate 'stdin' to see piped input.
The last return value is piped to the next command.
Use :repl/quit to quit the REPL.
user=> (-> (json/parse-string stdin) rand-nth (get "name"))
"git"
user=> ^D
/usr/local/bin/git

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment