# Portions of this file are adapted from the tidyverse package. # Copyright (c) 2024 tidyverse authors. # License: MIT; full notice preserved in LICENSE.note. # Source: https://github.com/tidyverse/tidyverse/blob/0231aafbc56914ee5371dd6c7b60677f168d7154/R/attach.R #' Print stanflow status and conflicts #' #' Print a consolidated status report showing attached packages, available #' interfaces, and any conflicts. #' #' @param only Set this to a character vector to restrict to conflicts only #' between the provided packages and loaded stanflow packages. #' @param check_updates If `TRUE`, checks for stable updates to stanflow packages. #' @return Invisibly returns the character vector that was printed. #' @export #' @examples #' flow_check() flow_check <- function(check_updates = FALSE, only = NULL) { messages <- list( core_attach_message(show_all = TRUE), backends_attach_message(), stanflow_conflict_message(stanflow_conflicts(only)) ) |> Filter(Negate(is.null), x = _) if (check_updates) { messages <- c(messages, update_check_message()) } cli::cat_line(messages) invisible(unlist(messages)) } core_attach_message <- function(show_all = FALSE) { core_unloaded <- .find_unloaded(core) suppressPackageStartupMessages(lapply(core_unloaded, .same_library)) to_show <- if (show_all) core else core_unloaded if (length(to_show) == 0) { return(NULL) } versions <- vapply(to_show, .package_version_h, character(1)) packages <- paste0( cli::col_green(cli::symbol$tick), " ", cli::col_blue(format(to_show)), " ", cli::ansi_align(versions, max(cli::ansi_nchar(versions))) ) header <- cli::rule( left = cli::style_bold("Attaching Stan processing packages"), right = paste0("stanflow ", .package_version_h("stanflow")) ) message_packages(packages, header) } backends_attach_message <- function() { versions <- vapply( backends, function(x) { if (is_installed(x)) .package_version_h(x) else "" }, character(1) ) symbols <- vapply( backends, function(x) { if (is_attached(x)) { cli::col_green(cli::symbol$tick) } else if (is_installed(x)) { cli::col_blue(cli::symbol$bullet) } else { cli::col_red(cli::symbol$cross) } }, character(1) ) # Grey out names if not installed names <- format(backends) names <- ifelse(versions == "", cli::col_silver(names), cli::col_blue(names)) packages <- paste0( symbols, " ", names, " ", cli::ansi_align(versions, max(cli::ansi_nchar(versions))) ) header <- cli::rule( left = cli::style_bold("Available Stan interfaces"), right = "setup_interface()" ) message_packages(packages, header) } update_check_message <- function() { deps <- stanflow_deps(FALSE, check_updates = TRUE) deps <- deps[ !(deps$package %in% c("ascribe", "withr", "knitr", "quarto", "testthat")), ] behind <- deps[deps$behind, , drop = FALSE] header <- cli::rule( left = cli::style_bold("Available updates"), right = "stanflow_update()" ) if (nrow(behind) == 0) { ok <- paste0( cli::col_green(cli::symbol$tick), " All stanflow packages up-to-date" ) return(paste0(header, "\n", ok)) } versions <- paste0( cli::col_silver(behind$local), " -> ", cli::col_green(behind$remote) ) versions <- cli::ansi_align(versions, max(cli::ansi_nchar(versions))) packages <- paste0( cli::col_red(cli::symbol$cross), " ", cli::col_blue(format(behind$package)), " ", versions ) message_packages(packages, header) } message_packages <- function(packages, header) { if (length(packages) %% 2 == 1) { packages <- append(packages, "") } col1 <- seq_len(length(packages) / 2) info <- paste0(packages[col1], " ", packages[-col1]) paste0(header, "\n", paste(info, collapse = "\n")) } .find_unloaded <- \(pkgs) pkgs[!paste0("package:", pkgs) %in% search()] .package_version_h <- function(pkg) { pkg |> utils::packageVersion() |> as.character() |> strsplit(".", fixed = TRUE) |> lapply(function(x) ifelse(x == "9000", cli::col_red(x), x)) |> vapply(paste, collapse = ".", FUN.VALUE = character(1)) }