From 85cc08b727ed0e7f2414d52cf86267525181f5bb Mon Sep 17 00:00:00 2001 From: Scott Chamberlain Date: Sat, 30 Nov 2024 07:31:52 -0800 Subject: [PATCH] change bubble and tail: make tail separately; have tail be relative to the x-y position of the top most non emtpy string of the animal --- NAMESPACE | 2 ++ R/bubble.R | 44 ++++++++++++++++++++--------- R/say.R | 6 ++-- man/bubble.Rd | 6 +--- man/{dot-tail.Rd => bubble_tail.Rd} | 31 ++++++++++++-------- 5 files changed, 58 insertions(+), 31 deletions(-) rename man/{dot-tail.Rd => bubble_tail.Rd} (56%) diff --git a/NAMESPACE b/NAMESPACE index 25ce6f1..97cd4f0 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -2,6 +2,8 @@ export(animals) export(bubble) +export(bubble_tail) +export(bubble_tail2) export(endless_horse) export(say) importFrom(rlang,abort) diff --git a/R/bubble.R b/R/bubble.R index 512f9e7..7daf9ff 100644 --- a/R/bubble.R +++ b/R/bubble.R @@ -3,9 +3,6 @@ #' @export #' @param x (character) a character vector #' @param width (integer/numeric) width of each line. default: 60 -#' @param thought_sym (character) scalar character to use for the -#' speech bubble tail (see ). -#' default: "o" #' @return character vector of length greater than the input `x` #' @note modified from #' @examplesIf rlang::is_installed("fortunes") @@ -18,7 +15,7 @@ #' text_style(bubble(quote)) #' #' cat(bubble(paste(quote, collapse = " ")), sep = "\n") -bubble <- function(x, width = 60, thought_sym = "o") { +bubble <- function(x, width = 60) { empty_to_avoid_rlang_header <- "" x <- strwrap(x, width = width) n <- max(nchar(x)) @@ -32,22 +29,43 @@ bubble <- function(x, width = 60, thought_sym = "o") { quote[i] <- paste0("|", string_pad(x[i], n + 4, "left"), "|") } } - thought <- .tail(n, thought_sym) - c(empty_to_avoid_rlang_header, top, quote, bottom, thought) + c(empty_to_avoid_rlang_header, top, quote, bottom) } -#' Make the tail part of a thought bubble -#' @keywords internal +#' Make the tail part of a thought bubble +#' +#' @export +#' @param animal (character) a string +#' @param thought_sym (character) scalar character to use for the +#' speech bubble tail (see ). +#' default: "o" #' @param max_char_length (numeric) length of the maximum line. this is used #' to determine how much whitespace padding to add to the left of #' `thought_sym` #' @inheritParams bubble #' @examplesIf interactive() -#' .tail(59) -#' cat(.tail(59), sep = "\n") -#' cat(.tail(11), sep = "\n") -#' cat(.tail(11, "%"), sep = "\n") -.tail <- function(max_char_length, thought_sym = "o") { +#' bubble_tail(animals[["chicken"]]) +#' cat(bubble_tail(animals[["chicken"]]), sep = "\n") +#' cat(bubble_tail(animals[["chicken"]]), sep = "\n") +#' cat(bubble_tail(animals[["chicken"]], "%"), sep = "\n") +#' +#' bubble_tail2(59) +#' cat(bubble_tail2(59), sep = "\n") +#' cat(bubble_tail2(11), sep = "\n") +#' cat(bubble_tail2(11, "%"), sep = "\n") +bubble_tail <- function(animal, thought_sym = "o") { + animal_split <- strsplit(animal, "\n")[[1]] + animal_split <- animal_split[nchar(animal_split) > 0] + n_first_spaces <- length(gregexpr("\\s", animal_split[1])[[1]]) + c( + string_pad(thought_sym, n_first_spaces - 2, "left"), + string_pad(thought_sym, (n_first_spaces - 2) + 2, "left") + ) +} + +#' @export +#' @rdname bubble_tail +bubble_tail2 <- function(max_char_length, thought_sym = "o") { c( string_pad(thought_sym, floor((max_char_length + 4) / 3), "left"), string_pad(thought_sym, floor((max_char_length + 4) / 3) + 2, "left") diff --git a/R/say.R b/R/say.R index 0a45862..598bb91 100644 --- a/R/say.R +++ b/R/say.R @@ -196,10 +196,12 @@ say <- function( abort("sorry, fillerama API is gone, sorry :(") } - what_bubbled <- bubble(x = what, width = width, thought_sym = thought_sym) + what_bubbled <- bubble(x = what, width = width) what_styled <- color_text(what_bubbled, what_color) + what_tail <- bubble_tail(who, thought_sym = thought_sym) + tail_styled <- color_text(what_tail, what_color) who_styled <- color_text(who, by_color) - what_who <- paste(c(what_styled, who_styled), collapse = "\n") + what_who <- paste(c(what_styled, tail_styled, who_styled), collapse = "\n") if (type == "warning") { if (nchar(what_who) < 100) { diff --git a/man/bubble.Rd b/man/bubble.Rd index 075c842..ca0c8be 100644 --- a/man/bubble.Rd +++ b/man/bubble.Rd @@ -4,16 +4,12 @@ \alias{bubble} \title{Thought/speech bubble/balloon} \usage{ -bubble(x, width = 60, thought_sym = "o") +bubble(x, width = 60) } \arguments{ \item{x}{(character) a character vector} \item{width}{(integer/numeric) width of each line. default: 60} - -\item{thought_sym}{(character) scalar character to use for the -speech bubble tail (see \url{https://en.wikipedia.org/wiki/Speech_balloon}). -default: "o"} } \value{ character vector of length greater than the input \code{x} diff --git a/man/dot-tail.Rd b/man/bubble_tail.Rd similarity index 56% rename from man/dot-tail.Rd rename to man/bubble_tail.Rd index 1198c55..3595ce7 100644 --- a/man/dot-tail.Rd +++ b/man/bubble_tail.Rd @@ -1,29 +1,38 @@ % Generated by roxygen2: do not edit by hand % Please edit documentation in R/bubble.R -\name{.tail} -\alias{.tail} +\name{bubble_tail} +\alias{bubble_tail} +\alias{bubble_tail2} \title{Make the tail part of a thought bubble} \usage{ -.tail(max_char_length, thought_sym = "o") +bubble_tail(animal, thought_sym = "o") + +bubble_tail2(max_char_length, thought_sym = "o") } \arguments{ -\item{max_char_length}{(numeric) length of the maximum line. this is used -to determine how much whitespace padding to add to the left of -\code{thought_sym}} +\item{animal}{(character) a string} \item{thought_sym}{(character) scalar character to use for the speech bubble tail (see \url{https://en.wikipedia.org/wiki/Speech_balloon}). default: "o"} + +\item{max_char_length}{(numeric) length of the maximum line. this is used +to determine how much whitespace padding to add to the left of +\code{thought_sym}} } \description{ Make the tail part of a thought bubble } \examples{ \dontshow{if (interactive()) (if (getRversion() >= "3.4") withAutoprint else force)(\{ # examplesIf} -.tail(59) -cat(.tail(59), sep = "\n") -cat(.tail(11), sep = "\n") -cat(.tail(11, "\%"), sep = "\n") +bubble_tail(animals[["chicken"]]) +cat(bubble_tail(animals[["chicken"]]), sep = "\n") +cat(bubble_tail(animals[["chicken"]]), sep = "\n") +cat(bubble_tail(animals[["chicken"]], "\%"), sep = "\n") + +bubble_tail2(59) +cat(bubble_tail2(59), sep = "\n") +cat(bubble_tail2(11), sep = "\n") +cat(bubble_tail2(11, "\%"), sep = "\n") \dontshow{\}) # examplesIf} } -\keyword{internal}