如何使用{cli}自动缩进列表?

  • 本文关键字:缩进 列表 何使用 cli r
  • 更新时间 :
  • 英文 :


我想用{cli}创建两个文件列表。奇怪的是,我没有找到一个简单的方法来做这件事。现在,这是我所拥有的:

library(cli)
f <- function() {
cli::cli_div(theme = list(ul = list(`margin-left` = 2, before = "")))

cli::cli_text("this is my first list:")
cli::cli_ul()
for (i in c("a", "b", "c")) {
cli::cli_li("{.file {i}}")
}

cli::cli_text("this is my second list:")
cli::cli_ul()
for (i in c("d", "e", "f")) {
cli::cli_li("{.file {i}}")
}

cli::cli_alert_info("this is an info")
}
f()
#> this is my first list:
#>   • 'a'
#>   • 'b'
#>   • 'c'
#>   this is my second list:
#>       • 'd'
#>       • 'e'
#>       • 'f'
#>       ℹ this is an info

由reprex包(v2.0.1)创建于2022-06-10

如您所见,这里有一个格式问题。这是预期的输出:
#> this is my first list:
#>   • 'a'
#>   • 'b'
#>   • 'c'
#> this is my second list:
#>   • 'd'
#>   • 'e'
#>   • 'f'
#> ℹ this is an info

你知道有什么方法可以输出这个结果吗?

需要给每个cli_ul一个id,并使用这个id与cli_end关闭它们:

library(cli)
f <- function() {
cli::cli_div(theme = list(ul = list(`margin-left` = 2, before = "")))
cli::cli_text("this is my first list:")
cli::cli_ul(id = "foo")
for (i in c("a", "b", "c")) {
cli::cli_li("{.file {i}}")
}
cli::cli_end(id = "foo")

cli::cli_text("this is my first list:")
cli::cli_ul(id = "foo2")
for (i in c("a", "b", "c")) {
cli::cli_li("{.file {i}}")
}
cli::cli_end(id = "foo2")

cli::cli_alert_info("this is an info")
}
f()
#> this is my first list:
#>   • 'a'
#>   • 'b'
#>   • 'c'
#> this is my first list:
#>   • 'a'
#>   • 'b'
#>   • 'c'
#> ℹ this is an info

由reprex包(v2.0.1)创建于2022-06-10

最新更新