Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

colorize output sent to nrepl buffer #275

Closed
wants to merge 3 commits into from

Conversation

fmezas
Copy link

@fmezas fmezas commented Feb 13, 2013

when present, apply ansi color codes to output sent to the nrepl buffer via nrepl-interactive-eval-handler and nrepl-load-file-handler

when present, apply ansi color codes to nrepl output
@@ -726,7 +726,10 @@ DONE-HANDLER as appropriate."
(lambda (buffer value)
(message "%s" value))
(lambda (buffer value)
(nrepl-emit-interactive-output value))
(nrepl-emit-interactive-output value)
(with-current-buffer "*nrepl*"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That won't work properly, since now we support multiple REPL buffers.

@bbatsov
Copy link
Member

bbatsov commented Feb 13, 2013

The change seems reasonable to me, but there a few problems with the current PR.

Instead of hardcoding *nrepl* as the buffer name, you should be using (nrepl-current-connection-buffer). You're commit message does not comply with the suggested style in the project's contribution guidelines and you have not updated the changelog accordingly. Please, address those issues.

when ANSI color codes are present use them to colorize nrepl output
@fmezas
Copy link
Author

fmezas commented Feb 14, 2013

Thanks for the comments. I should have taken a closer look to the buffer handling code. I ended up using nrepl-current-nrepl-buffer instead of nrepl-current-connection-buffer to avoid hardcoding *nrepl* as the buffer name. I also added an entry to the changelog. Also, thanks for pointing me in the direction of the project contribution guidelines and the docs linked in it; I'm fairly new around here and wasn't really sure about how to proceed, now I know I was supposed to log an issue first :). I am still not completely sure about the problem with the commit message; this time I capitalized it, checked its length, etc, but let me know if I'm still missing something

@bbatsov
Copy link
Member

bbatsov commented Feb 14, 2013

Yep, you're right. I meant nrepl-current-nrepl-buffer, no idea why I wrote connection instead :-) Things look good now, but you might want to use something more localized than (point-min), since as it stands after each evaluation we'll applying the colors to the entire repl buffer and this might be slow if there is a lot of output in the buffer. Maybe you should just use the current point location or the location of the REPL prompt.

Use nrepl-emit-interactive-output. Restrict the colorized region to
just the recent output.
@fmezas
Copy link
Author

fmezas commented Feb 16, 2013

You are right. I've restricted the area to be ansi colorized to just the most recently printed output. I've avoided the duplication of the coloring code by moving it to nrepl-emit-interactive-output

kingtim added a commit that referenced this pull request Feb 19, 2013
@kingtim
Copy link
Member

kingtim commented Feb 19, 2013

Thank you! This is merged to master now.

@kingtim kingtim closed this Feb 19, 2013
@fmezas
Copy link
Author

fmezas commented Feb 20, 2013

You are welcome! glad it was useful

@marick
Copy link

marick commented May 21, 2013

How do you turn this feature on? When I jack in to nrepl and run Midje autotest, the ANSI escapes come out as plain text rather than coloring the text they enclose.

@bbatsov
Copy link
Member

bbatsov commented May 21, 2013

It's on by default. Maybe you're not using the latest nrepl.el?

@gregsexton
Copy link

I have the same problem as marick. Using a clone of the git repo from today. The repl prints ; nREPL 0.1.8-preview on load.

@marick
Copy link

marick commented May 25, 2013

That's my version as well.

@gregsexton
Copy link

I dug into this further. It turns out that ansi escape sequences are only colorized for some 'response handlers' and not others. Midje outputs to stdout and this handler has not had the ansi-color-apply-on-region function call added to it. I got this working by replacing the nrepl-emit-output function with:

(defun nrepl-emit-output (buffer string &optional bol)
  "Using BUFFER, emit STRING.
If BOL is non-nil, emit at the beginning of the line."
  (with-current-buffer buffer
    (nrepl-emit-output-at-pos buffer string nrepl-input-start-mark bol)
    (ansi-color-apply-on-region (marker-position nrepl-output-start) (point-max))))

This is a rather ugly hack to prove my understanding is correct. I think the ansi-color-apply-on-region call should be moved to a single place higher in the call stack. It should be called on any output that nrepl is about to write into the buffer, rather than for specific response handlers.

@marick
Copy link

marick commented May 25, 2013

Here's a sample project you can clone:

git clone git@github.com:marick/sample-midje-project.git

If I run it in an emacs shell buffer, I see this:

lein-midje

In an nrepl buffer, I see this:

nrepl

I'm running "GNU Emacs 24.3.1 (x86_64-apple-darwin, NS apple-appkit-1038.36) of 2013-03-12 on bob.porkrind.org".

Possibly relevant settings:

(setq-default ansi-color-for-comint-mode t
              visible-bell t
              scroll-conservatively 5
              scroll-margin 5)

(custom-set-variables
 '(ansi-color-names-vector ["#212526" "#ff4b4b" "#b4fa70" "#fce94f" "#729fcf" "#ad7fa8" "#8cc4ff" "#eeeeec"])

@genovese
Copy link

genovese commented Nov 1, 2013

I'm having the same problem as the recent posters, for the same basic reason that @gregsexton mentioned,
with CIDER 0.3.0alpha (package: 20131023.1732) (Clojure 1.5.1, nREPL 0.2.1). This is happening on GNU Emacs 24.3.1 (OS X 10.7.5 and 10.6.8) and on GNU Emacs 24.3.50.1 (i386-apple-darwin11.4.2, NS apple-appkit-1138.51) of 2013-10-30 on OS X 10.7.5. The problem is not new and showed up in earlier versions (before the name change). My ansi-color-for-comint-mode and comint-output-filter-functions are set properly.

My temporary fix has been to add the coloration to the end of cider-emit-output-at-pos, as follows:

(defun cider-emit-output-at-pos (buffer string position &optional bol)
  "Using BUFFER, insert STRING at POSITION and mark it as output.
If BOL is non-nil insert at the beginning of line."
  (with-current-buffer buffer
    (save-excursion
      (cider-save-marker cider-repl-output-start
        (cider-save-marker cider-repl-output-end
          (goto-char position)
          (when (and bol (not (bolp))) (insert-before-markers "\n"))
          (cider-propertize-region `(face cider-repl-output-face
                                          rear-nonsticky (face))
            (insert-before-markers string)
            (when (and (= (point) cider-repl-prompt-start-mark)
                       (not (bolp)))
              (insert-before-markers "\n")
              (set-marker cider-repl-output-end (1- (point)))))))
      (ansi-color-apply-on-region (marker-position cider-repl-output-start)
                                  (point)))   ; <<-- the last sexp in excursion is only change
    (cider-show-maximum-output)))

This has worked fine for a good while now, although as @gregsexton suggests, there may be a more appropriate place for the change.

@bbatsov
Copy link
Member

bbatsov commented Nov 1, 2013

You’re using an outdated version of CIDER. I’ve recently fixed this.

Cheers,
Bozhidar

On Friday, November 1, 2013 at 3:35 AM, Christopher Genovese wrote:

I'm having the same problem as the recent posters, for the same basic reason that @gregsexton (https://github.com/gregsexton) mentioned,
with CIDER 0.3.0alpha (package: 20131023.1732) (Clojure 1.5.1, nREPL 0.2.1). This is happening on GNU Emacs 24.3.1 (OS X 10.7.5 and 10.6.8) and on GNU Emacs 24.3.50.1 (i386-apple-darwin11.4.2, NS apple-appkit-1138.51) of 2013-10-30 on OS X 10.7.5. The problem is not new and showed up in earlier versions (before the name change). My ansi-color-for-comint-mode and comint-output-filter-functions are set properly.
My temporary fix has been to add the coloration to the end of cider-emit-output-at-pos, as follows:
(defun cider-emit-output-at-pos (buffer string position &optional bol) "Using BUFFER, insert STRING at POSITION and mark it as output. If BOL is non-nil insert at the beginning of line." (with-current-buffer buffer (save-excursion (cider-save-marker cider-repl-output-start (cider-save-marker cider-repl-output-end (goto-char position) (when (and bol (not (bolp))) (insert-before-markers "\n")) (cider-propertize-region `(face cider-repl-output-face rear-nonsticky (face)) (insert-before-markers string) (when (and (= (point) cider-repl-prompt-start-mark) (not (bolp))) (insert-before-markers "\n") (set-marker cider-repl-output-end (1- (point))))))) (ansi-color-apply-on-region (marker-position cider-repl-output-start) (point))) ; <<-- the last sexp in excursion is only change (cider-show-maximum-output)))
This has worked fine for a good while now, although as @gregsexton (https://github.com/gregsexton) suggests, there may be a more appropriate place for the change.


Reply to this email directly or view it on GitHub (#275 (comment)).

@genovese
Copy link

genovese commented Nov 1, 2013

Ah, my apologies, I didn't catch the update. Thanks, Chris

On Fri, Nov 1, 2013 at 2:15 AM, Bozhidar Batsov notifications@github.comwrote:

You’re using an outdated version of CIDER. I’ve recently fixed this.

Cheers,
Bozhidar

On Friday, November 1, 2013 at 3:35 AM, Christopher Genovese wrote:

I'm having the same problem as the recent posters, for the same basic
reason that @gregsexton (https://github.com/gregsexton) mentioned,
with CIDER 0.3.0alpha (package: 20131023.1732) (Clojure 1.5.1, nREPL
0.2.1). This is happening on GNU Emacs 24.3.1 (OS X 10.7.5 and 10.6.8) and
on GNU Emacs 24.3.50.1 (i386-apple-darwin11.4.2, NS apple-appkit-1138.51)
of 2013-10-30 on OS X 10.7.5. The problem is not new and showed up in
earlier versions (before the name change). My ansi-color-for-comint-mode
and comint-output-filter-functions are set properly.
My temporary fix has been to add the coloration to the end of
cider-emit-output-at-pos, as follows:
(defun cider-emit-output-at-pos (buffer string position &optional bol)
"Using BUFFER, insert STRING at POSITION and mark it as output. If BOL is
non-nil insert at the beginning of line." (with-current-buffer buffer
(save-excursion (cider-save-marker cider-repl-output-start
(cider-save-marker cider-repl-output-end (goto-char position) (when (and
bol (not (bolp))) (insert-before-markers "\n")) (cider-propertize-region
`(face cider-repl-output-face rear-nonsticky (face)) (insert-before-markers
string) (when (and (= (point) cider-repl-prompt-start-mark) (not (bolp)))
(insert-before-markers "\n") (set-marker cider-repl-output-end (1-
(point))))))) (ansi-color-apply-on-region (marker-position
cider-repl-output-start) (point))) ; <<-- the last sexp in excursion is
only change (cider-show-maximum-output)))
This has worked fine for a good while now, although as @gregsexton (
https://github.com/gregsexton) suggests, there may be a more appropriate
place for the change.


Reply to this email directly or view it on GitHub (
#275 (comment)).


Reply to this email directly or view it on GitHubhttps://github.com//pull/275#issuecomment-27549485
.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants