Skip to content

Commit

Permalink
change bubble and tail: make tail separately; have tail be relative t…
Browse files Browse the repository at this point in the history
…o the x-y position of the top most non emtpy string of the animal
  • Loading branch information
sckott committed Nov 30, 2024
1 parent 656e3e4 commit 85cc08b
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 31 deletions.
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

export(animals)
export(bubble)
export(bubble_tail)
export(bubble_tail2)
export(endless_horse)
export(say)
importFrom(rlang,abort)
Expand Down
44 changes: 31 additions & 13 deletions R/bubble.R
Original file line number Diff line number Diff line change
Expand Up @@ -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 <https://en.wikipedia.org/wiki/Speech_balloon>).
#' default: "o"
#' @return character vector of length greater than the input `x`
#' @note modified from <https://github.com/schochastics/startifyR>
#' @examplesIf rlang::is_installed("fortunes")
Expand All @@ -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))
Expand All @@ -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 <https://en.wikipedia.org/wiki/Speech_balloon>).
#' 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")
Expand Down
6 changes: 4 additions & 2 deletions R/say.R
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
6 changes: 1 addition & 5 deletions man/bubble.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 20 additions & 11 deletions man/dot-tail.Rd → man/bubble_tail.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 85cc08b

Please sign in to comment.