jupyter icon indicating copy to clipboard operation
jupyter copied to clipboard

ANSI escape characters not rendering correctly

Open ssfrr opened this issue 6 years ago • 3 comments

See #56 for more context.

As @dzop requested, here's some additional debugging output.

In an org-mode buffer I ran eval-expression and ran:

(with-temp-buffer
  (org-mode)
  (insert ": �[31mhello�[39m")
  (font-lock-ensure)
  (text-properties-at 8))

which returned

(face org-code wrap-prefix "" line-prefix "")

I created an elisp file (with the proper escape character which isn't displayed here):

(push "~/.emacs.d/elpa/26.3/develop/jupyter-20190917.1642" load-path)
(push "~/.emacs.d/elpa/26.3/develop/simple-httpd-20190110.1505" load-path)
(push "~/.emacs.d/elpa/26.3/develop/websocket-20190621.54" load-path)
(push "~/.emacs.d/elpa/26.3/develop/org-plus-contrib-20190909" load-path)
(require 'org)
(require 'jupyter-org-client)
(with-temp-buffer
  (org-mode)
  (insert ": [31mhello[39m")
  (font-lock-ensure)
  (prin1 (text-properties-at 8)))

And ran it with emacs -Q -batch -l /tmp/ansi-test.el. emacs exited with status 0 but otherwise didn't produce any output.

ssfrr avatar Oct 29 '19 17:10 ssfrr

Hmm...you should be seeing something printed. Can you change the prin1 call to (message "%s" (text-properties-at 8)) to see if you see something.

Also, are you using Spacemacs or similar?

nnicandro avatar Mar 06 '20 00:03 nnicandro

Hi, I have encountered the same problem with jupyter-julia too, and I am using vanilla Emacs 27.1 and emacs-jupyter with straight.el.

According to the documentation of hooks, the org-export-before-parsing-hook is only executed on a copy of the original buffer that is being exported. So the function org-babel-jupyter-strip-ansi-escapes won't be called until exportation starts, and it does not remove the ansi escape codes in the original buffer.

To make sure the escape codes are handled after source code block's execution, I added this function in ob-jupyter.el:

(defun org-babel-jupyter-handle-result-ansi-escapes ()
  "Handle ANSI escapes in Jupyter src-block result."
  (org-babel-map-src-blocks nil
    (when (org-babel-jupyter-language-p lang)
      (goto-char (org-babel-where-is-src-block-result))
      (ansi-color-apply-on-region (point) (org-babel-result-end)))))

and added the following hook:

(add-hook 'org-babel-after-execute-hook #'org-babel-jupyter-handle-result-ansi-escapes)

ansi

haldai avatar Oct 16 '20 20:10 haldai

@nnicandro I'm still being bitten by this problem, and it seems to be worse now after doing some updates to org and emacs-jupyter.

when I run

(with-temp-buffer
  (org-mode)
  (insert ": �[31mhello�[39m")
  (font-lock-ensure)
  (text-properties-at 8))

I get

(jupyter-ansi t face org-code)

In addition, the various ways of handling ansi colour codes now just removes the ansi codes for me, but does not actually apply the colour styling.

MasonProtter avatar Sep 05 '23 09:09 MasonProtter