From 665c53fc391639d537d96489fc06453d56ae93cb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kirill=20M=C3=BCller?= Date: Thu, 30 Jun 2022 02:48:42 +0200 Subject: [PATCH] Avoid aligning NA inside quotes for very short character vectors --- R/shaft-.R | 12 ++++++++---- tests/testthat/_snaps/format_character.md | 10 +++++----- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/R/shaft-.R b/R/shaft-.R index 29c2f9223..c17d37915 100644 --- a/R/shaft-.R +++ b/R/shaft-.R @@ -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 @@ -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 ) } diff --git a/tests/testthat/_snaps/format_character.md b/tests/testthat/_snaps/format_character.md index 139a9d314..905bfb5e4 100644 --- a/tests/testthat/_snaps/format_character.md +++ b/tests/testthat/_snaps/format_character.md @@ -35,28 +35,28 @@ "" - + Code pillar(add_special(c(" ")), width = 5) Output " " - + Code pillar(add_special(c(" a")), width = 5) Output " a" - + Code pillar(add_special(c("a ")), width = 5) Output "a " - + Code pillar(add_special(c("a b")), width = 5) Output @@ -70,7 +70,7 @@ "\t" - + Code pillar(add_special(c("a\nb")), width = 10) Output