This function allows you to interact with a user by posing questions that
accept text input. It's inspired by usethis::ui_yeah()
and
shiny::textInput()
. It wraps base::readline()
with cli::cli_text()
allowing you to format questions using the cli
package.
cl_text_input(prompt, .envir = parent.frame(1))
prompt | A character string with the question to be asked. Passed into
|
---|---|
.envir | Used to ensure that |
A character string containing the user's response.
In general, I would recommend using cl_text_action
as it
accepts code to execute depending on the user's input, but this function is
perhaps more adaptable.
Other command-line-tools:
cl_text_action()
,
cl_yes_no_action()
,
cl_yes_no_lgl()
favorite_cat_question <- function() { answer <- cl_text_input("Name your {.strong favorite} cat.") if (tolower(answer) != "tucker") { cli::cli_alert_danger("\U1F63E Come meet my cat, Tucker, and I think you'll change your mind!") } else { cli::cli_alert_success("Correct answer! \U1F63B") } } favorite_dog_question <- function() { answer <- cl_text_input("Name your {.strong favorite} dog.") if (tolower(answer) != "ella") { cli::cli_alert_danger("\U1F44E Come meet my dog, Ella, and I think you'll change your mind!") } else { cli::cli_alert_success("Correct answer!\U1F436") } } if (interactive()) favorite_cat_question() if (interactive()) favorite_dog_question()