cli icon indicating copy to clipboard operation
cli copied to clipboard

custom theme styles ignored when all styled content is interpolated

Open dgkf opened this issue 10 months ago • 1 comments

some custom theme properties seem to only take effect if there is some raw text in the rendered content, ignored otherwise.

cli::cli_div(theme = list(
  ".example" = list(
    "background-color" = "red",                           # BUG?
    "font-weight" = "bold",                               # GOOD
    fmt = \(x) { cat("formatting:", x, "\n"); x },        # BUG?
    transform = \(x) { cat("transforming:", x, "\n"); x } # BUG?
  )
))
# GOOD: bold, background, transform, fmt
cli::cli_text("{.example abc}")
#> transforming: abc
#> formatting: abc
#> abc

# GOOD: bold, italics; BUG?: no background, transform, fmt
cli::cli_text("{.example {.emph abc}}")
#> abc

# GOOD: bold, italics, background, transform, fmt
cli::cli_text("{.example abc{.emph def}}")
#> transforming: abcdef
#> formatting: abcdef
#> abcdef

font-weight applies regardless, while the background-color, transform and fmt only apply if there exists some non-interpolated text in the span.

dgkf avatar Feb 07 '25 18:02 dgkf

Unfortunately the parsing in cli is kind of ad-hoc currently, and pretty hard to improve without losing compatibility. Nevertheless I am planning on improving it the next time I work on cli.

Btw. interestingly the opposite order seems to work:

cli::cli_text("{.emph {.example abc}}")
#> transforming: abc
#> formatting: abc
#> abc

gaborcsardi avatar Feb 10 '25 11:02 gaborcsardi