Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
elipousson opened this issue Jun 23, 2022 · 2 comments
Open
Labels
feature a feature request or enhancement

Comments

@elipousson
Copy link

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
}
@gaborcsardi
Copy link
Member

Thanks, I'll will have to think about this more, but hopefully will get to it soon.

@gaborcsardi gaborcsardi added the feature a feature request or enhancement label Oct 31, 2023
@JosiahParry
Copy link

Came here to see if this functionality existed in cli yet.

I was hoping to use cli to create a series of prompts to help create a metadata file. Something along the lines of

widget_type <- menu(c("fizz", "buzz"), title = "Which kind of widget?")
widget_name <- cli::cli_ask("Give your widget a name")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature a feature request or enhancement
Projects
None yet
Development

No branches or pull requests

3 participants