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

Is it possible to use suppressPackageStartupMessages on cli message? #589

Open
JBGruber opened this issue Mar 13, 2023 · 3 comments
Open
Labels
feature a feature request or enhancement

Comments

@JBGruber
Copy link

Is there a way to write a suppressible startup message with cli? I'm writing a package for beginners of R and wanted to give some hints, but startup messages need to be suppressible to conform with CRAN guidelines. I had assumed something like this would work:

greet_startup_cli <- function() {
  cli::cli_alert_info("Hi, this is {.emph askgpt} {cli::symbol$smiley}.", class = "packageStartupMessage")
  cli::cli_li("To start error logging, run {.run [{.fun log_init}](askgpt::log_init())} now.", class = "packageStartupMessage")
  cli::cli_li("To see what you can do use {.help [{.fun ?askgpt}](askgpt::askgpt)}.", class = "packageStartupMessage")
  cli::cli_li("Or just run {.run [{.fun askgpt}](askgpt::askgpt())} with any question you want!", class = "packageStartupMessage")
}
suppressPackageStartupMessages(greet_startup_cli())
#> ℹ Hi, this is askgpt ☺.
#> • To start error logging, run `` `log_init()` `` now.
#> • To see what you can do use `?askgpt()` (`?askgpt::askgpt()`).
#> • Or just run `` `askgpt()` `` with any question you want!

Created on 2023-03-13 with reprex v2.0.2

Or maybe this:

greet_startup <- function() {
  cli::cli_alert_info("Hi, this is {.emph askgpt} {cli::symbol$smiley}.")
  cli::cli_li("To start error logging, run {.run [{.fun log_init}](askgpt::log_init())} now.")
  cli::cli_li("To see what you can do use {.help [{.fun ?askgpt}](askgpt::askgpt)}.")
  cli::cli_li("Or just run {.run [{.fun askgpt}](askgpt::askgpt())} with any question you want!")
}

suppressPackageStartupMessages(withr::with_options(list(cli.message_class = "packageStartupMessage"), greet_startup()))
#> ℹ Hi, this is askgpt ☺.
#> • To start error logging, run `` `log_init()` `` now.
#> • To see what you can do use `?askgpt()` (`?askgpt::askgpt()`).
#> • Or just run `` `askgpt()` `` with any question you want!

Created on 2023-03-13 with reprex v2.0.2

Am I missing something obvious?

@JBGruber
Copy link
Author

I looked at the approach tidyverse uses and pieced something together with rlang::inform and cli::format_inline, which I'm quite happy with:

greet_startup <- function() {
  msg <- paste0(
    c(
      "Hi, this is {.emph askgpt} {cli::symbol$smiley}.",
      "{cli::symbol$bullet} To start error logging, run {.run [{.fun log_init}](askgpt::log_init())} now.",
      "{cli::symbol$bullet} To see what you can do use {.help [{.fun ?askgpt}](askgpt::askgpt)}.",
      "{cli::symbol$bullet} Or just run {.run [{.fun askgpt}](askgpt::askgpt())} with any question you want!"
    ),
    collapse = "\n"
  )
  rlang::inform(cli::format_inline(msg), class = "packageStartupMessage")
}


greet_startup()
#> Hi, this is askgpt ☺.
#> • To start error logging, run `` `log_init()` `` now.
#> • To see what you can do use `?askgpt()` (`?askgpt::askgpt()`).
#> • Or just run `` `askgpt()` `` with any question you want!
suppressPackageStartupMessages(greet_startup())

Created on 2023-03-14 with reprex v2.0.2

It would be cool to get a way of doing this just in cli, but I assume you already have this in mind, so I'm closing the issue.

@gaborcsardi
Copy link
Member

There is no good way, currently, but it is easy to add an option for this.

@gaborcsardi gaborcsardi reopened this Mar 14, 2023
@gaborcsardi gaborcsardi added the feature a feature request or enhancement label Oct 31, 2023
@dsweber2
Copy link

So... it looks like someone fixed this? At least, if you use cli_inform:

library(cli)

greet_startup_cli <- function() {
  cli_inform("Hello, world!", class = "packageStartupMessage")
}
suppressPackageStartupMessages(greet_startup_cli())

Seems to work!

@gaborcsardi gaborcsardi added the tidy-dev-day 🤓 Tidyverse Developer Day rstd.io/tidy-dev-day label Jul 17, 2024
@DavisVaughan DavisVaughan removed the tidy-dev-day 🤓 Tidyverse Developer Day rstd.io/tidy-dev-day label Aug 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature a feature request or enhancement
Projects
None yet
Development

No branches or pull requests

4 participants