cli
cli copied to clipboard
Console width is not recognized at R session startup
I want to print a nice welcome message at the RStudio project startup and I noticed that in the very first phases of an R session startup, it behaves as if the console is 80 characters wide. Note that it may be in fact the correct value and RStudio is only sluggish to set the console properties so they match what we see in the console pane.
Set this as your .Rprofile and restart the session:
# .Rprofile
cli::console_width()
setHook("rstudio.sessionInit", function(newSession) {
print(cli::console_width())
}, action = "append")
Notice you get 80 no matter what in the first case. The second one is run only after the RStudio is fully available.
Glad to hear anyone else's opinion!
EDIT: Now I noticed in the cli source that 80 is the fallback value. Maybe this whole mission is not possible.
Yes, I am afraid that this is because RStudio hasn't set the console width yet, when the profile runs, so there isn't much we can do here. If you feel strongly about this, you can look at the rstudio/rstudio repository. It is possible that there is an issue about this already, and if not then you can open one, although I suspect that they cannot fix it at this point.
For there record, it seems like there is no rstudioapi::executeCommand() command that gives you the console width.
Thanks for the quick reply! I think I have just figured it out with:
# .Rprofile
local({
c_args <- commandArgs()
likely_rstudio <- basename(c_args[1L]) == "RStudio"
if (isTRUE(likely_rstudio)) {
setHook("rstudio.sessionInit", function(newSession) {
if (newSession) {
print(cli::console_width())
}
}, action = "append")
} else {
print(cli::console_width())
}
})
Maybe you can implement something of that sort into {cli}, but I'm not sure of the consequences. Nonetheless, I think for my purpose it'll suffice.
So what does this code do?
Oh, I'm sorry. First I get CLI arguments that were supplied when the R session was invoked (as ?commandArgs says):
c_args <- commandArgs()
likely_rstudio <- basename(c_args[1L]) == "RStudio"
likely_rstudio is TRUE if the R session was invoked by a program called RStudio (basename() removes any potential path before). It is not definite as I can rename my program to RStudio as well, but that really is an edge-case.
In case I think that RStudio invoked my R session, I can set up a hook described at https://docs.posit.co/ide/server-pro/rstudio_pro_sessions/session_startup_scripts.html or https://docs.posit.co/ide/server-pro/rstudio_pro_sessions/session_startup_scripts.html. So cli::console_width() would only run after the RStudio claims itself "available" and set up. (Ignore the newSession condition.) If the R session was invoked by */R, e.g. in the terminal, cli::console_width() is run as usual.
The point is to "wait" for RStudio to set up and then try to get the console width when it is likely we are dealing with RStudio.
FWIW, if you are already using cli, you can call cli:::rstudio_detect()$type to detect RStudio. It should return "rstudio_console_starting" or "rstudio_console" when you run it from the profile.
I am having a similar issue. I found this old RStudio issue that helps too https://github.com/rstudio/rstudio/issues/1579.
I think this issue can be closed in cli however, unless there are plans to make cli::console_width() to look in more places.
However, there could be a bullet added in the cli documentation about this.
unless there are plans to make
cli::console_width()to look in more places.
Where?
I had nothing particular in mind. I just meant that unless cli wants to hack its way into figuring out the console width on startup, then the issue could be closed.
Doc suggestion: Current: If we cannot determine the size of the terminal or console window, then we use the width option. If the width option is not set, then we return 80L. Suggestion: If we cannot determine the size of the terminal or console window, then we use the width option. If the width option is not set, then we return 80L. This is the case in startup script like Rprofile in RStudio where cli cannot determine the console width.