Skip to content

Commit 357d5b9

Browse files
dpsuttonbbatsov
authored andcommitted
[Fix #1794] Flush ansi color context after printing (#1813)
1 parent 8c0f219 commit 357d5b9

File tree

3 files changed

+68
-22
lines changed

3 files changed

+68
-22
lines changed

cider-repl.el

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -526,6 +526,14 @@ Return the position of the prompt beginning."
526526
(set-marker cider-repl-prompt-start-mark prompt-start)
527527
prompt-start))))
528528

529+
(defun cider-repl--flush-ansi-color-context ()
530+
"Flush ansi color context after printing.
531+
When there is a possible unfinished ansi control sequence,
532+
`ansi-color-context` maintains this list."
533+
(when (and ansi-color-context (stringp (cadr ansi-color-context)))
534+
(insert-before-markers (cadr ansi-color-context))
535+
(setq ansi-color-context nil)))
536+
529537
(defun cider-repl--emit-output-at-pos (buffer string output-face position &optional bol)
530538
"Using BUFFER, insert STRING (applying to it OUTPUT-FACE) at POSITION.
531539
If BOL is non-nil insert at the beginning of line."
@@ -540,6 +548,7 @@ If BOL is non-nil insert at the beginning of line."
540548
(ansi-color-apply (propertize string
541549
'font-lock-face output-face
542550
'rear-nonsticky '(font-lock-face))))
551+
(cider-repl--flush-ansi-color-context)
543552
(when (and (= (point) cider-repl-prompt-start-mark)
544553
(not (bolp)))
545554
(insert-before-markers "\n")

test/cider-repl-tests.el

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,62 @@
6868
;; Javadoc: (javadoc java-object-or-class)
6969
;; Exit: <C-c C-q>
7070
;; Results: Stored in vars *1, *2, *3, an exception in *e;"))))
71+
72+
(defvar cider-testing-ansi-colors-vector
73+
["black" "red3" "green3" "yellow3" "blue2"
74+
"magenta3" "cyan3" "gray90"]
75+
"Vector of translations for ansi color codes")
76+
77+
(defmacro with-testing-ansi-table (colors &rest body)
78+
`(let* ((ansi-color-names-vector ,colors)
79+
(ansi-color-map (ansi-color-make-color-map)))
80+
,@body))
81+
82+
(describe "multiple calls to cider-repl--emit-output-at-pos"
83+
(it "Multiple emit output calls set properties and emit text"
84+
(with-temp-buffer
85+
(with-testing-ansi-table cider-testing-ansi-colors-vector
86+
(cider-repl-reset-markers)
87+
88+
(cider-repl--emit-output-at-pos (current-buffer) "a" 'cider-repl-stdout-face (point))
89+
(cider-repl--emit-output-at-pos (current-buffer) "b" 'cider-repl-stdout-face (point))
90+
(cider-repl--emit-output-at-pos (current-buffer) "c" 'cider-repl-stdout-face (point))
91+
(cider-repl--emit-output-at-pos (current-buffer) "d" 'cider-repl-stdout-face (point))
92+
93+
(expect (buffer-string) :to-equal "a\nb\nc\nd\n")
94+
(expect (get-text-property 1 'font-lock-face)
95+
:to-equal '(foreground-color . "black"))
96+
(expect (get-text-property 3 'font-lock-face)
97+
:to-equal 'cider-repl-stdout-face)
98+
(expect (get-text-property 5 'font-lock-face)
99+
:to-equal '(foreground-color . "red3"))
100+
(expect (get-text-property 7 'font-lock-face)
101+
:to-equal '(foreground-color . "red3"))))))
102+
103+
(defun simulate-cider-output (s property)
104+
"Return properties from cider-repl--emit-output-at-pos.
105+
PROPERTY shoudl be a symbol of either 'text, 'ansi-context or
106+
'properties."
107+
(with-temp-buffer
108+
(with-testing-ansi-table cider-testing-ansi-colors-vector
109+
(cider-repl-reset-markers)
110+
(cider-repl--emit-output-at-pos (current-buffer) s nil (point-min) nil))
111+
(case property
112+
('text (substring-no-properties (buffer-string)))
113+
('ansi-context ansi-color-context)
114+
('properties (substring (buffer-string))))))
115+
116+
(describe "cider-repl--emit-output-at-pos"
117+
(it "prints simple strings"
118+
(expect (simulate-cider-output "hi" 'text)
119+
:to-equal "hi\n"))
120+
121+
(it "when invlaid escape code, doesn't hold string looking for close tag"
122+
(expect (simulate-cider-output "\033hi" 'text)
123+
:to-equal "\033hi\n")
124+
(expect (simulate-cider-output "\033hi" 'ansi-context)
125+
:to-equal nil))
126+
127+
(it "preserves context when valid"
128+
(let ((context (simulate-cider-output "abcd" 'ansi-context)))
129+
(expect context :to-equal '((31) nil)))))

test/cider-util-tests.el

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -138,28 +138,6 @@
138138
(expect (cider-repl-prompt-abbreviated "some.pretty.long.namespace.name")
139139
:to-equal "s.p.l.n.name> ")))
140140

141-
(describe "cider-repl--emit-output-at-pos"
142-
(it "formats and inserts the text in the given buffer"
143-
(with-temp-buffer
144-
(let* ((ansi-color-names-vector ["black" "red3" "green3" "yellow3" "blue2" "magenta3" "cyan3" "gray90"])
145-
(ansi-color-map (ansi-color-make-color-map)))
146-
(cider-repl-reset-markers)
147-
148-
(cider-repl--emit-output-at-pos (current-buffer) "a" 'cider-repl-stdout-face (point))
149-
(cider-repl--emit-output-at-pos (current-buffer) "b" 'cider-repl-stdout-face (point))
150-
(cider-repl--emit-output-at-pos (current-buffer) "c" 'cider-repl-stdout-face (point))
151-
(cider-repl--emit-output-at-pos (current-buffer) "d" 'cider-repl-stdout-face (point))
152-
153-
(expect (buffer-string) :to-equal "a\nb\nc\nd\n")
154-
(expect (get-text-property 1 'font-lock-face)
155-
:to-equal '(foreground-color . "black"))
156-
(expect (get-text-property 3 'font-lock-face)
157-
:to-equal 'cider-repl-stdout-face)
158-
(expect (get-text-property 5 'font-lock-face)
159-
:to-equal '(foreground-color . "red3"))
160-
(expect (get-text-property 7 'font-lock-face)
161-
:to-equal '(foreground-color . "red3"))))))
162-
163141
(describe "cider--url-to-file"
164142
(it "returns a url for a given file name"
165143
(expect (cider--url-to-file "file:/space%20test")

0 commit comments

Comments
 (0)