Skip to content

Commit

Permalink
Add justl-per-recipe-buffer custom variable
Browse files Browse the repository at this point in the history
Fixes #44
  • Loading branch information
TristanCacqueray committed Jan 23, 2024
1 parent 1364da3 commit 8c4df5b
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 21 deletions.
28 changes: 22 additions & 6 deletions justl.el
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,17 @@ other cases, it's a known path."
:type 'boolean
:safe 'booleanp)

(defcustom justl-per-recipe-buffer nil
"If non-nil, create a new buffer per recipe."
:type 'boolean
:safe 'booleanp)

(defun justl--recipe-output-buffer (recipe-name)
"Return the buffer name for the RECIPE-NAME."
(if justl-per-recipe-buffer
(format "*just-%s*" recipe-name)
justl--output-process-buffer))

(defun justl--process-error-buffer (process-name)
"Return the error buffer name for the PROCESS-NAME."
(format "*%s:err*" process-name))
Expand Down Expand Up @@ -167,12 +178,14 @@ CMD is the just command as a list."
(justl--append-to-process-buffer
(format "[%s] \ncommand: %S" process-name cmd))))

(defun justl--sentinel (process _)
"Sentinel function for PROCESS."
(defun justl--sentinel (process buffer)
"Sentinel function for PROCESS.
BUFFER is the name of the output buffer."
(let ((process-name (process-name process))
(inhibit-read-only t)
(exit-status (process-exit-status process)))
(with-current-buffer (get-buffer justl--output-process-buffer)
(with-current-buffer buffer
(goto-char (point-max))
(if (zerop exit-status)
(insert (format "\nTarget execution finished at %s" (substring (current-time-string) 0 19)))
Expand Down Expand Up @@ -249,7 +262,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--process-filter)
(set-process-sentinel process sentinel)
(set-process-sentinel process (lambda (proc _) (funcall sentinel proc buf)))
(set-process-coding-system process 'utf-8-emacs-unix 'utf-8-emacs-unix)
(pop-to-buffer buf)))))

Expand Down Expand Up @@ -295,14 +308,15 @@ Error matching regexes from compile.el are removed."
(setq-local overlay-arrow-string "")
(setq next-error-overlay-arrow-position nil))

(defun justl--exec (process-name args)
(defun justl--exec (process-name recipe-name args)
"Utility function to run commands in the proper setting.
PROCESS-NAME is an identifier for the process. Default to \"just\".
RECIPE-NAME is the name of the recipe.
ARGS is a ist of arguments."
(when (equal process-name "")
(setq process-name justl-executable))
(let ((buffer-name justl--output-process-buffer)
(let ((buffer-name (justl--recipe-output-buffer recipe-name))
(error-buffer (justl--process-error-buffer process-name))
(cmd (append (list justl-executable (justl--justfile-argument)) args))
(mode 'justl-compile-mode))
Expand Down Expand Up @@ -542,6 +556,7 @@ not executed."
(let* ((recipe (justl--get-recipe-under-cursor)))
(justl--exec
justl-executable
(justl--recipe-name recipe)
(append (transient-args 'justl-help-popup)
(cons (justl--recipe-name recipe)
(mapcar 'justl--read-arg
Expand All @@ -557,6 +572,7 @@ not executed."
recipe-name))))
(justl--exec
justl-executable
recipe-name
(append (transient-args 'justl-help-popup)
(cons
recipe-name
Expand Down
30 changes: 15 additions & 15 deletions test/justl-test.el
Original file line number Diff line number Diff line change
Expand Up @@ -45,36 +45,36 @@
(with-current-buffer (justl--buffer-name)
(search-forward "plan")
(justl-exec-recipe)
(justl--wait-till-exit justl--output-process-buffer))
(with-current-buffer justl--output-process-buffer
(justl--wait-till-exit (justl--recipe-output-buffer "plan")))
(with-current-buffer (justl--recipe-output-buffer "plan")
(let ((buf-string (buffer-substring-no-properties (point-min) (point-max))))
(should (s-contains? "planner" buf-string))))
(kill-buffer (justl--buffer-name))
(kill-buffer justl--output-process-buffer))
(kill-buffer (justl--recipe-output-buffer "plan")))

(ert-deftest justl--execute-test-exit-status ()
(justl)
(with-current-buffer (justl--buffer-name)
(search-forward "plan")
(justl-exec-recipe)
(justl--wait-till-exit justl--output-process-buffer))
(with-current-buffer justl--output-process-buffer
(justl--wait-till-exit (justl--recipe-output-buffer "plan")))
(with-current-buffer (justl--recipe-output-buffer "plan")
(let ((buf-string (buffer-substring-no-properties (point-min) (point-max))))
(should (s-contains? "Target execution finished" buf-string))))
(kill-buffer (justl--buffer-name))
(kill-buffer justl--output-process-buffer))
(kill-buffer (justl--recipe-output-buffer "plan")))

(ert-deftest justl--fail-recipe ()
(justl)
(with-current-buffer (justl--buffer-name)
(search-forward "fail")
(justl-exec-recipe)
(justl--wait-till-exit justl--output-process-buffer))
(with-current-buffer justl--output-process-buffer
(justl--wait-till-exit (justl--recipe-output-buffer "fail"))
(with-current-buffer (justl--recipe-output-buffer "fail")
(let ((buf-string (buffer-substring-no-properties (point-min) (point-max))))
(should (s-contains? "exited abnormally" buf-string))))
(kill-buffer (justl--buffer-name))
(kill-buffer justl--output-process-buffer))
(kill-buffer (justl--recipe-output-buffer "fail"))))

(ert-deftest justl--find-justfile-check ()
(should (equal (f-filename (justl--find-justfile default-directory)) "justfile")))
Expand All @@ -85,26 +85,26 @@
(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
(justl--wait-till-exit (justl--recipe-output-buffer "carriage-return")))
(with-current-buffer (justl--recipe-output-buffer "carriage-return")
(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))
(kill-buffer (justl--recipe-output-buffer "carriage-return")))

(ert-deftest justl--execute-recipe-with-color ()
"A target printing color is handled properly."
(justl)
(with-current-buffer (justl--buffer-name)
(search-forward "color")
(justl-exec-recipe)
(justl--wait-till-exit justl--output-process-buffer))
(with-current-buffer justl--output-process-buffer
(justl--wait-till-exit (justl--recipe-output-buffer "color")))
(with-current-buffer (justl--recipe-output-buffer "color")
(let ((buf-string (buffer-substring-no-properties (point-min) (point-max))))
(should (s-contains? "This is red text\n" buf-string))))
(kill-buffer (justl--buffer-name))
(kill-buffer justl--output-process-buffer))
(kill-buffer (justl--recipe-output-buffer "color")))

(ert-deftest justl--execute-default-recipe ()
"Checks that default recipe is printed."
Expand Down

0 comments on commit 8c4df5b

Please sign in to comment.