diff --git a/DESCRIPTION b/DESCRIPTION index c1c2b99a..711e03f5 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: quarto Title: R Interface to 'Quarto' Markdown Publishing System -Version: 1.4.4.9006 +Version: 1.4.4.9007 Authors@R: c( person("JJ", "Allaire", , "jj@posit.co", role = "aut", comment = c(ORCID = "0000-0003-0174-9868")), diff --git a/NEWS.md b/NEWS.md index af8567ca..8e231348 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,7 @@ # quarto (development version) +- `quarto_preview()` gains a `quiet` argument to suppress any output from R or Quarto CLI (thanks, @cwickham, #232.) + - 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)). - Add `quarto.quiet` options to allow more verbose error message when `quarto_*` function are used inside other package. diff --git a/R/daemon.R b/R/daemon.R index 590786c8..4bd0e3d0 100644 --- a/R/daemon.R +++ b/R/daemon.R @@ -6,7 +6,9 @@ run_serve_daemon <- function( render, port, host, - browse + browse, + quiet = FALSE, + .call = rlang::caller_env() ) { # resolve target if provided if (!is.null(target)) { @@ -39,14 +41,14 @@ run_serve_daemon <- function( } else { port <- find_port() if (is.null(port)) { - stop("Unable to find port to start server on") + cli::cli_abort("Unable to find port to start server on.", call = .call) } } } # check for port availability if (port_active(port)) { - stop("Server port ", port, " already in use.") + cli::cli_abort("Server port {port} already in use.", call = .call) } # command and target @@ -75,6 +77,13 @@ run_serve_daemon <- function( # no browse (we'll use browseURL) args <- c(args, "--no-browse") + # quiet mode + quiet_msg_suffix <- NULL + if (is_quiet(quiet)) { + args <- cli_arg_quiet(args) + quiet_msg_suffix <- " Set {.code quiet = FALSE} to have more information from quarto CLI output." + } + # add extra args args <- c(args, extra_args) @@ -92,10 +101,12 @@ run_serve_daemon <- function( init <- "" while (!port_active(port)) { quarto[[ps_key]]$poll_io(50) - cat(quarto[[ps_key]]$read_output()) + if (isFALSE(quiet)) { + cat(quarto[[ps_key]]$read_output()) + } if (!quarto[[ps_key]]$is_alive()) { stop_serve_daemon(command) - stop("Error starting quarto") + cli::cli_abort(c(x = "Error starting quarto.", quiet_msg_suffix)) } } quarto[[port_key]] <- port @@ -105,22 +116,28 @@ run_serve_daemon <- function( if (is.null(quarto[[ps_key]])) { return() } - ro <- quarto[[ps_key]]$read_output() - cat(ro) - # Look at url to browse too in `quarto preview log` - if ( - !isFALSE(browse) && - is.null(quarto[[url_key]]) && - grepl("Browse at https?://", ro) - ) { - m <- regexec("Browse at (https?://[^ ]+)\n", ro) - quarto[[url_key]] <- regmatches(ro, m)[[1]][2] + # No output to read url from if quiet + if (isFALSE(quiet)) { + ro <- quarto[[ps_key]]$read_output() + cat(ro) + # Look at url to browse too in `quarto preview log` + if ( + !isFALSE(browse) && + is.null(quarto[[url_key]]) && + grepl("Browse at https?://", ro) + ) { + m <- regexec("Browse at (https?://[^ ]+)\n", ro) + quarto[[url_key]] <- regmatches(ro, m)[[1]][2] + } } if (!quarto[[ps_key]]$is_alive()) { status <- quarto[[ps_key]]$get_exit_status() quarto[[ps_key]] <- NULL if (status != 0) { - stop("Error running quarto ", command) + cli::cli_abort(c( + x = "Error running {.code quarto {command}}.", + quiet_msg_suffix + )) } return() } @@ -129,7 +146,13 @@ run_serve_daemon <- function( poll_process() # indicate server is running - cat(paste0("Stop the preview with quarto_", command, "_stop()")) + if (isFALSE(quiet)) { + cli::cli + cli::cli_inform(c( + "", + i = "Stop the preview with {.code quarto_{command}_stop()}" + )) + } # run the preview browser if (!isFALSE(browse)) { diff --git a/R/preview.R b/R/preview.R index ef544bcd..34ea8dbe 100644 --- a/R/preview.R +++ b/R/preview.R @@ -21,6 +21,8 @@ #' @param watch Watch for changes and automatically reload browser. #' @param navigate Automatically navigate the preview browser to the most #' recently rendered document. +#' @param quiet Suppress warning and other messages, from R and also Quarto CLI +#' (i.e `--quiet` is passed as command line) #' #' @importFrom processx process #' @importFrom rstudioapi isAvailable @@ -52,7 +54,8 @@ quarto_preview <- function( host = "127.0.0.1", browse = TRUE, watch = TRUE, - navigate = TRUE + navigate = TRUE, + quiet = FALSE ) { # default for file if (is.null(file)) { @@ -77,7 +80,8 @@ quarto_preview <- function( render, port, host, - browse + browse, + quiet = quiet ) } diff --git a/man/quarto_preview.Rd b/man/quarto_preview.Rd index 0e23231d..f36c9c60 100644 --- a/man/quarto_preview.Rd +++ b/man/quarto_preview.Rd @@ -12,7 +12,8 @@ quarto_preview( host = "127.0.0.1", browse = TRUE, watch = TRUE, - navigate = TRUE + navigate = TRUE, + quiet = FALSE ) quarto_preview_stop() @@ -40,6 +41,9 @@ RStudio Viewer when running within RStudio.Pass a function (e.g. \item{navigate}{Automatically navigate the preview browser to the most recently rendered document.} + +\item{quiet}{Suppress warning and other messages, from R and also Quarto CLI +(i.e \code{--quiet} is passed as command line)} } \description{ Render and preview a Quarto document or website project.