├── .gitignore ├── README.md ├── project.clj ├── src └── colorize │ └── core.clj └── test └── colorize └── test └── core.clj /.gitignore: -------------------------------------------------------------------------------- 1 | pom.xml 2 | *jar 3 | /lib/ 4 | /classes/ 5 | .lein-failures 6 | .lein-deps-sum 7 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # colorize 2 | 3 | A simple library to color string output with ansi-codes. 4 | 5 | ## Usage 6 | 7 | ```clojure 8 | (use 'colorize.core) 9 | 10 | (color :red "hey " "what's up") 11 | (red "hi!") 12 | (cyan "what's " (green "going on?")) 13 | (cyan-bg "blah") 14 | (underline (bold "cool")) 15 | 16 | ;;Available commands: 17 | [reset default white black red green blue yellow magenta cyan 18 | black-bg red-bg green-bg yellow-bg blue-bg magenta-bg cyan-bg white-bg 19 | underline italic bold strikethrough inverse] 20 | ``` 21 | 22 | ## License 23 | 24 | Copyright (C) 2011 Chris Granger 25 | 26 | Distributed under the Eclipse Public License, the same as Clojure. 27 | -------------------------------------------------------------------------------- /project.clj: -------------------------------------------------------------------------------- 1 | (defproject colorize "0.1.1" 2 | :description "Colorize console output" 3 | :dependencies [[org.clojure/clojure "[1.2.1],[1.3.0]"]]) 4 | -------------------------------------------------------------------------------- /src/colorize/core.clj: -------------------------------------------------------------------------------- 1 | (ns colorize.core 2 | "A set of functions to wrap strings in ansi-colors." 3 | (:use [clojure.pprint :only [pprint]])) 4 | 5 | (def ansi-colors {:reset "[0m" 6 | :default "[39m" 7 | :white "[37m" 8 | :black "[30m" 9 | :red "[31m" 10 | :green "[32m" 11 | :blue "[34m" 12 | :yellow "[33m" 13 | :magenta "[35m" 14 | :cyan "[36m" 15 | :black-bg "[40m" 16 | :red-bg "[41m" 17 | :green-bg "[42m" 18 | :yellow-bg "[43m" 19 | :blue-bg "[44m" 20 | :magenta-bg "[45m" 21 | :cyan-bg "[46m" 22 | :white-bg "[47m" 23 | :bold "[1m" 24 | :italic "[3m" 25 | :underline "[4m" 26 | :inverse "[7m" 27 | :strikethrough "[9m"}) 28 | 29 | (defn ansi 30 | "Get the ansi code for a specific color" 31 | [code] 32 | (str \u001b (get ansi-colors code (:reset ansi-colors)))) 33 | 34 | (defn color 35 | "Wrap the given strings in the provided color. Color should be 36 | a keyword and can be any of the following: 37 | 38 | [:reset :default :white :black :red :green :blue :yellow :magenta :cyan 39 | :black-bg :red-bg :green-bg :yellow-bg :blue-bg :magenta-bg :cyan-bg :white-bg 40 | :underline :italic :bold :strikethrough :inverse]. 41 | 42 | Each of these also has a function created for it: (cyan \"woohoo\")" 43 | [code & s] 44 | (str (ansi code) (apply str s) (ansi :reset))) 45 | 46 | (defn show-all [] 47 | (let [all-colors (apply juxt (for [cur (keys ansi-colors)] 48 | (fn [input] 49 | [cur (color cur input)])))] 50 | (pprint (into (sorted-map) (all-colors "test"))))) 51 | 52 | (defmacro create-colors [] 53 | (apply list 'do 54 | (for [k (keys ansi-colors)] 55 | (let [code (ansi k) 56 | reset (ansi :reset)] 57 | `(defn ~(symbol (name k)) [& s#] 58 | (str ~code (apply str s#) ~reset)))))) 59 | 60 | (create-colors) 61 | 62 | -------------------------------------------------------------------------------- /test/colorize/test/core.clj: -------------------------------------------------------------------------------- 1 | (ns colorize.test.core 2 | (:use [colorize.core]) 3 | (:use [clojure.test])) 4 | 5 | (deftest replace-me ;; FIXME: write 6 | (is false "No tests have been written.")) 7 | --------------------------------------------------------------------------------