Skip to content

Commit

Permalink
Support running interactive programs
Browse files Browse the repository at this point in the history
  • Loading branch information
isvilen committed May 14, 2024
1 parent e54bbae commit c9ca017
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 16 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,9 @@ By default these are bound to:
- <kbd>C-c C-c C-t</kbd> `rust-test`
- <kbd>C-c C-c C-r</kbd> `rust-run`

To run programs requiring user input use universal argument when invoking
`rust-run` (<kbd>C-u C-c C-c C-r</kbd>).

### Clippy

`rust-run-clippy` runs
Expand Down
40 changes: 24 additions & 16 deletions rust-cargo.el
Original file line number Diff line number Diff line change
Expand Up @@ -67,46 +67,54 @@

;;; Internal

(defun rust--compile (format-string &rest args)
(defun rust--compile (comint format-string &rest args)
(when (null rust-buffer-project)
(rust-update-buffer-project))
(let ((default-directory
(or (and rust-buffer-project
(file-name-directory rust-buffer-project))
default-directory)))
(compile (apply #'format format-string args))))
default-directory))
;; make sure comint is a boolean value
(comint (not (not comint))))
(compile (apply #'format format-string args) comint)))

;;; Commands

(defun rust-check ()
"Compile using `cargo check`"
(interactive)
(rust--compile "%s check %s" rust-cargo-bin rust-cargo-default-arguments))
(rust--compile nil "%s check %s" rust-cargo-bin rust-cargo-default-arguments))

(defun rust-compile ()
"Compile using `cargo build`"
(interactive)
(rust--compile "%s build %s" rust-cargo-bin rust-cargo-default-arguments))
(rust--compile nil "%s build %s" rust-cargo-bin rust-cargo-default-arguments))

(defun rust-compile-release ()
"Compile using `cargo build --release`"
(interactive)
(rust--compile "%s build --release" rust-cargo-bin))
(rust--compile nil "%s build --release" rust-cargo-bin))

(defun rust-run ()
"Run using `cargo run`"
(interactive)
(rust--compile "%s run %s" rust-cargo-bin rust-cargo-default-arguments))
(defun rust-run (&optional comint)
"Run using `cargo run`
(defun rust-run-release ()
"Run using `cargo run --release`"
(interactive)
(rust--compile "%s run --release" rust-cargo-bin))
If optional arg COMINT is t or invoked with universal prefix arg,
output buffer will be in comint mode, i.e. interactive."
(interactive "P")
(rust--compile comint "%s run %s" rust-cargo-bin rust-cargo-default-arguments))

(defun rust-run-release (&optional comint)
"Run using `cargo run --release`
If optional arg COMINT is t or invoked with universal prefix arg,
output buffer will be in comint mode, i.e. interactive."
(interactive "P")
(rust--compile comint "%s run --release" rust-cargo-bin))

(defun rust-test ()
"Test using `cargo test`"
(interactive)
(rust--compile "%s test %s" rust-cargo-bin rust-cargo-default-arguments))
(rust--compile nil "%s test %s" rust-cargo-bin rust-cargo-default-arguments))

(defun rust-run-clippy ()
"Run `cargo clippy'."
Expand All @@ -118,7 +126,7 @@
;; set `compile-command' temporarily so `compile' doesn't
;; clobber the existing value
(compile-command (mapconcat #'shell-quote-argument args " ")))
(rust--compile compile-command)))
(rust--compile nil compile-command)))

;;; _
(provide 'rust-cargo)
Expand Down

0 comments on commit c9ca017

Please sign in to comment.