Skip to content

Instantly share code, notes, and snippets.

@aserranoni
Created February 28, 2024 01:40
Show Gist options
  • Save aserranoni/a1833fba8518e317cdeb93855ff98281 to your computer and use it in GitHub Desktop.
Save aserranoni/a1833fba8518e317cdeb93855ff98281 to your computer and use it in GitHub Desktop.
Making pact a bit more human-readable via org-mode
(defun org-babel-execute:pact (body params)
"Execute a Pact block with specified parameters."
(let* ((tempfile (org-babel-temp-file "pact-" ".repl"))
(pact-process (start-process "pact-repl" "*pact-repl-output*" "pact"))
(load-command (format "(load \"%s\")\n" tempfile))
(output-buffer "*pact-repl-output*"))
;; Ensure the REPL is ready
(sleep-for 1) ;; Adjust based on REPL start time
;; Write body to temporary file
(with-temp-file tempfile
(insert body))
;; Send load command to Pact REPL
(process-send-string pact-process load-command)
;; Allow time for command execution
(sleep-for 2) ;; Adjust based on execution time
;; Capture and filter the output
;;
(with-current-buffer output-buffer
(goto-char (point-max)) ; Start from the end of the buffer
(if (re-search-backward "^pact> " nil t) ; Find the last occurrence of 'pact>'
(let ((end (match-beginning 0))) ; Mark the beginning of the last occurrence
(if (re-search-backward "^pact> " nil t) ; Find the second to last occurrence
(let ((start (match-end 0))) ; Mark the end of the second to last occurrence
(replace-regexp-in-string "^.*Loading.*\n?" "" (buffer-substring-no-properties start end))) ; Extract the text between them
"")) ; If there's no second to last 'pact>', return an empty string
""))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment