Liking cljdoc? Tell your friends :D

jansi-clj

jansi-clj is a Clojure wrapper around jansi, a library that handles ANSI escape sequences (e.g. for terminal colors, cursor movement, ...) on a per-OS basis, proclaiming that it "even works on windows".

Build Status Clojars Project

This library provides a dead-simple way of enabling ANSI support for all terminal emulators supported by jansi, as well as functions to produce those ANSI sequences.

Auto-Enable ANSI Codes

If you need ANSI support for different platforms but don't want to replace the code that actually generates the escape sequences, just require the namespace jansi-clj.auto.

(require 'jansi-clj.auto)

That's it. The standard output streams (as well as Clojure's *out* and *err* writers) will be wrapped in a platform-/terminal-specific way to provide correct handling of ANSI codes. This means that you can make any such console output portable without having to touch any existing code.

Note: Wrapping the streams currently seems not to work correctly when done within a JVM spun up by Leiningen since stdout/stderr are then redirected and Jansi cannot detect that there is a color-enabled terminal at their ends. This means that escape sequences will be filtered out when produced within lein repl or lein run. (lein trampoline run and executing an ubjerar should work, though.)

The choice of which colorization library to use is completely yours. But since you're already here...

Formatting Terminal Output

(require '[jansi-clj.core :refer :all])

Colors

(colors)
;; => (:black :default :magenta :white :red :blue :green :yellow :cyan)

For each color, there exist four functions, e.g. red (create a string with red foreground), red-bright (create a string with bright red foreground), as well as red-bg (red background) and red-bg-bright.

(println "ERROR:" (red "This" " is " "a message."))
;; ERROR: This is a message.
;; => nil

As you can see, the functions behave like str regarding the concatenation of the parts.

Attributes

(attributes)
;; => (:underline-double :no-negative :no-underline :blink-fast :no-strikethrough
;;     :conceal :negative :no-italic :italic :faint :no-conceal :no-bold :no-blink
;;     :strikethrough :blink-slow :bold :underline)

For each of these keywords there exists a respective function that provides the desired formatting.

(println (bold (red "ERROR")))
;; ERROR
;; => nil

render

Jansi offers a special render syntax that can be used via jansi-clj.core/render:

;; syntax: "@|code(,code)* text|@"
(render "@|green,bold Success!|@ (Code: 0)")

There is renderf that accepts a format string as its first parameter:

(renderf "@|green,bold %s|@ (Code: %d)" "Sucess!" 0)

Others

There is a variety of other functions that can be used. You can generate the respective documentation using codox:

$ lein doc

Globally enable/disable ANSI Codes

The two functions jansi-clj.core/enable! and jansi-clj.core/disable! can be used to prevent the different escape sequences from showing up in the generated strings in the first place.

License

Copyright © 2014-2016 Yannick Scherer

Distributed under the Eclipse Public License either version 1.0 or (at your option) any later version.

Can you improve this documentation?Edit on GitHub

cljdoc is a website building & hosting documentation for Clojure/Script libraries

× close