/src/colorize/core.clj

https://github.com/ibdknox/colorize · Clojure · 61 lines · 52 code · 9 blank · 0 comment · 2 complexity · 83fb451f7f9f1dd532d54bc842bd050b MD5 · raw file

  1. (ns colorize.core
  2. "A set of functions to wrap strings in ansi-colors."
  3. (:use [clojure.pprint :only [pprint]]))
  4. (def ansi-colors {:reset "[0m"
  5. :default "[39m"
  6. :white "[37m"
  7. :black "[30m"
  8. :red "[31m"
  9. :green "[32m"
  10. :blue "[34m"
  11. :yellow "[33m"
  12. :magenta "[35m"
  13. :cyan "[36m"
  14. :black-bg "[40m"
  15. :red-bg "[41m"
  16. :green-bg "[42m"
  17. :yellow-bg "[43m"
  18. :blue-bg "[44m"
  19. :magenta-bg "[45m"
  20. :cyan-bg "[46m"
  21. :white-bg "[47m"
  22. :bold "[1m"
  23. :italic "[3m"
  24. :underline "[4m"
  25. :inverse "[7m"
  26. :strikethrough "[9m"})
  27. (defn ansi
  28. "Get the ansi code for a specific color"
  29. [code]
  30. (str \u001b (get ansi-colors code (:reset ansi-colors))))
  31. (defn color
  32. "Wrap the given strings in the provided color. Color should be
  33. a keyword and can be any of the following:
  34. [:reset :default :white :black :red :green :blue :yellow :magenta :cyan
  35. :black-bg :red-bg :green-bg :yellow-bg :blue-bg :magenta-bg :cyan-bg :white-bg
  36. :underline :italic :bold :strikethrough :inverse].
  37. Each of these also has a function created for it: (cyan \"woohoo\")"
  38. [code & s]
  39. (str (ansi code) (apply str s) (ansi :reset)))
  40. (defn show-all []
  41. (let [all-colors (apply juxt (for [cur (keys ansi-colors)]
  42. (fn [input]
  43. [cur (color cur input)])))]
  44. (pprint (into (sorted-map) (all-colors "test")))))
  45. (defmacro create-colors []
  46. (apply list 'do
  47. (for [k (keys ansi-colors)]
  48. (let [code (ansi k)
  49. reset (ansi :reset)]
  50. `(defn ~(symbol (name k)) [& s#]
  51. (str ~code (apply str s#) ~reset))))))
  52. (create-colors)