Skip to content

Instantly share code, notes, and snippets.

@escherize
Created April 30, 2025 16:15
Show Gist options
  • Save escherize/3e5db9119dfd376eb9244c6128df838c to your computer and use it in GitHub Desktop.
Save escherize/3e5db9119dfd376eb9244c6128df838c to your computer and use it in GitHub Desktop.
(ns mage.color
(:require
[clojure.string :as str]
[clojure.walk :as w]))
(def ^:dynamic *disable-colors*
"If set to true, all color functions will return the input string as is."
false)
(def ^:private reset
(str "\033[" 0 "m"))
(defn- do-color [color-code args]
(if (or *disable-colors* (System/getenv "NO_COLOR"))
(apply str args)
(str (str/join (map #(str color-code %) args)) reset)))
(defn- bold "Wrap a string with code to make it bold then resets it."
[& args] (do-color "" args))
(defn- dark "Wrap a string with code to make it dark then resets it."
[& args] (do-color "" args))
(defn- underline "Wrap a string with code to make it underline then resets it."
[& args] (do-color "" args))
(defn- blink "Wrap a string with code to make it blink then resets it."
[& args] (do-color "" args))
(defn- reverse-color "Wrap a string with code to make it reverse-color then resets it."
[& args] (do-color "" args))
(defn- concealed "Wrap a string with code to make it concealed then resets it."
[& args] (do-color "" args))
(defn- gray "Wrap a string with code to make it gray then resets it."
[& args] (do-color "" args))
(defn- grey "Wrap a string with code to make it grey then resets it."
[& args] (do-color "" args))
(defn- red "Wrap a string with code to make it red then resets it."
[& args] (do-color "" args))
(defn- green "Wrap a string with code to make it green then resets it."
[& args] (do-color "" args))
(defn- yellow "Wrap a string with code to make it yellow then resets it."
[& args] (do-color "" args))
(defn- blue "Wrap a string with code to make it blue then resets it."
[& args] (do-color "" args))
(defn- magenta "Wrap a string with code to make it magenta then resets it."
[& args] (do-color "" args))
(defn- cyan "Wrap a string with code to make it cyan then resets it."
[& args] (do-color "" args))
(defn- white "Wrap a string with code to make it white then resets it."
[& args] (do-color "" args))
(defn- on-grey "Wrap a string with code to make it on-grey then resets it."
[& args] (do-color "" args))
(defn- on-gray "Wrap a string with code to make it on-gray then resets it."
[& args] (do-color "" args))
(defn- on-red "Wrap a string with code to make it on-red then resets it."
[& args] (do-color "" args))
(defn- on-green "Wrap a string with code to make it on-green then resets it."
[& args] (do-color "" args))
(defn- on-yellow "Wrap a string with code to make it on-yellow then resets it."
[& args] (do-color "" args))
(defn- on-blue "Wrap a string with code to make it on-blue then resets it."
[& args] (do-color "" args))
(defn- on-magenta "Wrap a string with code to make it on-magenta then resets it."
[& args] (do-color "" args))
(defn- on-cyan "Wrap a string with code to make it on-cyan then resets it."
[& args] (do-color "" args))
(defn- on-white "Wrap a string with code to make it on-white then resets it."
[& args] (do-color "" args))
(def ^:private colors
{:bold bold :dark dark :underline underline :blink blink :reverse-color reverse-color :concealed concealed
:gray gray :grey grey :red red :green green :yellow yellow :blue blue :magenta magenta :cyan cyan :white white
:on-grey on-grey :on-gray on-gray :on-red on-red :on-green on-green :on-yellow on-yellow :on-blue on-blue
:on-magenta on-magenta :on-cyan on-cyan :on-white on-white})
(defn- ice* [args]
(w/prewalk
(fn [x]
(if-let [color-fn (and (vector? x)
(contains? colors (first x))
(colors (first x)))]
(apply color-fn (ice* (into [] (rest x))))
x))
args))
(defn ice
"Iced out println: Takes strings and vector of strings and colors, and prints them out with the specified colors.
Example:
(ice [:red 1 [:blue 2 [:green 3] 2] 1])
;; =prints=> 12321
;; where 1's are red, 2's are blue and 3 is green."
[args] (println (ice* args)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment