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

Console width is not recognized at R session startup #578

Open
netique opened this issue Feb 3, 2023 · 9 comments
Open

Console width is not recognized at R session startup #578

netique opened this issue Feb 3, 2023 · 9 comments
Labels
documentation tidy-dev-day 🤓 Tidyverse Developer Day rstd.io/tidy-dev-day

Comments

@netique
Copy link

netique commented Feb 3, 2023

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.

@gaborcsardi
Copy link
Member

gaborcsardi commented Feb 3, 2023

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.

@gaborcsardi
Copy link
Member

For there record, it seems like there is no rstudioapi::executeCommand() command that gives you the console width.

@netique
Copy link
Author

netique commented Feb 3, 2023

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.

@gaborcsardi
Copy link
Member

So what does this code do?

@netique
Copy link
Author

netique commented Feb 3, 2023

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.

@gaborcsardi
Copy link
Member

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.

@olivroy
Copy link
Contributor

olivroy commented Aug 30, 2023

I am having a similar issue.
I found this old RStudio issue that helps too rstudio/rstudio#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.

@gaborcsardi
Copy link
Member

unless there are plans to make cli::console_width() to look in more places.

Where?

@olivroy
Copy link
Contributor

olivroy commented Aug 30, 2023

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation tidy-dev-day 🤓 Tidyverse Developer Day rstd.io/tidy-dev-day
Projects
None yet
Development

No branches or pull requests

3 participants