diff --git a/justl.el b/justl.el index 89b4dd8..21fa4b9 100644 --- a/justl.el +++ b/justl.el @@ -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: @@ -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." @@ -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. @@ -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))))) @@ -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 diff --git a/test/justfile b/test/justfile index d9a61d6..0f05dc5 100644 --- a/test/justfile +++ b/test/justfile @@ -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" \ No newline at end of file diff --git a/test/justl-test.el b/test/justl-test.el index f34e421..cf5555c 100644 --- a/test/justl-test.el +++ b/test/justl-test.el @@ -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 () @@ -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)