Skip to content

Instantly share code, notes, and snippets.

@tylerw
Forked from borkdude/bipe
Created November 30, 2021 20:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tylerw/f7cd2fa1b0cf1fcaa74ef5804819228a to your computer and use it in GitHub Desktop.
Save tylerw/f7cd2fa1b0cf1fcaa74ef5804819228a 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment