Search code examples
clojurecommand-line-interface

Clojure: how to tell if out is going to console or is being piped?


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.


Solution

  • 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.