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

chore: drop scale_name ggplot2 arg and drop fansi dep #663

Merged
merged 9 commits into from
Jul 1, 2024
1 change: 0 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
7 changes: 4 additions & 3 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 4 additions & 4 deletions R/extent.R
Original file line number Diff line number Diff line change
Expand Up @@ -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 <- cli::utf8_nchar(x, "width")
olivroy marked this conversation as resolved.
Show resolved Hide resolved
if (anyNA(width)) {
is_na <- which(is.na(width))
width[is_na] <- nchar(x[is_na], type = "width")
Expand Down
16 changes: 8 additions & 8 deletions R/ggplot2.R
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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)
Expand All @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion R/pillar.R
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion R/tbl-format.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
22 changes: 12 additions & 10 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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) {
Expand All @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion R/vctr.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion R/zzz.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down