From da9fa64c96e9d0de1df85709c7eb1aeca6835f8a Mon Sep 17 00:00:00 2001 From: Andrew Heiss Date: Fri, 19 Sep 2025 15:03:53 -0400 Subject: [PATCH] Clarify a situation where console width cannot be determined Fixes #578 --- R/width.R | 15 ++++++++++----- man/console_width.Rd | 7 ++++--- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/R/width.R b/R/width.R index 5ae974196..434809afe 100644 --- a/R/width.R +++ b/R/width.R @@ -20,9 +20,10 @@ #' * We are _not_ using the `RSTUDIO_CONSOLE_WIDTH` environment variable #' if we are in the RStudio console. #' -#' 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. +#' If we cannot determine the size of the terminal or console window (e.g. if +#' `console_width()` is called in a startup `.Rprofile` script before a console +#' is present), then we use the `width` option. If the `width` option is not +#' set, then we return 80L. #' #' @return Integer scalar, the console with, in number of characters. #' @@ -102,7 +103,9 @@ tty_size <- function() { } terminal_width <- function() { - if (isTRUE(clienv$notaconsole)) return(NULL) + if (isTRUE(clienv$notaconsole)) { + return(NULL) + } w <- tryCatch( tty_size()[["width"]], error = function(e) { @@ -112,7 +115,9 @@ terminal_width <- function() { ) # this is probably a pty that does not set the width, use st sensible - if (!is.null(w) && w == 0) w <- 80L + if (!is.null(w) && w == 0) { + w <- 80L + } w } diff --git a/man/console_width.Rd b/man/console_width.Rd index 96682c10a..f6258d52f 100644 --- a/man/console_width.Rd +++ b/man/console_width.Rd @@ -35,9 +35,10 @@ get garbled. if we are in the RStudio console. } -If we cannot determine the size of the terminal or console window, then -we use the \code{width} option. If the \code{width} option is not set, then -we return 80L. +If we cannot determine the size of the terminal or console window (e.g. if +\code{console_width()} is called in a startup \code{.Rprofile} script before a console +is present), then we use the \code{width} option. If the \code{width} option is not +set, then we return 80L. } \examples{ console_width()