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

Use compilation-filter as subprocess filter #21

Merged
merged 1 commit into from
Jul 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 18 additions & 14 deletions justl.el
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
;; Keywords: just justfile tools processes
;; URL: https://github.com/psibi/justl.el
;; License: GNU General Public License >= 3
;; Package-Requires: ((transient "0.1.0") (emacs "25.3") (xterm-color "2.0") (s "1.2.0") (f "0.20.0"))
;; Package-Requires: ((transient "0.1.0") (emacs "25.3") (s "1.2.0") (f "0.20.0"))

;;; Commentary:

Expand Down Expand Up @@ -60,17 +60,20 @@
;;
;; You can also control the width of the RECIPE column in the justl
;; buffer via `justl-recipe width`. By default it has a value of 20.
;;

;;; Code:

(require 'ansi-color)
(require 'transient)
(require 'cl-lib)
(require 'xterm-color)
(require 'eshell)
(require 'esh-mode)
(require 's)
(require 'f)
(require 'compile)
(require 'comint)
(require 'subr-x)

(defgroup justl nil
"Justfile customization group."
Expand Down Expand Up @@ -283,20 +286,21 @@ CMD is the just command as a list."
process-name
err))))))

(defun justl--xterm-color-filter (proc string)
"Filter function for PROC handling colors.
(defun justl--process-filter (proc string)
"Filter function for PROC handling colors and carriage return.
STRING is the data returned by the PROC"
(when (buffer-live-p (process-buffer proc))
(with-current-buffer (process-buffer proc)
(let ((inhibit-read-only t)
(moving (= (point) (process-mark proc))))
(save-excursion
;; Insert the text, advancing the process marker.
(goto-char (process-mark proc))
(insert (xterm-color-filter string))
(set-marker (process-mark proc) (point)))
(if moving (goto-char (process-mark proc)))))))
(let ((inhibit-read-only t))
(unwind-protect
(progn
(widen)
(goto-char (marker-position (process-mark proc)))
(insert string)
(comint-carriage-motion (process-mark proc) (point))
(ansi-color-apply-on-region (process-mark proc) (point))
(set-marker (process-mark proc) (point))))))))

(defun justl-compilation-setup-buffer (buf dir mode &optional no-mode-line)
"Setup the compilation buffer for just-compile-mode.
Expand Down Expand Up @@ -349,7 +353,7 @@ ARGS is a plist that affects how the process is run.

(setq-local justl--justfile (justl--justfile-from-arg (elt command 1)))
(run-hook-with-args 'compilation-start-hook process)
(set-process-filter process 'justl--xterm-color-filter)
(set-process-filter process 'justl--process-filter)
(set-process-sentinel process sentinel)
(set-process-coding-system process 'utf-8-emacs-unix 'utf-8-emacs-unix)
(pop-to-buffer buf)))))
Expand Down Expand Up @@ -430,7 +434,7 @@ ARGS is a ist of arguments."
(justl--log-command process-name cmd)
(make-process :name process-name
:buffer buffer-name
:filter 'justl--xterm-color-filter
:filter 'justl--process-filter
:sentinel #'justl--sentinel
:file-handler t
:stderr nil
Expand Down
8 changes: 8 additions & 0 deletions test/justfile
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,11 @@ push2 version1 version2:
# A target that will fail
fail:
exit 1

# A target that will print strings with carriage return
carriage-return:
#!/usr/bin/env bash
printf "1/3\r"
printf "2/3\r"
printf "3/3\r"
printf "DONE\n"
16 changes: 15 additions & 1 deletion test/justl-test.el
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

(ert-deftest justl--get-recipies-test ()
(should (equal
(list "default" "build-cmd" "plan" "push" "push2" "fail")
(list "default" "build-cmd" "plan" "push" "push2" "fail" "carriage-return")
(justl--get-recipies))))

(ert-deftest justl--list-to-recipe-test ()
Expand Down Expand Up @@ -211,6 +211,20 @@
(should (member (list "push" nil) recipies))
(should (member (list "push2" nil) recipies))))

(ert-deftest justl--execute-recipe-which-prints-carriage-return ()
"Carriage return should be handled in a way that allows overwriting lines."
(justl)
(with-current-buffer (justl--buffer-name)
(search-forward "carriage-return")
(justl-exec-recipe)
(justl--wait-till-exit justl--output-process-buffer))
(with-current-buffer justl--output-process-buffer
(let ((buf-string (buffer-substring-no-properties (point-min) (point-max))))
(should (s-contains? "DONE\n" buf-string))
(should-not (s-contains? "1/3\r2/3\r3/3\rDONE\n" buf-string))))
(kill-buffer (justl--buffer-name))
(kill-buffer justl--output-process-buffer))

;; (ert "justl--**")

(provide 'justl-test)
Expand Down