Skip to content

Instantly share code, notes, and snippets.

@borkdude
Last active April 9, 2024 21:21
Show Gist options
  • Save borkdude/82dcdd36a1e61ef36f19221876e7b1b6 to your computer and use it in GitHub Desktop.
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