Skip to content

Commit

Permalink
Merge pull request #562 from r-lib/f-556-na-quotes
Browse files Browse the repository at this point in the history
  • Loading branch information
krlmlr authored Jun 30, 2022
2 parents 99cf7ec + 665c53f commit dc75144
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 9 deletions.
12 changes: 8 additions & 4 deletions R/shaft-.R
Original file line number Diff line number Diff line change
Expand Up @@ -299,8 +299,10 @@ pillar_shaft.character <- function(x, ..., min_width = NULL) {
# Add subtle quotes if necessary
needs_quotes <- which(is_ambiguous_string(x))
if (has_length(needs_quotes)) {
out[needs_quotes] <- gsub('"', '\\"', x[needs_quotes], fixed = TRUE)
out[!is.na(x)] <- paste0(style_subtle('"'), out[!is.na(x)], style_subtle('"'))
quoted <- gsub('"', '\\"', x[needs_quotes], fixed = TRUE)
out[needs_quotes] <- quoted
valid <- paste0(style_subtle('"'), out[!is.na(x)], style_subtle('"'))
out[!is.na(x)] <- valid
na_indent <- 1
} else {
na_indent <- 0
Expand All @@ -320,12 +322,14 @@ pillar_shaft.character <- function(x, ..., min_width = NULL) {
pillar_shaft.glue <- function(x, ..., min_width = NULL, na_indent = 0L, shorten = NULL) {
min_width <- max(min_width, 3L)
width <- get_max_extent(x)
na <- pillar_na(use_brackets_if_no_color = TRUE)
force(na_indent)

new_pillar_shaft_simple(
x,
width = width, align = "left", min_width = min(width, min_width),
na = pillar_na(use_brackets_if_no_color = TRUE),
na_indent = na_indent,
na = na,
na_indent = if (width > get_extent(na)) na_indent else 0,
shorten = shorten
)
}
Expand Down
10 changes: 5 additions & 5 deletions tests/testthat/_snaps/format_character.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,28 +35,28 @@
<pillar>
<chr>
""
<NA>
<NA>
Code
pillar(add_special(c(" ")), width = 5)
Output
<pillar>
<chr>
" "
<NA>
<NA>
Code
pillar(add_special(c(" a")), width = 5)
Output
<pillar>
<chr>
" a"
<NA>
<NA>
Code
pillar(add_special(c("a ")), width = 5)
Output
<pillar>
<chr>
"a "
<NA>
<NA>
Code
pillar(add_special(c("a b")), width = 5)
Output
Expand All @@ -70,7 +70,7 @@
<pillar>
<chr>
"\t"
<NA>
<NA>
Code
pillar(add_special(c("a\nb")), width = 10)
Output
Expand Down

0 comments on commit dc75144

Please sign in to comment.