r-lib / gargle

Infrastructure for calling Google APIs from R, including auth
https://gargle.r-lib.org/
Other
113 stars 33 forks source link

Introduce `cli_menu()` and friends #242

Closed jennybc closed 1 year ago

jennybc commented 1 year ago

This is functionally important for gargle, because cli_menu() is ultimately implemented on top of readline(), not utils::menu(). And Jupyter has shimmed readline() to work even though, at the time of writing, base::interactive() returns FALSE in Jupyter.

Therefore this PR makes gargle's interactive choices work in Jupyter notebooks, which notably covers Google Colab.

This is part (but not all) of making user auth "just work" on Colab (#140).

I have adapted cli_menu() from https://github.com/rstudio/rsconnect/blob/main/R/utils-cli.R. I think most of my changes might be generally positive and could be considered when/if something like this goes into cli itself (https://github.com/r-lib/cli/issues/228). I also added tests.

@hadley @gaborcsardi I don't really need feedback on this (though it's welcome). This is more of an FYI request for review, to continue the discussion around https://github.com/r-lib/cli/issues/228. I'll make a few comments inline.


Links to the Jupyter + readline() story:

https://github.com/IRkernel/IRkernel/pull/452

https://github.com/IRkernel/IRkernel/blob/1eddb304b246c14b62949abd946e8d4ca5080d25/R/execution.r#L131-L137

https://github.com/IRkernel/IRkernel/blob/1eddb304b246c14b62949abd946e8d4ca5080d25/R/execution.r#L271-272

jennybc commented 1 year ago

I found a solution I like to the infinite loop problem, which is to append "0" to any mocked user input. And this retains the ability to provide multiple inputs.

The original intent was to be able to supply multiple values that were used in turn.

I also added a test for this behaviour.