Skip to content

Commit

Permalink
Autodetect color (#20)
Browse files Browse the repository at this point in the history
* fix: use keyword for default value

Parsing doesn't happen on default values, so we must provide the
correct format default value.

* feat: autodetect color

We can now automatically detect if we should use color when printing.

Mostly color will be set to off when cq is used in a pipe, because a
console is not available. This can be forced on with the command-line flags.

* chore: bump org.clojure/tools.cli to the latest version
  • Loading branch information
Macroz authored Oct 31, 2022
1 parent f8c8c4f commit 9cece7f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
2 changes: 1 addition & 1 deletion deps.edn
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
org.clojure/data.csv {:mvn/version "1.0.0"}
org.clojure/data.xml {:mvn/version "0.2.0-alpha6"}
org.clojure/data.json {:mvn/version "1.1.0"}
org.clojure/tools.cli {:mvn/version "1.0.206"}
org.clojure/tools.cli {:mvn/version "1.0.214"}
org.clojure/tools.reader {:mvn/version "1.3.4"}
org.babashka/sci {:mvn/version "0.3.2"}
camel-snake-kebab/camel-snake-kebab {:mvn/version "0.4.2"}
Expand Down
15 changes: 13 additions & 2 deletions src/cq/main.clj
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
["-p" "--[no-]pretty" "Pretty print output - default is true"
:default true]
[nil "--color COLOR" (str "When pretty printing, whether to use colors: " colors-str " - default is auto")
:default "auto"
:default :auto
:parse-fn keyword
:validate [colors]]
["-c" nil "Same as --color=on"
Expand Down Expand Up @@ -110,9 +110,20 @@
(def ^:dynamic *stdin* System/in)
(def ^:dynamic *stdout* System/out)

(defn- autodetect-color
"Autodetects whether ANSI color should be enabled.
REPLs in general support colors, but may not use a console.
JVM gives us the `System/console`, if we have a console,
and not a plain pipe."
[]
(or (contains? (set (loaded-libs)) 'nrepl.core)
(System/console)))

(defn- handle-auto-options [opts]
(update opts :color #(case %
:auto true ; would be nice to detect
:auto (autodetect-color)
:on true
false)))

Expand Down

0 comments on commit 9cece7f

Please sign in to comment.