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

feat: Avoid aligning NA inside quotes for very short character vectors #562

Merged
merged 1 commit into from
Jun 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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