Skip to content

Commit d643d27

Browse files
cwickhamcderv
andauthored
Add quiet argument to quarto_preview (#232)
* Add `quiet` argument to `quarto_preview` * Apply quiet in every place of run_server_daemon * use cli instead of stop() --------- Co-authored-by: Christophe Dervieux <[email protected]>
1 parent 7a1e2ce commit d643d27

File tree

5 files changed

+54
-21
lines changed

5 files changed

+54
-21
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Package: quarto
22
Title: R Interface to 'Quarto' Markdown Publishing System
3-
Version: 1.4.4.9006
3+
Version: 1.4.4.9007
44
Authors@R: c(
55
person("JJ", "Allaire", , "[email protected]", role = "aut",
66
comment = c(ORCID = "0000-0003-0174-9868")),

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# quarto (development version)
22

3+
- `quarto_preview()` gains a `quiet` argument to suppress any output from R or Quarto CLI (thanks, @cwickham, #232.)
4+
35
- Add some helpers function `theme_brand_*` and `theme_colors_*` to help theme with dark and light brand using some common graph and table packages (thanks, @gordonwoodhull, [#234](https://github.com/quarto-dev/quarto-r/issues/234)).
46

57
- Add `quarto.quiet` options to allow more verbose error message when `quarto_*` function are used inside other package.

R/daemon.R

Lines changed: 40 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ run_serve_daemon <- function(
66
render,
77
port,
88
host,
9-
browse
9+
browse,
10+
quiet = FALSE,
11+
.call = rlang::caller_env()
1012
) {
1113
# resolve target if provided
1214
if (!is.null(target)) {
@@ -39,14 +41,14 @@ run_serve_daemon <- function(
3941
} else {
4042
port <- find_port()
4143
if (is.null(port)) {
42-
stop("Unable to find port to start server on")
44+
cli::cli_abort("Unable to find port to start server on.", call = .call)
4345
}
4446
}
4547
}
4648

4749
# check for port availability
4850
if (port_active(port)) {
49-
stop("Server port ", port, " already in use.")
51+
cli::cli_abort("Server port {port} already in use.", call = .call)
5052
}
5153

5254
# command and target
@@ -75,6 +77,13 @@ run_serve_daemon <- function(
7577
# no browse (we'll use browseURL)
7678
args <- c(args, "--no-browse")
7779

80+
# quiet mode
81+
quiet_msg_suffix <- NULL
82+
if (is_quiet(quiet)) {
83+
args <- cli_arg_quiet(args)
84+
quiet_msg_suffix <- " Set {.code quiet = FALSE} to have more information from quarto CLI output."
85+
}
86+
7887
# add extra args
7988
args <- c(args, extra_args)
8089

@@ -92,10 +101,12 @@ run_serve_daemon <- function(
92101
init <- ""
93102
while (!port_active(port)) {
94103
quarto[[ps_key]]$poll_io(50)
95-
cat(quarto[[ps_key]]$read_output())
104+
if (isFALSE(quiet)) {
105+
cat(quarto[[ps_key]]$read_output())
106+
}
96107
if (!quarto[[ps_key]]$is_alive()) {
97108
stop_serve_daemon(command)
98-
stop("Error starting quarto")
109+
cli::cli_abort(c(x = "Error starting quarto.", quiet_msg_suffix))
99110
}
100111
}
101112
quarto[[port_key]] <- port
@@ -105,22 +116,28 @@ run_serve_daemon <- function(
105116
if (is.null(quarto[[ps_key]])) {
106117
return()
107118
}
108-
ro <- quarto[[ps_key]]$read_output()
109-
cat(ro)
110-
# Look at url to browse too in `quarto preview log`
111-
if (
112-
!isFALSE(browse) &&
113-
is.null(quarto[[url_key]]) &&
114-
grepl("Browse at https?://", ro)
115-
) {
116-
m <- regexec("Browse at (https?://[^ ]+)\n", ro)
117-
quarto[[url_key]] <- regmatches(ro, m)[[1]][2]
119+
# No output to read url from if quiet
120+
if (isFALSE(quiet)) {
121+
ro <- quarto[[ps_key]]$read_output()
122+
cat(ro)
123+
# Look at url to browse too in `quarto preview log`
124+
if (
125+
!isFALSE(browse) &&
126+
is.null(quarto[[url_key]]) &&
127+
grepl("Browse at https?://", ro)
128+
) {
129+
m <- regexec("Browse at (https?://[^ ]+)\n", ro)
130+
quarto[[url_key]] <- regmatches(ro, m)[[1]][2]
131+
}
118132
}
119133
if (!quarto[[ps_key]]$is_alive()) {
120134
status <- quarto[[ps_key]]$get_exit_status()
121135
quarto[[ps_key]] <- NULL
122136
if (status != 0) {
123-
stop("Error running quarto ", command)
137+
cli::cli_abort(c(
138+
x = "Error running {.code quarto {command}}.",
139+
quiet_msg_suffix
140+
))
124141
}
125142
return()
126143
}
@@ -129,7 +146,13 @@ run_serve_daemon <- function(
129146
poll_process()
130147

131148
# indicate server is running
132-
cat(paste0("Stop the preview with quarto_", command, "_stop()"))
149+
if (isFALSE(quiet)) {
150+
cli::cli
151+
cli::cli_inform(c(
152+
"",
153+
i = "Stop the preview with {.code quarto_{command}_stop()}"
154+
))
155+
}
133156

134157
# run the preview browser
135158
if (!isFALSE(browse)) {

R/preview.R

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
#' @param watch Watch for changes and automatically reload browser.
2222
#' @param navigate Automatically navigate the preview browser to the most
2323
#' recently rendered document.
24+
#' @param quiet Suppress warning and other messages, from R and also Quarto CLI
25+
#' (i.e `--quiet` is passed as command line)
2426
#'
2527
#' @importFrom processx process
2628
#' @importFrom rstudioapi isAvailable
@@ -52,7 +54,8 @@ quarto_preview <- function(
5254
host = "127.0.0.1",
5355
browse = TRUE,
5456
watch = TRUE,
55-
navigate = TRUE
57+
navigate = TRUE,
58+
quiet = FALSE
5659
) {
5760
# default for file
5861
if (is.null(file)) {
@@ -77,7 +80,8 @@ quarto_preview <- function(
7780
render,
7881
port,
7982
host,
80-
browse
83+
browse,
84+
quiet = quiet
8185
)
8286
}
8387

man/quarto_preview.Rd

Lines changed: 5 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)