-
Notifications
You must be signed in to change notification settings - Fork 70
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
Alternative to utils::menu()
?
#228
Comments
Yes, definitely. Some requirements would be very helpful, thanks! #119 is related. |
OK, I will start adding thoughts incrementally. Similar to
As it stands with I'm using It would be best if the front matter were handled holistically, with the rest of the menu-making elements, e.g. choices. This also seems to fit nicely with the semantic UI goals. Yes, I know about the |
It feels like there should be a standard (probably optional) footer, explaining how to decline to make a choice, e.g. ESC or entering 0. Styled in a suitably subtle way. |
It would be great to have a default choice that one could accept just by pressing "enter". |
The "enter this" codes should? could? be customizable:
|
This would be very helpful. Any chance that will be available on the next version of |
Some things that I think would make menus more user-friendly but which may (probably?) be harder to implement consistently across all terminal types/environments. These would also go beyond just minor improvements to
Presumably for terminals which can't have earlier parts overwritten, 1 and 2 would be disabled. |
@CoryMcCartan Good ideas!
Given that few people would benefit from 2-3 I would not make them a priority for the first implementation. |
Thank you, that makes sense! Just to be clear (and this may not change the doability / priority of it), for 2 the cursor itself wouldn't need to move, you'd just need to be able to listen for keypresses at the prompt & update the visual marker in 1 accordingly. |
To update the visual marker you need to move the cursor to the marker first. |
I was just looking into interactive terminal packages for R and came across this repo. Is there any news on menus? Maybe to add inspiration; I love the way ESLint (a linter in the JS world), creates their interactive terminals: |
If there will be news on this, you'll see it in this issue. :) Yeah, the JS ecosystem has a lot of tools that makes this easier, e.g. https://www.npmjs.com/package/enquirer and a whole terminal handling stack as well. The menus are nice, but sadly they are not possible in RStudio, only in a real terminal, which makes them much less important. In the terminal it is not hard to implement them, here is a poc: https://github.com/gaborcsardi/ask |
That is great! I think |
It'll be in cli at some point, but the fancy terminal stuff is not high priority because it only works in terminals. |
I know that this is a bit off-topic; yet, I'm wondering about the underlying reason for why this interactive stuff works better in the terminal and worse in RStudio? So ultimately, that is something RStudio needs to address? |
I don't think that will happen. The RStudio console is not a terminal, and it is very unlikely that it will turn into one. If we want menus, etc. in RStudio we could potentially use addins. |
A small but useful feature is using |
This is what I've come up with: cli_menu <- function(prompt, not_interactive, choices, quit = integer(), .envir = caller_env()) {
if (!is_interactive()) {
cli::cli_abort(c(prompt, not_interactive), .envir = .envir)
}
choices <- sapply(choices, cli::format_inline, .envir = .envir, USE.NAMES = FALSE)
choices <- paste0(seq_along(choices), " ", choices)
cli::cli_inform(
c(prompt, "What do you want to do?", choices),
.envir = .envir
)
repeat {
selected <- readline("Selection: ")
if (selected %in% c("0", seq_along(choices))) {
break
}
cli::cli_inform("Enter an item from the menu, or 0 to exit")
}
selected <- as.integer(selected)
if (selected %in% c(0, quit)) {
cli::cli_abort("Quiting...", call = NULL)
}
selected
} Compared to my previous comment, the additional thing I've realised is that it's useful to mock cli_readline <- function(prompt) {
testing <- getOption("cli_prompt", character())
if (length(testing) > 0) {
selected <- testing[[1]]
cli::cli_inform(paste0(prompt, ": ", selected))
options(cli_prompt = testing[-1])
selected
} else {
readline("Selection: ")
}
} |
Probably want some kind of helper like |
Btw. don't call |
@gaborcsardi thanks; that was a remnant of an older approach. |
Semi-related to #151
More than once, when working on package UI, I've wished for a better version of
utils::menu()
?Do you think that could fit here? If so, I'll start to jot down a wish list as specifics come up.
The text was updated successfully, but these errors were encountered: