Skip to content

Commit

Permalink
Add ansi-apply.
Browse files Browse the repository at this point in the history
Can be used to apply color when you have name of color.
  • Loading branch information
rejeep committed Aug 24, 2013
1 parent df2225f commit 57d0561
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 33 deletions.
6 changes: 6 additions & 0 deletions README.markdown
Expand Up @@ -55,6 +55,12 @@ as the above.
(blink "bar"))
```

You can also use `ansi-apply` if you only have the name of the color:

```lisp
(ansi-apply 'red "foo %s" "bar")
```

### Nesting

Lets say you want to make a text bold, red and blinking. You can do
Expand Down
77 changes: 44 additions & 33 deletions ansi.el
Expand Up @@ -79,14 +79,19 @@
(defun ansi--concat (&rest sequences)
(apply 's-concat (-select 'stringp sequences)))

(defmacro ansi--define (scope effect)
(defun ansi--code (effect)
"Return code for EFFECT."
(or
(cdr (assoc effect ansi-colors))
(cdr (assoc effect ansi-on-colors))
(cdr (assoc effect ansi-styles))))

(defmacro ansi--define (effect)
"Define ansi function with EFFECT."
(let ((fn-name (intern (format "ansi-%s" (symbol-name effect)))))
`(defun ,fn-name (string &rest objects)
,(format "Add '%s' ansi effect to string." effect)
(let ((code (cdr (assoc ',effect ,scope)))
(formatted (apply 'format (cons string objects))))
(format "\e[%dm%s\e[%sm" code formatted ,ansi-reset)))))
`(defun ,fn-name (format-string &rest objects)
,(format "Add '%s' ansi effect to text." effect)
(apply 'ansi-apply (cons ',effect (cons format-string objects))))))

(defmacro with-ansi (&rest body)
"In this block shortcut names (without ansi- prefix) can be used."
Expand All @@ -101,35 +106,41 @@
(-map 'car ansi-styles)))
,(cons 'ansi--concat body)))

(defun ansi-apply (effect format-string &rest objects)
"Add EFFECT to text."
(let ((code (ansi--code effect))
(text (apply 'format format-string objects)))
(format "\e[%dm%s\e[%sm" code text ansi-reset)))



(ansi--define ansi-colors black)
(ansi--define ansi-colors red)
(ansi--define ansi-colors green)
(ansi--define ansi-colors yellow)
(ansi--define ansi-colors blue)
(ansi--define ansi-colors magenta)
(ansi--define ansi-colors cyan)
(ansi--define ansi-colors white)

(ansi--define ansi-on-colors on-black)
(ansi--define ansi-on-colors on-red)
(ansi--define ansi-on-colors on-green)
(ansi--define ansi-on-colors on-yellow)
(ansi--define ansi-on-colors on-blue)
(ansi--define ansi-on-colors on-magenta)
(ansi--define ansi-on-colors on-cyan)
(ansi--define ansi-on-colors on-white)

(ansi--define ansi-styles bold)
(ansi--define ansi-styles dark)
(ansi--define ansi-styles italic)
(ansi--define ansi-styles underscore)
(ansi--define ansi-styles blink)
(ansi--define ansi-styles rapid)
(ansi--define ansi-styles contrary)
(ansi--define ansi-styles concealed)
(ansi--define ansi-styles strike)
(ansi--define black)
(ansi--define red)
(ansi--define green)
(ansi--define yellow)
(ansi--define blue)
(ansi--define magenta)
(ansi--define cyan)
(ansi--define white)

(ansi--define on-black)
(ansi--define on-red)
(ansi--define on-green)
(ansi--define on-yellow)
(ansi--define on-blue)
(ansi--define on-magenta)
(ansi--define on-cyan)
(ansi--define on-white)

(ansi--define bold)
(ansi--define dark)
(ansi--define italic)
(ansi--define underscore)
(ansi--define blink)
(ansi--define rapid)
(ansi--define contrary)
(ansi--define concealed)
(ansi--define strike)

(provide 'ansi)

Expand Down
8 changes: 8 additions & 0 deletions test/ansi-test.el
Expand Up @@ -18,3 +18,11 @@
(with-ansi nil)
(with-ansi 0)
(with-ansi '(a)))

(ert-deftest test-ansi-apply ()
(should (equal (ansi-black "foo %s" "bar")
(ansi-apply 'black "foo %s" "bar")))
(should (equal (ansi-on-black "foo %s" "bar")
(ansi-apply 'on-black "foo %s" "bar")))
(should (equal (ansi-bold "foo %s" "bar")
(ansi-apply 'bold "foo %s" "bar"))))

0 comments on commit 57d0561

Please sign in to comment.