Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to detect if stdout is a terminal #1299

Open
justone opened this issue Jun 17, 2022 · 1 comment
Open

Add ability to detect if stdout is a terminal #1299

justone opened this issue Jun 17, 2022 · 1 comment

Comments

@justone
Copy link
Contributor

justone commented Jun 17, 2022

Is your feature request related to a problem? Please describe.

I was excited by the new jet features that revealed it was possible from Graalvm to detect if stdout is a terminal. I immediately thought this same feature would allow babashka scripts that emit colored output to remove the colors if their output was not directly to the terminal.

Describe the solution you'd like

Perhaps a function called isatty that you can call like this:

(if (isatty *out)
  (println with-colors)
  (println plain))

I don't know which namespace this function should belong to. Perhaps babashka.process.

Describe alternatives you've considered

I was able to achieve a similar detection by shelling out to bash:

#!/usr/bin/env bb

(require '[babashka.process :as process])

(defn isatty
  []
  (try (process/check (process/$ {:inherit true} bash -c "if [ ! -t 1 ]; then exit 1; fi"))
       true
       (catch Exception _e
         false)))

(if (isatty)
  (println "tty")
  (println "pipe"))
@borkdude
Copy link
Collaborator

Another alternative is using (System/console) which returns nil if either stdin/stdout/stderr is not connected to a terminal.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants