From 45e312375a597e8cfc7cfa380275f365a525ada2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kirill=20M=C3=BCller?= Date: Fri, 22 Apr 2022 16:55:06 +0200 Subject: [PATCH 1/3] Extract str_add_ellipsis_untick() --- R/tick.R | 24 ++---------------------- R/utils.R | 23 +++++++++++++++++++++++ 2 files changed, 25 insertions(+), 22 deletions(-) diff --git a/R/tick.R b/R/tick.R index 4533f082e..15e2d8f8f 100644 --- a/R/tick.R +++ b/R/tick.R @@ -1,25 +1,5 @@ -format_title <- function(x, width) { - out <- align(str_trunc(x, width)) - - # HACK: Abbreviating text inbetween ticks - ticked <- grepl("^`", x) - if (!any(ticked)) { - return(out) - } - - ticked[which(get_extent(x[ticked]) <= width)] <- FALSE - if (!any(ticked)) { - return(out) - } - - x_ticked <- x[ticked] - rx <- "^`(.*)(`[^`]*)$" - match <- gsub(rx, "\\1", x[ticked]) - rest <- gsub(rx, "\\2", x[ticked]) - - short <- str_trunc(match, width + get_extent(match) - get_extent(x_ticked)) - out[ticked] <- align(paste0("`", short, rest)) - out +format_title <- function(x, width, footnote = FALSE) { + align(str_trunc(x, width, if (footnote) "untick_footnote" else "untick")) } tick_names_if_needed <- function(x) { diff --git a/R/utils.R b/R/utils.R index ad368bce6..b6135c624 100644 --- a/R/utils.R +++ b/R/utils.R @@ -43,6 +43,8 @@ str_add_ellipsis <- function(x, str_width, width, shorten) { abbr <- substr2_ctl(x, 1, width - 1, type = "width") 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") paste0(get_ellipsis(), abbr) @@ -60,6 +62,27 @@ str_add_ellipsis <- function(x, str_width, width, shorten) { ) } +str_add_ellipsis_untick <- function(x, str_width, width, footnote = FALSE) { + if (footnote) { + width <- width - 1L + } + + rx <- "^(.*[^`])(`?)$" + match <- gsub(rx, "\\1", x) + rest <- gsub(rx, "\\2", x) + + short <- substr2_ctl(match, 1, width - 1L + get_extent(match) - str_width, type = "width") + abbr <- paste0(short, get_ellipsis(), rest) + + if (footnote) { + # Placeholder, regular title can't end with this string, + # we can use this to detect a footnote + abbr <- paste0(abbr, "\u02df") + } + + abbr +} + paste_with_space_if_needed <- function(x, y) { if (y == "") { x From 903dd00b3c64594a756ebe926dd93d06bda5cb96 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kirill=20M=C3=BCller?= Date: Fri, 22 Apr 2022 19:01:07 +0200 Subject: [PATCH 2/3] Remove unused --- R/ctl_pillar_component.R | 1 - 1 file changed, 1 deletion(-) diff --git a/R/ctl_pillar_component.R b/R/ctl_pillar_component.R index e7280ea84..60e43e342 100644 --- a/R/ctl_pillar_component.R +++ b/R/ctl_pillar_component.R @@ -72,7 +72,6 @@ pillar_format_parts_2 <- function(x, width, is_focus = FALSE) { formatted <- map(x, function(.x) format(.x[[1L]], width = min(width, get_width(.x)))) # FIXME: Support missing type component - flat_focus_pos <- integer() if (is_focus) { type_idx <- which(names(x) == "type") if (length(type_idx) > 0) { From a5c807dadd5dd4d2846c6c64161cbe38eccadc20 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kirill=20M=C3=BCller?= Date: Fri, 22 Apr 2022 19:03:32 +0200 Subject: [PATCH 3/3] Add is_focus argument to format.pillar_type() --- R/ctl_pillar_component.R | 14 +++++--------- R/type.R | 8 ++++++-- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/R/ctl_pillar_component.R b/R/ctl_pillar_component.R index 60e43e342..a4cdd834d 100644 --- a/R/ctl_pillar_component.R +++ b/R/ctl_pillar_component.R @@ -69,15 +69,11 @@ pillar_get_min_width <- function(x) { } pillar_format_parts_2 <- function(x, width, is_focus = FALSE) { - formatted <- map(x, function(.x) format(.x[[1L]], width = min(width, get_width(.x)))) - - # FIXME: Support missing type component - if (is_focus) { - type_idx <- which(names(x) == "type") - if (length(type_idx) > 0) { - formatted[[type_idx]] <- crayon_underline(formatted[[type_idx]]) - } - } + formatted <- map(x, function(.x) format( + .x[[1L]], + width = min(width, get_width(.x)), + is_focus = is_focus + )) align <- attr(formatted[["data"]], "align", exact = TRUE) %||% "left" diff --git a/R/type.R b/R/type.R index 5f8fb681e..86556888e 100644 --- a/R/type.R +++ b/R/type.R @@ -44,8 +44,12 @@ get_pillar_type <- function(x) { } #' @export -format.pillar_type <- function(x, width = NULL, ...) { - format_type_sum(x[[1]], width) +format.pillar_type <- function(x, width = NULL, ..., is_focus = FALSE) { + out <- format_type_sum(x[[1]], width) + if (is_focus) { + out <- crayon_underline(out) + } + out } format_full_pillar_type <- function(x) {