Skip to content

Instantly share code, notes, and snippets.

@thiagokokada
Last active April 29, 2021 03:33
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 thiagokokada/854e6be42fe80afb946324c62d299ce8 to your computer and use it in GitHub Desktop.
Save thiagokokada/854e6be42fe80afb946324c62d299ce8 to your computer and use it in GitHub Desktop.
Python's os.isatty() function equivalent to Babashka
#!/usr/bin/env bb
(ns is-tty
(:require [babashka.process :as p]))
(defn- is-tty
[fd key]
(-> ["test" "-t" (str fd)]
(p/process {key :inherit :env {}})
deref
:exit
(= 0)))
(defn in-is-tty? [] (is-tty 0 :in))
(defn out-is-tty? [] (is-tty 1 :out))
(defn err-is-tty? [] (is-tty 2 :err))
(when (= *file* (System/getProperty "babashka.file"))
(println (in-is-tty?) (out-is-tty?) (err-is-tty?)))
;; $ bb is-tty.clj
;; true true true
;;
;; $ bb is-tty.clj </dev/null
;; false true true
;;
;; $ bb is-tty.clj 1>&2 >/dev/null
;; true false true
;;
;; $ bb is-tty.clj 2>/dev/null
;; true true false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment