Skip to content

Commit

Permalink
#86 remove multicolor for now
Browse files Browse the repository at this point in the history
  • Loading branch information
sckott committed Nov 2, 2023
1 parent d6984a5 commit 2a47912
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 124 deletions.
1 change: 0 additions & 1 deletion DESCRIPTION
Expand Up @@ -39,7 +39,6 @@ Suggests:
curl,
jsonlite,
knitr,
multicolor,
rmarkdown,
testthat
RoxygenNote: 7.2.3
20 changes: 3 additions & 17 deletions R/say.r
Expand Up @@ -77,21 +77,6 @@
#' say()
#' say("what")
#' say('time')
#'
#' if (requireNamespace("multicolor", quietly=TRUE) &&
#' requireNamespace("jsonlite", quietly=TRUE)) {
#' say("meow", "cat", what_color = "blue")
#' say('time', "poop", by_color = "cyan", what_color = "pink")
#'
#' library(jsonlite)
#' say("hello world",
#' by = "hypnotoad",
#' what_color = c("royalblue1", "tomato2"),
#' by_color = c("rainbow", "rainbow"))
#' say("whats up",
#' what_color = crayon::cyan$bgMagenta,
#' by_color = c("salmon1", "springgreen"))
#' }
#'
#' say("who you callin chicken", "chicken")
#' say("ain't that some shit", "poop")
Expand Down Expand Up @@ -219,8 +204,9 @@ say <- function(what="Hello world!", by="cat",
c <- crayon::make_style(c)
out <- c(txt)
} else if (length(c) >= 1) {
out <- multicolor::multi_color(txt, c,
type = "string")
stop("this functionality requires multicolor, which is temporarily not on CRAN")
# out <- multicolor::multi_color(txt, c,
# type = "string")
}
}
return(out)
Expand Down
18 changes: 16 additions & 2 deletions R/utils.R
Expand Up @@ -6,10 +6,11 @@ check_color <- function(clr) {

if (all(inherits(clr, "character"))) {
if (length(clr) > 1 || clr == "rainbow") {
check4pkg("multicolor")
# check4pkg("multicolor")

# Change "rainbow" into roygbiv
clr <- multicolor::insert_rainbow(clr)
# clr <- multicolor::insert_rainbow(clr)
clr <- cowsay_insert_rainbow(clr)

if (!all(is.character(clr))) {
stop("If color arguments have > length 1, all colors must be of class character",
Expand All @@ -19,3 +20,16 @@ check_color <- function(clr) {
}
return(clr)
}

cowsay_insert_rainbow <- function(clr) {
if (inherits(clr, "crayon")) {
return(clr)
}
else if (any(clr == "rainbow")) {
rb_idx <- which(clr == "rainbow")
clr[rb_idx] <- list(c("red", "orange", "yellow", "green",
"blue", "purple"))
clr <- unlist(clr)
}
return(clr)
}
15 changes: 0 additions & 15 deletions man/say.Rd

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

49 changes: 26 additions & 23 deletions tests/testthat/test-say.R
Expand Up @@ -12,7 +12,7 @@ test_that("say types works as expected", {
})

test_that("say works with multicolor", {
skip_if_not_installed("multicolor")
# skip_if_not_installed("multicolor")

expect_equal(
length(suppressMessages(say("foo", by_color = "cyan"))) + 1,
Expand All @@ -26,30 +26,33 @@ test_that("say works with multicolor", {
say(what = "rms", by = "rms", type = "print")
)

expect_equal(
say(what = "I'm a rare Irish buffalo",
by = "buffalo",
what_color = "pink",
by_color = c("green", "white", "orange")),
say(what = "I'm a rare Irish buffalo",
by = "buffalo",
type = "print")
)
# TODO: undo comments when multicolor comes back
# expect_equal(
# say(what = "I'm a rare Irish buffalo",
# by = "buffalo",
# what_color = "pink",
# by_color = c("green", "white", "orange")),
# say(what = "I'm a rare Irish buffalo",
# by = "buffalo",
# type = "print")
# )

expect_equal(
say("I'm not dying, you're dying", "yoda",
what_color = "green",
by_color = colors()),
say("I'm not dying, you're dying", "yoda",
type = "print")
)
# TODO: undo comments when multicolor comes back
# expect_equal(
# say("I'm not dying, you're dying", "yoda",
# what_color = "green",
# by_color = colors()),
# say("I'm not dying, you're dying", "yoda",
# type = "print")
# )

expect_equal(
say("asdfghjkl;'", "chicken",
what_color = blue,
by_color = c("rainbow", colors()[sample(100, 1)], "rainbow")),
say("asdfghjkl;'", "chicken", type = "print")
)
# TODO: undo comments when multicolor comes back
# expect_equal(
# say("asdfghjkl;'", "chicken",
# what_color = blue,
# by_color = c("rainbow", colors()[sample(100, 1)], "rainbow")),
# say("asdfghjkl;'", "chicken", type = "print")
# )

skip_if(!crayon::has_color(), message = "Shouldn't fail if colors can't be applied.")
expect_error(
Expand Down
67 changes: 1 addition & 66 deletions vignettes/cowsay.Rmd
Expand Up @@ -14,7 +14,7 @@ vignette: >
---

```{r echo=FALSE}
has_multicolor <- requireNamespace("multicolor", quietly = TRUE)
# has_multicolor <- requireNamespace("multicolor", quietly = TRUE)
knitr::opts_chunk$set(
comment = "#>",
collapse = TRUE
Expand Down Expand Up @@ -104,68 +104,3 @@ String
say("hello world", by = "cow", type = "string")
```


## Add color

```{r eval = has_multicolor}
library(jsonlite)
library(multicolor)
```


We rely on the [`crayon`](https://github.com/r-lib/crayon) package for color and the [`multicolor`](https://github.com/aedobbyn/multicolor) package for multiple colors. The arguments you supply to `what_color` and `by_color` can be strings -- either the color name or a hex value -- or a [function of class `crayon`](https://github.com/r-lib/crayon#styles).


```{r eval = has_multicolor}
say(what = "fortune",
by = "rabbit",
what_color = "#FF4500",
by_color = "red")
```


`colors()` are all supported as are any `rgb` values that evaluate to a string. Supplying multiple colors for `what_color` or `by_color` is allowed, as long as they're in a character vector. "rainbow" is also allowed.

```{r eval = has_multicolor}
# make a vector of animals safe to use on windows in case vignette built on windows
not_on_windows <- c('shortcat','longcat','fish','signbunny','stretchycat',
'anxiouscat','longtailcat','grumpycat','mushroom')
names_safe <- names(animals)[!names(animals) %in% not_on_windows]
```

```{r eval = has_multicolor}
say(what = "fortune",
by = sample(names_safe, 1),
what_color = rgb(.1, .2, .3),
by_color = sample(colors(), 5),
type = "message")
```


```{r eval = has_multicolor}
say(what = "fortune",
by = sample(names_safe, 1),
what_color = rgb(.1, .2, .3),
by_color = sample(colors(), 5),
type = "message")
```

`"rainbow"` is the same as `c("red", "orange", "yellow", "green", "blue", "purple")`. Saves you a bit of typing.

```{r eval = has_multicolor}
say(what = "foobar",
by = "shark",
what_color = "rainbow",
by_color = c("rainbow", "rainbow", "rainbow"))
```

The main advantage of using `crayon` functions instead of color strings is the ability to combine styles together.

```{r eval = has_multicolor}
library(crayon)
say(what = "fortune",
by = "egret",
what_color = bgBlue$white$italic,
by_color = bold$green)
```

0 comments on commit 2a47912

Please sign in to comment.