Skip to content

Commit

Permalink
Proper display of control characters (#589)
Browse files Browse the repository at this point in the history
  • Loading branch information
krlmlr authored Feb 21, 2023
1 parent 6b0aa04 commit 53dd647
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 4 deletions.
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ Suggests:
knitr,
pillar (>= 1.4.0),
rmarkdown,
testthat (>= 3.0.0)
testthat (>= 3.0.0),
utf8
LinkingTo:
cpp11
VignetteBuilder:
Expand Down
8 changes: 5 additions & 3 deletions R/labelled-pillar.R
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,8 @@ lbl_pillar_info <- function(x) {
MIN_LBL_DISPLAY <- 6
labels <- attr(x, "labels")
if (length(labels) > 0) {
names(labels) <- pillar::style_subtle(paste0(" [", names(labels), "]"))
encoded <- utf8::utf8_encode(names(labels))
names(labels) <- pillar::style_subtle(paste0(" [", encoded, "]"))
attr(x, "labels") <- labels
label_display <- as.character(as_factor(x, "labels"))
label_display[is.na(label_display)] <- ""
Expand Down Expand Up @@ -203,6 +204,7 @@ paste_with_align <- function(x, y, lhs_ws, rhs_ws) {
}

pillar_print_pkgs_available <- function() {
requireNamespace("crayon", quietly = TRUE) &
requireNamespace("cli", quietly = TRUE)
requireNamespace("crayon", quietly = TRUE) &&
requireNamespace("cli", quietly = TRUE) &&
requireNamespace("utf8", quietly = TRUE)
}
32 changes: 32 additions & 0 deletions tests/testthat/_snaps/labelled-pillar.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,3 +123,35 @@
10 10 (NA) [Refused]
11 NA

---

Code
x <- labelled(c("spaces", "tabs", "newlines", "c0", "quote", "backslash"), c(
`a b` = "spaces", `a\tb` = "tabs", `a\nb` = "newlines", `a\001b` = "c0",
`a"b` = "quote", `a\\b` = "backslash"))
tibble::tibble(x)
Output
# A tibble: 6 x 1
x
<chr+lbl>
1 spaces [a b]
2 tabs [a\tb]
3 newlines [a\nb]
4 c0 [a\u0001b]
5 quote [a"b]
6 backslash [a\\b]

---

Code
x <- "c1"
label <- x
names(label) <- "a\u0080b"
x <- labelled(x, label)
tibble::tibble(x)
Output
# A tibble: 1 x 1
x
<chr+lbl>
1 c1 [a\u0080b]

31 changes: 31 additions & 0 deletions tests/testthat/test-labelled-pillar.R
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,35 @@ test_that("pillar", {
)
tibble::tibble(x)
})

expect_snapshot({
x <- labelled(
c(
"spaces",
"tabs",
"newlines",
"c0",
"quote",
"backslash"
),
c(
"a b" = "spaces",
"a\tb" = "tabs",
"a\nb" = "newlines",
"a\u0001b" = "c0",
'a"b' = "quote",
"a\\b" = "backslash"
)
)
tibble::tibble(x)
})

skip_on_os("windows")
expect_snapshot({
x <- "c1"
label <- x
names(label) <- "a\u0080b"
x <- labelled(x, label)
tibble::tibble(x)
})
})

0 comments on commit 53dd647

Please sign in to comment.