Skip to content

Revise as_cli to return a single string without \n characters#6866

Open
dmuenz wants to merge 3 commits into
tidyverse:mainfrom
dmuenz:as-cli-collapse
Open

Revise as_cli to return a single string without \n characters#6866
dmuenz wants to merge 3 commits into
tidyverse:mainfrom
dmuenz:as-cli-collapse

Conversation

@dmuenz

@dmuenz dmuenz commented Jun 9, 2026

Copy link
Copy Markdown

Attempts to fix #6789. This PR revises the internal helper function as_cli, which is used to richly format text for printing to the console.

Previously, as_cli(...) could return a character vector of length > 1 if the text in ... was longer than the console width. That's a problem for S7 property validators which expect a string (i.e. a length-1 character vector) for signaling an error. So this PR aims to fix that by

  1. using the cli::cli_fmt(collapse = TRUE, strip_newline = TRUE) options which ensure we get a string but which may have internal "\n" characters, and

  2. locally setting the cli.width global option to an enormous number so that cli::cli_fmt thinks the console is enormously wide and thus does not insert any "\n"s.

The reason I don't want cli::cli_fmt to insert newline characters is that the function generally doesn't have access to the complete text to be printed, and thus it will insert newlines in the wrong places. E.g., with S7 property validators, the text "- @prop_name " is prepended to the error message, but cli doesn't account for the length of that text which can lead to strange-looking line wrapping.

Regarding testing, I added a new direct test of the revised as_cli behaviour in test-utilities.R. I also revised a couple tests of S7 property validators in test-properties.R which previously passed but only because they weren't testing the error messages with a narrow console width. Now the tests simulate a narrow console and they still pass but only because of the revision to as_cli.

I ran the whole suite of package tests and they all passed (except for a few that were skipped because they're OS-specific), including tests of the behavior of other functions that use as_cli, including check_required_aesthetics and validate_subclass.

I think this is ready for review at your convenience.

dmuenz added 3 commits June 8, 2026 21:21
…k characters

* Technically if the ... text width is greater than 1e9 then we could get line breaks, but that's unrealistic. I considered setting cli.width to Inf, but the cli package documentation doesn't explicitly say that Inf is a supported value.
This test would have failed prior to as_cli being revised
The tests with "bad input" previously passed, but only because the default console width was wide enough to mask the problem with line breaks. By locally setting cli.width to a small number (20), the "bad input" tests would fail without the revision to as_cli.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Error messages using cli may wrap incorrectly for S7 property validators

1 participant