Skip to content

Error messages using cli may wrap incorrectly for S7 property validators #6789

@dmuenz

Description

@dmuenz

In an S7 property validator, the error message is supposed to be "a single string" according to the docs, but I noticed that ggplot2 sometimes violates this. When you use {cli} to format the error message, it may cause a long message to wrap across multiple lines. Each line is then a separate string in a multi-string vector.

The consequence of this is that each line of the error message is prefaced with - @<property_name>, making it look perhaps like several error messages instead of just one long one. Here's an example (note that the line wrapping depends on the console width):

ggplot2::element_text(face = "foo")
#> Error: <ggplot2::element_text> object properties are invalid:
#> - @face must be one of "plain", "bold", "italic", "oblique", or
#> - @face "bold.italic".

I think this would be easier to read as:

#> Error: <ggplot2::element_text> object properties are invalid:
#> - @face must be one of "plain", "bold", "italic", "oblique", or
#>   "bold.italic".

If you agree that this is a (admittedly very small) problem, I think it could be fixed by revising your internal as_cli function to use paste(collapse), so the output would be a single string. Something like this:

as_cli <- function(..., env = parent.frame(), collapse = NULL) {
  cli::cli_text(..., .envir = env) |>
    cli::cli_fmt() |>
    paste(collapse = collapse)
}

long_string <- "I am a very {.emph long} string and I just keep going and going but now I'm done"

as_cli(long_string)
#> [1] "I am a very \033[3mlong\033[23m string and I just keep going and going"
#> [2] "but now I'm done"                                                      

as_cli(long_string, collapse = "\n")
#> [1] "I am a very \033[3mlong\033[23m string and I just keep going and going\nbut now I'm done"

Note that this preserves as_cli's current behavior if you leave collapse = NULL . When using this for S7 property validators, you could perhaps do as_cli(long_string, collapse = "\n ") to indent the wrapped line a bit.

Anyway, this is me being super picky about a very minor issue. Thanks for your consideration.

Metadata

Metadata

Assignees

No one assigned

    Labels

    messagesrequests for improvements to error, warning, or feedback messages

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions