Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
ClojureCLR println with colors in Windows cmd.exe
(assembly-load "ClojureClrEx")
(ns clync.core
(:use [clojure.clr.pinvoke :only [dllimports]]))
(GetStdHandle IntPtr [Int32])
(SetConsoleTextAttribute Boolean [IntPtr UInt32]))
(def colors
{:blue 1
:green 2
:cyan 3
:red 4
:pink 5
:yellow 6
:light-gray 7
:dark-gray 8
:bright-blue 9
:bright-green 10
:bright-cyan 11
:bright-red 12
:bright-pink 13
:bright-yellow 14
:white 15})
(def ^:dynamic *cur-color*)
(defn set-color [color]
(when-let [color-code (colors color)]
(set! *cur-color* color)
(SetConsoleTextAttribute (GetStdHandle (int -11)) (uint color-code))))
(declare cprint*)
(defn cprint-color [sexp]
(let [prev-color *cur-color*]
(set-color (second sexp))
(cprint* (drop 2 sexp))
(set-color prev-color)))
(defn cprint* [sexp]
(loop [elems sexp]
(when-let [elem (first elems)]
(condp = (type elem)
System.String (print elem "")
clojure.lang.PersistentList (cprint* elem)
(if (= (type elem) clojure.lang.Symbol)
(condp = elem
(symbol "color") (cprint-color sexp))
(recur (rest elems))))))
(defmacro cprint [& body]
(println body)
(binding [*cur-color* :light-gray]
(cprint* body)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment