diff --git a/DESCRIPTION b/DESCRIPTION index eec0407cf..f8c8d8855 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 471963e14..bd642ddc3 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -159,11 +159,12 @@ export(tbl_sum) export(type_sum) import(lifecycle) import(rlang) +importFrom(cli,ansi_strip) +importFrom(cli,ansi_strwrap) +importFrom(cli,ansi_substr) importFrom(cli,symbol) -importFrom(fansi,strip_sgr) -importFrom(fansi,substr2_ctl) +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 e6956dce2..fc65cb2cd 100644 --- a/R/extent.R +++ b/R/extent.R @@ -5,15 +5,15 @@ #' #' @param x A character vector. #' @export -#' @importFrom fansi strip_sgr -#' @importFrom utf8 utf8_width +#' @importFrom cli ansi_strip +#' @importFrom cli utf8_nchar #' @examples #' get_extent(c("abc", "de")) #' get_extent("\u904b\u6c23") get_extent <- function(x) { force(x) - x <- strip_sgr(x, warn = FALSE) - width <- utf8_width(x, encode = FALSE, utf8 = TRUE) + x <- ansi_strip(x) + width <- 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/ggplot2.R b/R/ggplot2.R index e32401fe4..f7e336c02 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, @@ -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) + 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/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/utils.R b/R/utils.R index dcc02136b..9618fc8bc 100644 --- a/R/utils.R +++ b/R/utils.R @@ -16,8 +16,7 @@ cat_line <- function(...) { cat(..., "\n", sep = "") } -#' @importFrom utf8 utf8_width -#' @importFrom fansi strip_sgr substr2_ctl +#' @importFrom cli ansi_substr str_trunc <- function(x, width, shorten = NULL) { if (all(is.infinite(width))) { return(x) @@ -34,32 +33,34 @@ 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, + 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) { @@ -78,7 +79,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) { diff --git a/R/vctr.R b/R/vctr.R index 6a11b79b0..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_sgr(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) } 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