Skip to content

Commit a7c63c2

Browse files
committed
format_inline() now keeps all whitespace as is by default
And has a new argument: `keep_whitespace` to choose the old behavior.
1 parent 7eb58fe commit a7c63c2

File tree

9 files changed

+67
-44
lines changed

9 files changed

+67
-44
lines changed

NEWS.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
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.
14+
* `format_inline()` now has a new argument `keep_whitespace`, and it keeps
15+
whitespace, including newline and form feed characters by default.
1616

1717
# cli 3.5.0
1818

R/cli.R

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -102,24 +102,21 @@ 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.
105+
#' @param keep_whitespace Whether to keep all whitepace (spaces, newlines
106+
#' and form feeds) as is in the input.
107107
#' @return Character scalar, the formatted string.
108108
#'
109109
#' @export
110110
#' @examples
111111
#' format_inline("A message for {.emph later}, thanks {.fn format_inline}.")
112112

113113
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-
}
114+
keep_whitespace = TRUE) {
119115
opts <- options(cli.width = Inf)
120116
on.exit(options(opts), add = TRUE)
117+
fun <- if (keep_whitespace) cli_inline else cli_text
121118
cli_fmt(
122-
cli_text(str, .envir = .envir),
119+
fun(..., .envir = .envir),
123120
collapse = collapse,
124121
strip_newline = TRUE
125122
)
@@ -208,6 +205,15 @@ cli_text <- function(..., .envir = parent.frame()) {
208205
cli__message("text", list(text = glue_cmd(..., .envir = .envir, .call = sys.call())))
209206
}
210207

208+
cli_inline <- function(..., .envir = parent.frame()) {
209+
cli__message(
210+
"inline_text",
211+
list(
212+
text = glue_cmd(..., .envir = .envir, .call = sys.call(), .trim = FALSE)
213+
)
214+
)
215+
}
216+
211217
#' CLI verbatim text
212218
#'
213219
#' It is not wrapped, but printed as is. Long lines will overflow.

R/cliapp.R

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ cliapp <- function(theme = getOption("cli.theme"),
4040
clii_text(app, text),
4141

4242
## Text, not wrapped
43+
inline_text = function(text)
44+
clii_inline_text(app, text),
45+
46+
## Text, not wrapped, verbatim
4347
verbatim = function(...)
4448
clii_verbatim(app, ...),
4549

@@ -114,9 +118,9 @@ cliapp <- function(theme = getOption("cli.theme"),
114118
get_current_style = function()
115119
utils::tail(app$styles, 1)[[1]],
116120

117-
xtext = function(text = NULL, .list = NULL, indent = 0, padding = 0)
121+
xtext = function(text = NULL, .list = NULL, indent = 0, padding = 0, wrap = TRUE)
118122
clii__xtext(app, text, .list = .list, indent = indent,
119-
padding = padding),
123+
padding = padding, wrap = wrap),
120124

121125
vspace = function(n = 1)
122126
clii__vspace(app, n),
@@ -163,6 +167,10 @@ clii_text <- function(app, text) {
163167
app$xtext(text)
164168
}
165169

170+
clii_inline_text <- function(app, text) {
171+
app$xtext(text, wrap = FALSE)
172+
}
173+
166174
clii_verbatim <- function(app, ..., .envir) {
167175
style <- app$get_current_style()
168176
text <- unlist(strsplit(unlist(list(...)), "\n", fixed = TRUE))

R/glue.R

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
glue <- function(text, .envir = parent.frame(),
1313
.transformer = identity_transformer,
14-
.open = "{", .close = "}", .cli = FALSE) {
14+
.open = "{", .close = "}", .cli = FALSE, .trim = TRUE) {
1515

1616
text <- paste0(text, collapse = "")
1717

@@ -23,7 +23,9 @@ glue <- function(text, .envir = parent.frame(),
2323
return(text)
2424
}
2525

26-
text <- trim(text)
26+
if (.trim) {
27+
text <- trim(text)
28+
}
2729

2830
f <- function(expr) {
2931
eval_func <- as.character(.transformer(expr, .envir) %||% character())

R/inline.R

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,8 @@ clii__inline <- function(app, text, .list) {
231231
.envir = t$values,
232232
.transformer = inline_transformer,
233233
.open = paste0("<", t$values$marker),
234-
.close = paste0(t$values$marker, ">")
234+
.close = paste0(t$values$marker, ">"),
235+
.trim = FALSE
235236
)
236237
})
237238
paste(out, collapse = "")
@@ -323,15 +324,16 @@ make_cmd_transformer <- function(values, .call = NULL) {
323324
}
324325
}
325326

326-
glue_cmd <- function(..., .envir, .call = sys.call(-1)) {
327+
glue_cmd <- function(..., .envir, .call = sys.call(-1), .trim = TRUE) {
327328
str <- paste0(unlist(list(...), use.names = FALSE), collapse = "")
328329
values <- new.env(parent = emptyenv())
329330
transformer <- make_cmd_transformer(values, .call = .call)
330331
pstr <- glue(
331332
str,
332333
.envir = .envir,
333334
.transformer = transformer,
334-
.cli = TRUE
335+
.cli = TRUE,
336+
.trim = .trim
335337
)
336338
glue_delay(
337339
str = post_process_plurals(pstr, values),

R/internals.R

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ call_if_fun <- function(x) {
33
if (is.function(x)) x() else x
44
}
55

6-
clii__xtext <- function(app, text, .list, indent, padding, ln = TRUE) {
6+
clii__xtext <- function(app, text, .list, indent, padding, ln = TRUE, wrap = TRUE) {
77
style <- app$get_current_style()
88
text <- app$inline(text, .list = .list)
99
exdent <- style$`text-exdent` %||% 0L
@@ -17,11 +17,15 @@ clii__xtext <- function(app, text, .list, indent, padding, ln = TRUE) {
1717

1818
if (!is.null(style$fmt)) text <- style$fmt(text)
1919

20-
text <- ansi_strwrap(
21-
text,
22-
exdent = exdent,
23-
width = app$get_width(extra = padding)
24-
)
20+
if (wrap) {
21+
text <- ansi_strwrap(
22+
text,
23+
exdent = exdent,
24+
width = app$get_width(extra = padding)
25+
)
26+
} else {
27+
text <- ansi_simplify(text)
28+
}
2529

2630
app$cat_ln(text, indent = indent, padding)
2731
invisible(app)

man/format_inline.Rd

Lines changed: 3 additions & 3 deletions
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: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -401,38 +401,38 @@
401401
# format_inline and newlines
402402

403403
Code
404-
format_inline("foo\nbar", keep_newlines = TRUE)
404+
format_inline("foo\nbar")
405405
Output
406406
[1] "foo\nbar"
407407
Code
408-
format_inline("\nfoo\n\nbar\n", keep_newlines = TRUE)
408+
format_inline("\nfoo\n\nbar\n")
409409
Output
410410
[1] "\nfoo\n\nbar\n"
411411
Code
412-
format_inline("foo\fbar", keep_newlines = TRUE)
412+
format_inline("foo\fbar")
413413
Output
414-
[1] "foo\nbar"
414+
[1] "foo\fbar"
415415
Code
416-
format_inline("\ffoo\f\fbar\f", keep_newlines = TRUE)
416+
format_inline("\ffoo\f\fbar\f")
417417
Output
418-
[1] "\nfoo\n\nbar\n"
418+
[1] "\ffoo\f\fbar\f"
419419

420420
---
421421

422422
Code
423-
format_inline("foo\nbar", keep_newlines = FALSE)
423+
format_inline("foo\nbar", keep_whitespace = FALSE)
424424
Output
425425
[1] "foo bar"
426426
Code
427-
format_inline("\nfoo\n\nbar\n", keep_newlines = FALSE)
427+
format_inline("\nfoo\n\nbar\n", keep_whitespace = FALSE)
428428
Output
429429
[1] "foo\n\nbar"
430430
Code
431-
format_inline("foo\fbar", keep_newlines = FALSE)
431+
format_inline("foo\fbar", keep_whitespace = FALSE)
432432
Output
433433
[1] "foo\nbar"
434434
Code
435-
format_inline("\ffoo\f\fbar\f", keep_newlines = FALSE)
435+
format_inline("\ffoo\f\fbar\f", keep_whitespace = FALSE)
436436
Output
437437
[1] "\nfoo\n\nbar\n"
438438

tests/testthat/test-inline-2.R

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -184,15 +184,16 @@ test_that("various errors", {
184184

185185
test_that("format_inline and newlines", {
186186
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)
187+
format_inline("foo\nbar")
188+
format_inline("\nfoo\n\nbar\n")
189+
format_inline("foo\fbar")
190+
format_inline("\ffoo\f\fbar\f")
191191
})
192+
192193
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)
194+
format_inline("foo\nbar", keep_whitespace = FALSE)
195+
format_inline("\nfoo\n\nbar\n", keep_whitespace = FALSE)
196+
format_inline("foo\fbar", keep_whitespace = FALSE)
197+
format_inline("\ffoo\f\fbar\f", keep_whitespace = FALSE)
197198
})
198199
})

0 commit comments

Comments
 (0)