branch: elpa/buttercup commit e71a40f1ffef4847df28c9d4ad7edc1e360ee52a Author: Ola Nilsson <ola.nils...@gmail.com> Commit: Ola Nilsson <ola.nils...@gmail.com>
Make sure carriage movement characters are not colorized Fixes #171, which reports that adding `ansi-color-apply-on-region' to `compilation-filter-hook' did not work with colorized buttercup output. This was because in the `compilation-filter' function, carriage movement characters '\r' and '\n' are handled (by `comint-carriage-motion') before `compilation-filter-hook'. It is often recommended to use ;; either `point' or `point-max' work as the second argument (ansi-color-apply-on-region compilation-filter-start (point)) in a `compilation-filter-hook' function to handle ANSI or SGR control sequences in compilation output. But carriage motion may cause `point' to move to before `compilation-filter-start'. Then `ansi-color-apply-on-region' will miss SGR control sequences in the `point' - `compilation-filter-start' range. Using (ansi-color-apply-on-region (save-excursion (goto-char compilation-filter-start) (line-beginning-position)) (point)))) instead will at least avoid this problem for carriage motion that does not move point to preceding lines. --- buttercup.el | 35 ++++++++++++++++++++++++++--------- 1 file changed, 26 insertions(+), 9 deletions(-) diff --git a/buttercup.el b/buttercup.el index eeaf5d3..bbbf3fc 100644 --- a/buttercup.el +++ b/buttercup.el @@ -1504,8 +1504,18 @@ Do not change the global value.") (with-current-buffer buttercup-warning-buffer-name (when (string-match-p "[^[:space:]\n\r]" (buffer-string)) (buttercup--print + "%s\n" (buttercup-colorize - (buffer-string) + ;; Any terminating newline in the buffer should not be + ;; colorized. It would mess up color handling in Emacs + ;; compilation buffers using + ;; `ansi-color-apply-on-region' in + ;; `compilation-filter-hook'. + (buffer-substring (point-min) + (save-excursion + (goto-char (1- (point-max))) + (if (looking-at-p "\n") + (point) (point-max)))) 'yellow))))) (when (get-buffer buttercup-warning-buffer-name) (kill-buffer buttercup-warning-buffer-name)) @@ -1653,22 +1663,29 @@ EVENT and ARG are described in `buttercup-reporter'." (unless (string-match-p "[\n\v\f]" (buttercup-spec-description arg)) (buttercup-reporter-batch event arg))) (`spec-done + ;; Carriage returns (\r) should not be colorized. It would mess + ;; up color handling in Emacs compilation buffers using + ;; `ansi-color-apply-on-region' in `compilation-filter-hook'. (pcase (buttercup-spec-status arg) (`passed - (buttercup--print (buttercup-colorize "\r%s" 'green) - (buttercup--indented-description arg))) + (buttercup--print + "\r%s" (buttercup-colorize (buttercup--indented-description arg) 'green))) (`failed - (buttercup--print (buttercup-colorize "\r%s FAILED" 'red) - (buttercup--indented-description arg)) + (buttercup--print + "\r%s" (buttercup-colorize + (concat (buttercup--indented-description arg) " FAILED") + 'red)) (setq buttercup-reporter-batch--failures (append buttercup-reporter-batch--failures (list arg)))) (`pending (if (equal (buttercup-spec-failure-description arg) "SKIPPED") (buttercup--print " %s" (buttercup-spec-failure-description arg)) - (buttercup--print (buttercup-colorize "\r%s %s" 'yellow) - (buttercup--indented-description arg) - (buttercup-spec-failure-description arg)))) + (buttercup--print + "\r%s" (buttercup-colorize + (concat (buttercup--indented-description arg) " " + (buttercup-spec-failure-description arg)) + 'yellow)))) (_ (error "Unknown spec status %s" (buttercup-spec-status arg)))) (buttercup--print " (%s)\n" (buttercup-elapsed-time-string arg))) @@ -1780,7 +1797,7 @@ the capturing behavior." (defun buttercup-colorize (string color) "Format STRING with COLOR." (let ((color-code (cdr (assoc color buttercup-colors)))) - (format "\u001b[%sm%s\u001b[0m" color-code string))) + (format "\e[%sm%s\e[0m" color-code string))) (defun buttercup-reporter-interactive (event arg) "Reporter for interactive sessions.