Skip to content

Commit 3de7ad6

Browse files
authored
Merge pull request #561 from r-lib/fix/format-inline-keep-newline
2 parents c317cee + 542dc86 commit 3de7ad6

File tree

5 files changed

+74
-3
lines changed

5 files changed

+74
-3
lines changed

NEWS.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
unconditionally, as before.) See the the `cli.progress_show_after`
1212
option in `?cli-config` for details (#542).
1313

14+
* `format_inline()` now has a new argument `keep_newlines`, and it keeps
15+
newline characters by default.
16+
1417
# cli 3.5.0
1518

1619
* New `keypress()` function to read a single key press from a terminal.

R/cli.R

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,17 +102,24 @@ cli_fmt <- function(expr, collapse = FALSE, strip_newline = FALSE) {
102102
#' @param .envir Environment to evaluate the expressions in.
103103
#' @param collapse Whether to collapse the result if it has multiple
104104
#' lines, e.g. because of `\f` characters.
105+
#' @param keep_newlines Whether to keep newlines in the result, or treat
106+
#' them as regular space characters.
105107
#' @return Character scalar, the formatted string.
106108
#'
107109
#' @export
108110
#' @examples
109111
#' format_inline("A message for {.emph later}, thanks {.fn format_inline}.")
110112

111-
format_inline <- function(..., .envir = parent.frame(), collapse = TRUE) {
113+
format_inline <- function(..., .envir = parent.frame(), collapse = TRUE,
114+
keep_newlines = TRUE) {
115+
str <- paste0(unlist(list(...), use.names = FALSE), collapse = "")
116+
if (keep_newlines) {
117+
str <- gsub("\n", "\f", str, fixed = TRUE)
118+
}
112119
opts <- options(cli.width = Inf)
113120
on.exit(options(opts), add = TRUE)
114121
cli_fmt(
115-
cli_text(..., .envir = .envir),
122+
cli_text(str, .envir = .envir),
116123
collapse = collapse,
117124
strip_newline = TRUE
118125
)

man/format_inline.Rd

Lines changed: 9 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/_snaps/inline-2.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,3 +398,41 @@
398398
Caused by error:
399399
! non-numeric argument to binary operator
400400

401+
# format_inline and newlines
402+
403+
Code
404+
format_inline("foo\nbar", keep_newlines = TRUE)
405+
Output
406+
[1] "foo\nbar"
407+
Code
408+
format_inline("\nfoo\n\nbar\n", keep_newlines = TRUE)
409+
Output
410+
[1] "\nfoo\n\nbar\n"
411+
Code
412+
format_inline("foo\fbar", keep_newlines = TRUE)
413+
Output
414+
[1] "foo\nbar"
415+
Code
416+
format_inline("\ffoo\f\fbar\f", keep_newlines = TRUE)
417+
Output
418+
[1] "\nfoo\n\nbar\n"
419+
420+
---
421+
422+
Code
423+
format_inline("foo\nbar", keep_newlines = FALSE)
424+
Output
425+
[1] "foo bar"
426+
Code
427+
format_inline("\nfoo\n\nbar\n", keep_newlines = FALSE)
428+
Output
429+
[1] "foo\n\nbar"
430+
Code
431+
format_inline("foo\fbar", keep_newlines = FALSE)
432+
Output
433+
[1] "foo\nbar"
434+
Code
435+
format_inline("\ffoo\f\fbar\f", keep_newlines = FALSE)
436+
Output
437+
[1] "\nfoo\n\nbar\n"
438+

tests/testthat/test-inline-2.R

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,3 +181,18 @@ test_that("various errors", {
181181
transform = function(x) sanitize_call(sanitize_srcref(x))
182182
)
183183
})
184+
185+
test_that("format_inline and newlines", {
186+
expect_snapshot({
187+
format_inline("foo\nbar", keep_newlines = TRUE)
188+
format_inline("\nfoo\n\nbar\n", keep_newlines = TRUE)
189+
format_inline("foo\fbar", keep_newlines = TRUE)
190+
format_inline("\ffoo\f\fbar\f", keep_newlines = TRUE)
191+
})
192+
expect_snapshot({
193+
format_inline("foo\nbar", keep_newlines = FALSE)
194+
format_inline("\nfoo\n\nbar\n", keep_newlines = FALSE)
195+
format_inline("foo\fbar", keep_newlines = FALSE)
196+
format_inline("\ffoo\f\fbar\f", keep_newlines = FALSE)
197+
})
198+
})

0 commit comments

Comments
 (0)