Skip to content

Commit

Permalink
Add a better check for running under nREPL
Browse files Browse the repository at this point in the history
  • Loading branch information
hlship committed Oct 20, 2022
1 parent 30392ef commit 8d9337d
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions src/io/aviso/ansi.clj
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
(ns io.aviso.ansi
"Help with generating textual output that includes ANSI escape codes for formatting."
(:require [clojure.string :as str]
[clojure.walk :as walk]))
(:require [clojure.string :as str]))

(defn- is-ns-available? [sym]
(try
(require sym)
true
(catch Throwable _ false)))

(def ^:const ansi-output-enabled?
"Determine if ANSI output is enabled. If the environment variable ENABLE_ANSI_COLORS is non-null,
then it sets the value: the value `false` (matched caselessly) disables ANSI colors and fonts,
otherwise they are enabled.
Next, there is an attempt to determine if execution is currently inside an REPL environment,
possibly started from an IDE; a check is made to see if `nrepl.core` namespace is loaded;
possibly started from an IDE; a check is made to see if `nrepl.core` namespace is available;
if so, then ANSI colors are enabled.
This has been verified to work with Cursive, with `lein repl`, and with `clojure` (or `clj`).
Expand All @@ -27,7 +32,7 @@
(not (.equalsIgnoreCase value "false"))
(some?
(or
(contains? (set (loaded-libs)) 'nrepl.core)
(is-ns-available? 'nrepl.core)
(System/console)))))

(defmacro ^:private if-enabled?
Expand All @@ -43,6 +48,7 @@
;; select graphic rendition
(def ^:const sgr
"The Select Graphic Rendition suffix: m"

"m")

(def ^:const reset-font
Expand Down

0 comments on commit 8d9337d

Please sign in to comment.