Last active
April 29, 2021 03:33
-
-
Save thiagokokada/854e6be42fe80afb946324c62d299ce8 to your computer and use it in GitHub Desktop.
Python's os.isatty() function equivalent to Babashka
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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