From c8d22d84042c395fc6e474a02d5e95cb349a815a Mon Sep 17 00:00:00 2001 From: olivroy Date: Fri, 28 Jun 2024 12:11:42 -0400 Subject: [PATCH 1/8] Use `fansi::strip_ctl()` instead of deprecated `fansi::strip_sgr()` --- NAMESPACE | 2 +- R/extent.R | 4 ++-- R/ggplot2.R | 2 +- R/utils.R | 2 +- R/vctr.R | 2 +- 5 files changed, 6 insertions(+), 6 deletions(-) diff --git a/NAMESPACE b/NAMESPACE index 471963e14..9e63002d1 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -160,7 +160,7 @@ export(type_sum) import(lifecycle) import(rlang) importFrom(cli,symbol) -importFrom(fansi,strip_sgr) +importFrom(fansi,strip_ctl) importFrom(fansi,substr2_ctl) importFrom(glue,as_glue) importFrom(utf8,utf8_width) diff --git a/R/extent.R b/R/extent.R index e6956dce2..9d0a9ea23 100644 --- a/R/extent.R +++ b/R/extent.R @@ -5,14 +5,14 @@ #' #' @param x A character vector. #' @export -#' @importFrom fansi strip_sgr +#' @importFrom fansi strip_ctl #' @importFrom utf8 utf8_width #' @examples #' get_extent(c("abc", "de")) #' get_extent("\u904b\u6c23") get_extent <- function(x) { force(x) - x <- strip_sgr(x, warn = FALSE) + x <- strip_ctl(x, warn = FALSE) width <- utf8_width(x, encode = FALSE, utf8 = TRUE) if (anyNA(width)) { is_na <- which(is.na(width)) diff --git a/R/ggplot2.R b/R/ggplot2.R index e32401fe4..87efcb11b 100644 --- a/R/ggplot2.R +++ b/R/ggplot2.R @@ -63,7 +63,7 @@ MakeScaleContinuousPositionNum <- function() { }, get_labels = function(self, breaks = self$get_breaks()) { out <- ggplot2::ggproto_parent(ggplot2::ScaleContinuousPosition, self)$get_labels(breaks) - fansi::strip_sgr(out) + fansi::strip_ctl(out) }, make_title = function(self, title) { out <- ggplot2::ggproto_parent(ggplot2::ScaleContinuousPosition, self)$make_title(title) diff --git a/R/utils.R b/R/utils.R index dcc02136b..33c487894 100644 --- a/R/utils.R +++ b/R/utils.R @@ -17,7 +17,7 @@ cat_line <- function(...) { } #' @importFrom utf8 utf8_width -#' @importFrom fansi strip_sgr substr2_ctl +#' @importFrom fansi strip_ctl substr2_ctl str_trunc <- function(x, width, shorten = NULL) { if (all(is.infinite(width))) { return(x) diff --git a/R/vctr.R b/R/vctr.R index 6a11b79b0..0da6b6783 100644 --- a/R/vctr.R +++ b/R/vctr.R @@ -45,7 +45,7 @@ obj_print_data.pillar_vctr <- function(x, ..., .size) { } # FIXME: base::print.default() can't use color, roll own implementation? - out <- stats::setNames(strip_sgr(format(x), warn = FALSE), names(x)) + out <- stats::setNames(strip_ctl(format(x), warn = FALSE), names(x)) print(out, quote = FALSE, max = vec_size(x)) invisible(x) } From b542e4eed21149e4e5066be2522acf642426b051 Mon Sep 17 00:00:00 2001 From: olivroy Date: Fri, 28 Jun 2024 12:16:00 -0400 Subject: [PATCH 2/8] specify argument names in ggplot2 call (name existed before 3.5.0, so no need to depend on ggplot2 3.5.0) (no need to specify `scale_name`? --- R/ggplot2.R | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/R/ggplot2.R b/R/ggplot2.R index 87efcb11b..47cee4dfc 100644 --- a/R/ggplot2.R +++ b/R/ggplot2.R @@ -15,13 +15,13 @@ scale_x_num <- function(..., position = "bottom", guide = ggplot2::waiver(), rescaler = NULL, super = NULL) { stopifnot(is.null(rescaler)) stopifnot(is.null(super)) - stopifnot(is_installed("ggplot2")) + check_installed("ggplot2") ggplot2::continuous_scale( - c( + aesthetics = c( "x", "xmin", "xmax", "xend", "xintercept", "xmin_final", "xmax_final", "xlower", "xmiddle", "xupper" ), - "position_c", identity, + palette = "position_c", ..., guide = guide, position = position, @@ -36,13 +36,13 @@ scale_y_num <- function(..., guide = ggplot2::waiver(), rescaler = NULL, super = NULL) { stopifnot(is.null(rescaler)) stopifnot(is.null(super)) - stopifnot(is_installed("ggplot2")) + check_installed("ggplot2") ggplot2::continuous_scale( - c( + aesthetics = c( "y", "ymin", "ymax", "yend", "yintercept", "ymin_final", "ymax_final", "lower", "middle", "upper" ), - "position_c", identity, + palette = "position_c", ..., guide = guide, rescaler = scales::rescale, From c6bea89b60621cc6166a7a09d6e962858859cba1 Mon Sep 17 00:00:00 2001 From: olivroy Date: Fri, 28 Jun 2024 13:45:58 -0400 Subject: [PATCH 3/8] Small performance improvement --- R/utils.R | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/R/utils.R b/R/utils.R index 33c487894..26c701a02 100644 --- a/R/utils.R +++ b/R/utils.R @@ -34,9 +34,10 @@ str_trunc <- function(x, width, shorten = NULL) { } str_add_ellipsis <- function(x, str_width, width, shorten) { - if (is.null(shorten)) { - shorten <- "back" + if (length(x) == 0L) { + return(x) } + shorten <- shorten %||% "back" switch(shorten, back = { From 5b122fd5a1318237d953640c2f6935cb21059c9e Mon Sep 17 00:00:00 2001 From: olivroy Date: Fri, 28 Jun 2024 13:51:12 -0400 Subject: [PATCH 4/8] Use `cli::ansi_strip()` instead of `fansi::strip_ctl()` --- NAMESPACE | 2 +- R/extent.R | 4 ++-- R/ggplot2.R | 4 ++-- R/pillar.R | 2 +- R/utils.R | 2 +- R/vctr.R | 2 +- 6 files changed, 8 insertions(+), 8 deletions(-) diff --git a/NAMESPACE b/NAMESPACE index 9e63002d1..4fd0aa6d3 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -159,8 +159,8 @@ export(tbl_sum) export(type_sum) import(lifecycle) import(rlang) +importFrom(cli,ansi_strip) importFrom(cli,symbol) -importFrom(fansi,strip_ctl) importFrom(fansi,substr2_ctl) importFrom(glue,as_glue) importFrom(utf8,utf8_width) diff --git a/R/extent.R b/R/extent.R index 9d0a9ea23..b5f3119a3 100644 --- a/R/extent.R +++ b/R/extent.R @@ -5,14 +5,14 @@ #' #' @param x A character vector. #' @export -#' @importFrom fansi strip_ctl +#' @importFrom cli ansi_strip #' @importFrom utf8 utf8_width #' @examples #' get_extent(c("abc", "de")) #' get_extent("\u904b\u6c23") get_extent <- function(x) { force(x) - x <- strip_ctl(x, warn = FALSE) + x <- ansi_strip(x) width <- utf8_width(x, encode = FALSE, utf8 = TRUE) if (anyNA(width)) { is_na <- which(is.na(width)) diff --git a/R/ggplot2.R b/R/ggplot2.R index 47cee4dfc..f7e336c02 100644 --- a/R/ggplot2.R +++ b/R/ggplot2.R @@ -63,7 +63,7 @@ MakeScaleContinuousPositionNum <- function() { }, get_labels = function(self, breaks = self$get_breaks()) { out <- ggplot2::ggproto_parent(ggplot2::ScaleContinuousPosition, self)$get_labels(breaks) - fansi::strip_ctl(out) + ansi_strip(out) }, make_title = function(self, title) { out <- ggplot2::ggproto_parent(ggplot2::ScaleContinuousPosition, self)$make_title(title) @@ -73,7 +73,7 @@ MakeScaleContinuousPositionNum <- function() { if (pillar_attr$notation == "si") { type <- attr(shaft, "type") if (!is.null(type)) { - out <- paste0(out, " ", cli::ansi_strip(type[[1]])) + out <- paste0(out, " ", ansi_strip(type[[1]])) } } else { # paste0() doesn't work here, paste() works like paste0() diff --git a/R/pillar.R b/R/pillar.R index 6ac0f85af..6212099cf 100644 --- a/R/pillar.R +++ b/R/pillar.R @@ -58,7 +58,7 @@ pillar_format_parts <- function(x, width, ...) { } format_abbrev <- function(x, title = NULL, space = " ") { - type_format <- fansi::strip_ctl(format_full_pillar_type(x)) + type_format <- ansi_strip(format_full_pillar_type(x)) if (is.null(title)) { type_format } else { diff --git a/R/utils.R b/R/utils.R index 26c701a02..e6ad1e684 100644 --- a/R/utils.R +++ b/R/utils.R @@ -17,7 +17,7 @@ cat_line <- function(...) { } #' @importFrom utf8 utf8_width -#' @importFrom fansi strip_ctl substr2_ctl +#' @importFrom fansi substr2_ctl str_trunc <- function(x, width, shorten = NULL) { if (all(is.infinite(width))) { return(x) diff --git a/R/vctr.R b/R/vctr.R index 0da6b6783..9f26a86a2 100644 --- a/R/vctr.R +++ b/R/vctr.R @@ -45,7 +45,7 @@ obj_print_data.pillar_vctr <- function(x, ..., .size) { } # FIXME: base::print.default() can't use color, roll own implementation? - out <- stats::setNames(strip_ctl(format(x), warn = FALSE), names(x)) + out <- stats::setNames(ansi_strip(format(x)), names(x)) print(out, quote = FALSE, max = vec_size(x)) invisible(x) } From cf9efc49b9314b926273566a73f465072134c5f7 Mon Sep 17 00:00:00 2001 From: olivroy Date: Fri, 28 Jun 2024 14:01:33 -0400 Subject: [PATCH 5/8] use `cli::ansi_strwrap()` instead of `fansi::strwrap_ctl()` --- NAMESPACE | 1 + R/tbl-format.R | 2 +- R/zzz.R | 2 +- 3 files changed, 3 insertions(+), 2 deletions(-) diff --git a/NAMESPACE b/NAMESPACE index 4fd0aa6d3..7f1b706d6 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -160,6 +160,7 @@ export(type_sum) import(lifecycle) import(rlang) importFrom(cli,ansi_strip) +importFrom(cli,ansi_strwrap) importFrom(cli,symbol) importFrom(fansi,substr2_ctl) importFrom(glue,as_glue) diff --git a/R/tbl-format.R b/R/tbl-format.R index 005636f2c..ef44d3da4 100644 --- a/R/tbl-format.R +++ b/R/tbl-format.R @@ -92,5 +92,5 @@ wrap <- function(..., indent = 0, prefix = "", width) { } strwrap2 <- function(x, width, indent) { - fansi::strwrap_ctl(x, width = max(width, 0), indent = indent, exdent = indent + 2) + cli::ansi_strwrap(x, width = max(width, 0), indent = indent, exdent = indent + 2, simplify = FALSE) } diff --git a/R/zzz.R b/R/zzz.R index 5e739e095..6dc6257c1 100644 --- a/R/zzz.R +++ b/R/zzz.R @@ -33,7 +33,7 @@ #' @importFrom vctrs vec_restore #' @importFrom vctrs vec_size #' @importFrom vctrs vec_slice -#' @importFrom cli symbol +#' @importFrom cli symbol ansi_strwrap NULL # https://github.com/r-lib/pkgdown/issues/1540 From 25fe69430245fa89fdfefed8a2d67a795adbb487 Mon Sep 17 00:00:00 2001 From: olivroy Date: Fri, 28 Jun 2024 14:12:49 -0400 Subject: [PATCH 6/8] Use `cli::ansi_substr()` instead of `fansi::substr2_ctl()` --- DESCRIPTION | 1 - NAMESPACE | 2 +- R/utils.R | 16 +++++++++------- 3 files changed, 10 insertions(+), 9 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index dad21eab5..33f8d0e1e 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -20,7 +20,6 @@ URL: https://pillar.r-lib.org/, https://github.com/r-lib/pillar BugReports: https://github.com/r-lib/pillar/issues Imports: cli (>= 2.3.0), - fansi, glue, lifecycle, rlang (>= 1.0.2), diff --git a/NAMESPACE b/NAMESPACE index 7f1b706d6..85bca39f7 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -161,8 +161,8 @@ import(lifecycle) import(rlang) importFrom(cli,ansi_strip) importFrom(cli,ansi_strwrap) +importFrom(cli,ansi_substr) importFrom(cli,symbol) -importFrom(fansi,substr2_ctl) importFrom(glue,as_glue) importFrom(utf8,utf8_width) importFrom(utils,head) diff --git a/R/utils.R b/R/utils.R index e6ad1e684..766d510e5 100644 --- a/R/utils.R +++ b/R/utils.R @@ -17,7 +17,7 @@ cat_line <- function(...) { } #' @importFrom utf8 utf8_width -#' @importFrom fansi substr2_ctl +#' @importFrom cli ansi_substr str_trunc <- function(x, width, shorten = NULL) { if (all(is.infinite(width))) { return(x) @@ -39,28 +39,29 @@ str_add_ellipsis <- function(x, str_width, width, shorten) { } shorten <- shorten %||% "back" - switch(shorten, + thing <- switch(shorten, back = { - abbr <- substr2_ctl(x, 1, width - 1, type = "width") + abbr <- ansi_substr(x, 1, width - 1) paste0(abbr, get_ellipsis()) }, untick = str_add_ellipsis_untick(x, str_width, width), untick_footnote = str_add_ellipsis_untick(x, str_width, width, footnote = TRUE), front = { - abbr <- substr2_ctl(x, str_width - width + 2, str_width, type = "width") + abbr <- ansi_substr(x, str_width - width + 2, str_width) paste0(get_ellipsis(), abbr) }, mid = { front_width <- ceiling(width / 2) - 1 back_width <- width - front_width - 1 - abbr_front <- substr2_ctl(x, 1, front_width, type = "width") - abbr_back <- substr2_ctl(x, str_width - back_width + 1, str_width, type = "width") + abbr_front <- ansi_substr(x, 1, front_width) + abbr_back <- ansi_substr(x, str_width - back_width + 1, str_width) paste0(abbr_front, get_ellipsis(), abbr_back) }, abbreviate = { abbreviate(x, minlength = width, strict = TRUE) } ) + thing } str_add_ellipsis_untick <- function(x, str_width, width, footnote = FALSE) { @@ -79,7 +80,8 @@ str_add_ellipsis_untick <- function(x, str_width, width, footnote = FALSE) { } # Add ellipsis even if short enough after removal of ticks - abbr <- substr2_ctl(x, 1, width - 1L, type = "width") + # TODO use ansi_strim() + abbr <- ansi_substr(x, 1, width - 1L) abbr <- paste0(abbr, get_ellipsis()) if (footnote) { From 13be514c7d177e5a748f7232de3e1a1846e2891b Mon Sep 17 00:00:00 2001 From: olivroy Date: Fri, 28 Jun 2024 14:25:49 -0400 Subject: [PATCH 7/8] Use `cli::utf8_nchar()` insted of `utf8::utf8_width()` --- NAMESPACE | 2 +- R/extent.R | 4 ++-- R/utils.R | 1 - 3 files changed, 3 insertions(+), 4 deletions(-) diff --git a/NAMESPACE b/NAMESPACE index 85bca39f7..bd642ddc3 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -163,8 +163,8 @@ importFrom(cli,ansi_strip) importFrom(cli,ansi_strwrap) importFrom(cli,ansi_substr) importFrom(cli,symbol) +importFrom(cli,utf8_nchar) importFrom(glue,as_glue) -importFrom(utf8,utf8_width) importFrom(utils,head) importFrom(utils,str) importFrom(vctrs,data_frame) diff --git a/R/extent.R b/R/extent.R index b5f3119a3..dce50c0a4 100644 --- a/R/extent.R +++ b/R/extent.R @@ -6,14 +6,14 @@ #' @param x A character vector. #' @export #' @importFrom cli ansi_strip -#' @importFrom utf8 utf8_width +#' @importFrom cli utf8_nchar #' @examples #' get_extent(c("abc", "de")) #' get_extent("\u904b\u6c23") get_extent <- function(x) { force(x) x <- ansi_strip(x) - width <- utf8_width(x, encode = FALSE, utf8 = TRUE) + width <- cli::utf8_nchar(x, "width") if (anyNA(width)) { is_na <- which(is.na(width)) width[is_na] <- nchar(x[is_na], type = "width") diff --git a/R/utils.R b/R/utils.R index 766d510e5..9618fc8bc 100644 --- a/R/utils.R +++ b/R/utils.R @@ -16,7 +16,6 @@ cat_line <- function(...) { cat(..., "\n", sep = "") } -#' @importFrom utf8 utf8_width #' @importFrom cli ansi_substr str_trunc <- function(x, width, shorten = NULL) { if (all(is.infinite(width))) { From 454ce4baca3cabf7a951cc07b6e81824c0cc7719 Mon Sep 17 00:00:00 2001 From: olivroy <52606734+olivroy@users.noreply.github.com> Date: Fri, 28 Jun 2024 16:45:05 -0400 Subject: [PATCH 8/8] Update R/extent.R MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Kirill Müller --- R/extent.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/extent.R b/R/extent.R index dce50c0a4..fc65cb2cd 100644 --- a/R/extent.R +++ b/R/extent.R @@ -13,7 +13,7 @@ get_extent <- function(x) { force(x) x <- ansi_strip(x) - width <- cli::utf8_nchar(x, "width") + width <- utf8_nchar(x, "width") if (anyNA(width)) { is_na <- which(is.na(width)) width[is_na] <- nchar(x[is_na], type = "width")