cli icon indicating copy to clipboard operation
cli copied to clipboard

Handling `...` to create an ideal alert

Open asadow opened this issue 2 years ago • 0 comments

I'm struggling to define a custom alert that reflects an API call being made and returning no data.

I'd like the user to be informed of what parameters and values were defined in the request. I've included this code below.

The message I'd like is

#> ℹ No data for:
#> • `endpoint = "some_endpoint"` AND
#> • `date = <date-types>, …, or 2023-12-31`
#>   AND
#> • `trade = "CUSTODIAL" or "ADMINISTRATION"`
#> # A tibble: 0 × 0

But I can't do two things in my reprex below.

  1. Use "or" instead of "and" as the last separator
  2. Print dates readably

How should I try to customize my message, or is this outside the intended limits of cli?

library(cli)
library(stringr)
library(rlang)
api_get <- function(endpoint, ...) {
  params <- rlang::list2(...)
  page_1 <- NULL

  if (is.null(page_1)) {

    par_info <- paste0("{.arg ", names(params),
                            " = {.val {", params, "}}} AND")
    par_info[length(par_info)] <- par_info[length(par_info)] |>
      stringr::str_remove("AND$")

    stars <- rep("*", length(params))
    names(par_info) <- stars

    cli::cli_alert_info("No data for:")

    cli::cli_bullets(c(
      "*" = "{.arg endpoint = {.val {endpoint}}} AND",
      par_info
    ))

    return(tibble::tibble())

  }
}
dec_2023 <- seq.Date(
  as.Date("2023-01-01"),
  as.Date("2023-01-31"),
  by = "day"
)
trade <- c("CUSTODIAL", "ADMINISTRATION")
params <- list(date = dec_2023, trade = trade)

api_get("some_endpoint", !!!params)
#> ℹ No data for:
#> • `endpoint = "some_endpoint"` AND
#> • `date = 19358, 19359, 19360, 19361, 19362, 19363, 19364, 19365, 19366, 19367,
#>   19368, 19369, 19370, 19371, 19372, 19373, 19374, 19375, …, 19387, and 19388`
#>   AND
#> • `trade = "CUSTODIAL" and "ADMINISTRATION"`
#> # A tibble: 0 × 0

Created on 2023-11-16 with reprex v2.0.2

asadow avatar Nov 16 '23 20:11 asadow