From 1473245e50a93859258c1e2fb2daab8c1f938c66 Mon Sep 17 00:00:00 2001 From: Christophe Dervieux Date: Fri, 19 Jul 2024 16:21:22 +0200 Subject: [PATCH] quarto_preview()` now looks at `quarto preview` log to browse to the correct url when inside RStudio viewer closes #167 --- NEWS.md | 4 +++- R/daemon.R | 17 +++++++++++++++-- R/utils.R | 4 ++++ 3 files changed, 22 insertions(+), 3 deletions(-) diff --git a/NEWS.md b/NEWS.md index 9c8af47..59df0b8 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,7 @@ # quarto (development version) +- `quarto_preview()` now looks at `quarto preview` log to browse to the correct url when inside RStudio viewer (thanks, @aronatkins, #167). + - This package now uses the x.y.z.dev versionning scheme to indicate development, patch, minor and major versions. This follows [Tidyverse package version conventions](https://r-pkgs.org/lifecycle.html#sec-lifecycle-version-number-tidyverse). - Adapt tests for CRAN checks issues due to Quarto v1.5.54 regression, fixed in Quarto v1.5.55. @@ -13,7 +15,7 @@ - Add registration of vignette engine to use `quarto` as a vignette builder, and use `.qmd` file as vignette. See `vignette("hello", package = "quarto")`. (thanks, @dcnorris, #57). - New `quarto_binary_sitrep()` checks possible difference in Quarto binary used by this package, and the one used by RStudio IDE (thanks, @jthomasmock, #12). - + - New `is_using_quarto()` to check if a directory requires using Quarto (i.e. it has a `_quarto.yml` or at least one `*.qmd` file) (thanks, @hadley, #103). - New `quarto_create_project()` calls `quarto create project ` (thanks, @maelle, #87). diff --git a/R/daemon.R b/R/daemon.R index 8452a66..4b47d2b 100644 --- a/R/daemon.R +++ b/R/daemon.R @@ -13,9 +13,14 @@ run_serve_daemon <- function(command, target, wd, extra_args, render, port, host # calculate keys ps_key <- paste0(command, "_ps") port_key <- paste0(command, "_port") + url_key <- paste0(command, "_url") + # We don't need to keep previous url + quarto[[url_key]] <- NULL # manage existing server instances stop_serve_daemon(command) + # we don't need to keep previous url + quarto[[url_key]] <- NULL # if the last server had a port then re-use it for "auto" if (port == "auto") { @@ -86,12 +91,20 @@ run_serve_daemon <- function(command, target, wd, extra_args, render, port, host } quarto[[port_key]] <- port + + # monitor the process for abnormal exit poll_process <- function() { if (is.null(quarto[[ps_key]])) { return() } - cat(quarto[[ps_key]]$read_output()) + 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 @@ -116,7 +129,7 @@ run_serve_daemon <- function(command, target, wd, extra_args, render, port, host utils::browseURL ) } - serve_url <- paste0("http://localhost:", port) + serve_url <- quarto[[url_key]] %||% paste0("http://localhost:", port) browse(serve_url) } diff --git a/R/utils.R b/R/utils.R index 13a2da5..43d1a77 100644 --- a/R/utils.R +++ b/R/utils.R @@ -21,3 +21,7 @@ merge_list <- function(x, y) { x[names(y)] <- y x } + +`%||%` <- function(x, y) { + if (is_null(x)) y else x +}