ob-ruby: Fix ruby tests

* lisp/ob-ruby.el (org-babel-ruby-prompt):
(org-babel-ruby-define-prompt): New variables used to set appropriate
unique prompt and disable echo in session.
(org-babel-ruby-initiate-session): Setup prompt and echo during
session initialization.
(org-babel-ruby-evaluate): Rely on `org-babel-ruby-initiate-session'
to setup prompt and echo.  Re-setting the prompt back after every
evaluation can be tricky because of async nature of the prompt, so it
is now dropped.
This commit is contained in:
Ihor Radchenko 2023-08-23 14:19:52 +03:00
parent c905565fa8
commit 430c72924c
No known key found for this signature in database
GPG Key ID: 6470762A7DA11D8B
1 changed files with 27 additions and 11 deletions

View File

@ -156,6 +156,12 @@ Emacs-lisp table, otherwise return the results as a string."
res)
res)))
(defvar org-babel-ruby-prompt "_org_babel_ruby_prompt "
"String used for unique prompt.")
(defvar org-babel-ruby-define-prompt
(format "IRB.conf[:PROMPT][:CUSTOM] = { :PROMPT_I => \"%s\" }" org-babel-ruby-prompt))
(defun org-babel-ruby-initiate-session (&optional session params)
"Initiate a ruby session.
If there is not a current inferior-process-buffer in SESSION
@ -166,6 +172,7 @@ then create one. Return the initialized session."
(assoc inf-ruby-default-implementation
inf-ruby-implementations))))
(buffer (get-buffer (format "*%s*" session)))
(new-session? (not buffer))
(session-buffer (or buffer (save-window-excursion
(run-ruby-or-pop-to-buffer
(if (functionp command)
@ -176,16 +183,30 @@ then create one. Return the initialized session."
(inf-ruby-buffer)))
(current-buffer)))))
(if (org-babel-comint-buffer-livep session-buffer)
(progn (sit-for .25) session-buffer)
(progn
(sit-for .25)
;; Setup machine-readable prompt: no echo, prompts matching
;; uniquely by regexp.
(when new-session?
(with-current-buffer session-buffer
(setq-local comint-prompt-regexp (concat "^" org-babel-ruby-prompt))
(insert org-babel-ruby-define-prompt ";")
(insert "_org_prompt_mode=conf.prompt_mode;conf.prompt_mode=:CUSTOM;")
(insert "conf.echo=false")
(comint-send-input nil t)))
session-buffer)
(sit-for .5)
(org-babel-ruby-initiate-session session)))))
(defvar org-babel-ruby-eoe-indicator ":org_babel_ruby_eoe"
"String to indicate that evaluation has completed.")
(defvar org-babel-ruby-f-write
"File.open('%s','w'){|f| f.write((_.class == String) ? _ : _.inspect)}")
(defvar org-babel-ruby-pp-f-write
"File.open('%s','w'){|f| $stdout = f; pp(results); $stdout = orig_out}")
(defvar org-babel-ruby-wrapper-method
"
def main()
@ -194,6 +215,7 @@ end
results = main()
File.open('%s', 'w'){ |f| f.write((results.class == String) ? results : results.inspect) }
")
(defvar org-babel-ruby-pp-wrapper-method
"
require 'pp'
@ -237,7 +259,6 @@ return the value of the last statement in BODY, as elisp."
(org-babel-comint-with-output
(buffer org-babel-ruby-eoe-indicator t eoe-string)
(insert eoe-string) (comint-send-input nil t))
;; Now we can start the evaluation.
(mapconcat
#'identity
(butlast
@ -246,14 +267,9 @@ return the value of the last statement in BODY, as elisp."
#'org-trim
(org-babel-comint-with-output
(buffer org-babel-ruby-eoe-indicator t body)
(mapc
(lambda (line)
(insert (org-babel-chomp line)) (comint-send-input nil t))
(list "conf.echo=false;_org_prompt_mode=conf.prompt_mode;conf.prompt_mode=:NULL"
body
"conf.prompt_mode=_org_prompt_mode;conf.echo=true"
eoe-string)))
"\n") "[\r\n]") 4) "\n")))
(insert (org-babel-chomp body) "\n" eoe-string)
(comint-send-input nil t))
"\n") "[\r\n]")) "\n")))
(`value
(let* ((tmp-file (org-babel-temp-file "ruby-"))
(ppp (or (member "code" result-params)