Skip to content

Feature suggestion: cli_ask function combining cli::cli_alert + readline #488

Open
@elipousson

Description

@elipousson

I created a simple utility function that I thought may be helpful to consider as a possible addition to the cli package. I know there is already a related issue w/ #228 and you have similar functionality in your separate {ask} package but figured I'd open the issue for consideration. I'm using it to offer the user an opportunity to input a URL but I can imagine a variety of potential use cases. Here is the code I have currently:

cli_ask <- function(text, prompt = ">> ", ..., .envir = parent.frame()) {
  cli::cli_alert(text, ..., .envir = .envir)
  readline(prompt = prompt)
}

If you're interested, let me know and I'm happy to open a pull request.

I also created a cli_yeah function that is a only very slightly modified version of the usethis::ui_yeah function but I saw you previously flagged this function as "not relevant" for {cli} in the comments for #68. If you're interested in revisiting this, I'm happy to include it in the same PR with cli_ask. Here is the code:

cli_yeah <- function(text,
                     yes = c("Yes", "Definitely", "For sure", "Yup", "Yeah", "I agree", "Absolutely"),
                     no = c("No way", "Not now", "Negative", "No", "Nope", "Absolutely not"),
                     n_yes = 1,
                     n_no = 2,
                     shuffle = TRUE,
                     .envir = parent.frame()) {
  x <- glue::glue_collapse(text, "\n")
  x <- glue::glue(text, .envir = .envir)

  if (!rlang::is_interactive()) {
    cli::cli_abort(
      c(
        "User input required, but session is not interactive.",
        "Query: {text}"
      )
    )
  }

  n_yes <- min(n_yes, length(yes))
  n_no <- min(n_no, length(no))
  qs <- c(sample(yes, n_yes), sample(no, n_no))

  if (shuffle) {
    qs <- sample(qs)
  }

  cli::cli_alert(text)
  out <- utils::menu(qs)
  out != 0L && qs[[out]] %in% yes
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    featurea feature request or enhancement

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions