I'm writing a clojure cli and would like to know if there is a way to test if the out (i.e. println) is being written to a console or is being piped to another program?
This is similar to this question but for clojure.
I'm writing a clojure cli and would like to know if there is a way to test if the out (i.e. println) is being written to a console or is being piped to another program?
This is similar to this question but for clojure.
Clojure is hosted language, so system interaction related stuff is more or less equivalent to Java. For Java there exists only partial solution described in this answer. You can of course implement isatty()
using JNI and then interop from Clojure.
However, from ClojureScript hosted on Node.js it's easily achievable using process.stdin.isTTY
(in ClojureScript would be (-> process .-stdin .-isTTY)
). More details are in this answer.
You could use jnr-posix Java library
[com.github.jnr/jnr-posix "3.0.10"]
to call native posix methods directly from Clojure (using Java Interop):
(import 'jnr.posix.POSIXFactory)
(def posix (POSIXFactory/getPOSIX))
(.isatty posix java.io.FileDescriptor/out)
N.B. If you'll run you Clojure application using lein run
command, Clojure will not be able to identify TTY terminal since lein
will internally pipe stdio
between two java processes. You could avoid internal piping by using lein trampoline run
command or compiled jar file.