Skip to content

Commit

Permalink
styling
Browse files Browse the repository at this point in the history
  • Loading branch information
sckott committed Nov 2, 2024
1 parent ba7ad54 commit 2f648f2
Show file tree
Hide file tree
Showing 9 changed files with 89 additions and 73 deletions.
6 changes: 3 additions & 3 deletions R/bubble.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#' Speech bubble
#'
#'
#' @export
#' @param x (character) a character vector
#' @param width (integer/numeric) width of each line. default: 60
Expand All @@ -12,11 +12,11 @@
#' library(fortunes)
#' quote <- as.character(fortune())
#' bubble(x = quote)
#'
#'
#' text_color <- sample(grDevices::colors(), 1)
#' text_style <- crayon::make_style(text_color)
#' text_style(bubble(quote))
#'
#'
#' cat(bubble(paste(quote, collapse = " ")), sep = "\n")
bubble <- function(x, width = 60, thought_sym = "o") {
empty_to_avoid_rlang_header <- ""
Expand Down
2 changes: 1 addition & 1 deletion R/constants.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ fillerama_what <- c(
"loremipsum"
)

catfact_api <- 'https://catfact.ninja/fact'
catfact_api <- "https://catfact.ninja/fact"
51 changes: 28 additions & 23 deletions R/endless_horse.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#' Endless horse
#'
#'
#' Each time you press enter, the horse keeps going...and going...
#'
#'
#' @export
#' @param what (character) What do you want to say? See details.
#' @param endless (logical) Should horse be enless, you better say yes.
Expand All @@ -10,12 +10,12 @@
#' geometrically after the first iteration in order to keep the horse
#' on screen for a while, but it **will** keep going forever. Or
#' until you hit escape/Ctrl-C depending on your platform).
#' @param what_color (character or crayon function) A
#' @param what_color (character or crayon function) A
#' \href{https://github.com/r-lib/crayon#256-colors}{\code{crayon}}-suported
#' text color or
#' \href{https://github.com/r-lib/crayon#styles}{\code{crayon style function}}
#' to color `what`. You might try `colors()` or `?rgb` for ideas.
#' @param horse_color (character or crayon function) A
#' @param horse_color (character or crayon function) A
#' \href{https://github.com/r-lib/crayon#256-colors}{\code{crayon}}-suported
#' text color or
#' \href{https://github.com/r-lib/crayon#styles}{\code{crayon style function}}
Expand All @@ -24,50 +24,55 @@
#' endless_horse()
#' endless_horse(endless = FALSE)
#' }
endless_horse <- function(what="Hello world!", endless = TRUE, wait = 0.5,
endless_horse <- function(what = "Hello world!", endless = TRUE, wait = 0.5,
what_color = NULL, horse_color = NULL) {

if (!is_null(what_color) & !(inherits(what_color, c("crayon", "character")))) {
stop("what_color must be of class character or crayon",
call. = FALSE)
call. = FALSE
)
}

if (!is_null(horse_color) & !(inherits(horse_color, c("crayon", "character")))) {
stop("by_color must be of class character or crayon",
call. = FALSE)
call. = FALSE
)
}

if (!is_null(what_color) & is_character(what_color)) {
what_color <- crayon::make_style(what_color)
} else if (!is_null(what_color) & is.function(what_color)) {
what_color <- what_color
} else {
what_color <- function(x) x
}

if (!is_null(horse_color) & is_character(horse_color)) {
horse_color <- crayon::make_style(horse_color)
} else if (!is_null(horse_color) & is.function(horse_color)) {
horse_color <- horse_color
} else {
horse_color <- function(x) x
}


horse <- get_who("endlesshorse", NULL)
what_pos_start <-
regexpr('%s', horse)[1] - 1

what_pos_start <-
regexpr("%s", horse)[1] - 1

what_pos_end <- what_pos_start + 3

horse <- paste0(horse_color(substr(horse, 1, what_pos_start)),
what_color(what),
horse_color(substr(horse, what_pos_end, nchar(horse))))


horse <- paste0(
horse_color(substr(horse, 1, what_pos_start)),
what_color(what),
horse_color(substr(horse, what_pos_end, nchar(horse)))
)

message(horse)
while (endless) {
tryCatch(interrupt=function(e) {endless <<- FALSE}, {
tryCatch(interrupt = function(e) {
endless <<- FALSE
}, {
Sys.sleep(wait)
message(horse_color(" | | | | || |"))
wait <- wait * 1.1
Expand Down
2 changes: 1 addition & 1 deletion R/init.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
.onAttach <- function(libname, pkgname) {
options(error = rlang::entrace)
options(error = rlang::entrace)
}
8 changes: 4 additions & 4 deletions R/say.r
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,10 @@
#'
#' # Clippy
#' say(fortune = 59, by = "clippy")
say <- function(what = "Hello world!", by = "cow", type = NULL,
what_color = NULL, by_color = NULL, length = 18, fortune = NULL,
thought_sym = "o", width = 60, ...) {

say <- function(
what = "Hello world!", by = "cow", type = NULL,
what_color = NULL, by_color = NULL, length = 18, fortune = NULL,
thought_sym = "o", width = 60, ...) {
stopifnot("what must be length 1" = has_length(what, 1))

if (
Expand Down
6 changes: 4 additions & 2 deletions R/zzz.R
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,10 @@ cowsay_insert_rainbow <- function(clr) {
return(clr)
} else if (any(clr == "rainbow")) {
rb_idx <- which(clr == "rainbow")
clr[rb_idx] <- list(c("red", "orange", "yellow", "green",
"blue", "purple"))
clr[rb_idx] <- list(c(
"red", "orange", "yellow", "green",
"blue", "purple"
))
clr <- unlist(clr)
}
return(clr)
Expand Down
74 changes: 41 additions & 33 deletions README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ sort(names(animals))
### Say Something

```{r}
say('time')
say("time")
```

```{r}
Expand All @@ -144,8 +144,9 @@ library(crayon)


```{r eval=FALSE}
say("boo!", "cow",
what_color = "cyan", by_color = "saddlebrown")
say("boo!", "cow",
what_color = "cyan", by_color = "saddlebrown"
)
```


Expand All @@ -155,8 +156,9 @@ say("boo!", "cow",


```{r eval=FALSE}
say("I love hooo you are!", "cow",
what_color = rgb(0, 1, 1), by_color = "#FF0000")
say("I love hooo you are!", "cow",
what_color = rgb(0, 1, 1), by_color = "#FF0000"
)
```


Expand All @@ -169,9 +171,11 @@ String styles together [crayon-style](https://github.com/r-lib/crayon#styles):


```{r eval=FALSE}
say(what = "rms", by = "rms",
what_color = yellow$bgMagenta$bold,
by_color = cyan$italic)
say(
what = "rms", by = "rms",
what_color = yellow$bgMagenta$bold,
by_color = cyan$italic
)
```


Expand All @@ -184,10 +188,12 @@ This doesn't preclude you from adding extra crayon colors to your `what` string


```{r eval=FALSE}
say(what = paste0("hello ", crayon::yellow("there "), crayon::underline("world")),
by = "cow",
what_color = bgBlue$cyan$italic,
by_color = "thistle") # Don't ask me why "thistle" is pink/purple
say(
what = paste0("hello ", crayon::yellow("there "), crayon::underline("world")),
by = "cow",
what_color = bgBlue$cyan$italic,
by_color = "thistle"
) # Don't ask me why "thistle" is pink/purple
```


Expand All @@ -200,10 +206,12 @@ say(what = paste0("hello ", crayon::yellow("there "), crayon::underline("world")
```{r eval=FALSE}
say(what = "I'm a rare Irish buffalo",
by = "buffalo",
what_color = c("salmon2", "darkcyan", "salmon2", "darkcyan"),
by_color = c("green", "white", "orange"))
say(
what = "I'm a rare Irish buffalo",
by = "buffalo",
what_color = c("salmon2", "darkcyan", "salmon2", "darkcyan"),
by_color = c("green", "white", "orange")
)
```
Expand All @@ -220,12 +228,12 @@ say("hell no!")


```{r}
say("hell no!", type="warning")
say("hell no!", type = "warning")
```


```{r}
say("hell no!", type="string")
say("hell no!", type = "string")
```


Expand Down Expand Up @@ -264,67 +272,67 @@ say("it's caturday", "longcat")
### Grumpy cat

```{r}
say('NO!', by='grumpycat')
say("NO!", by = "grumpycat")
```

```{r}
say('WOKE UP TODAY, IT WAS TERRIBLE', by='grumpycat')
say("WOKE UP TODAY, IT WAS TERRIBLE", by = "grumpycat")
```

```{r}
say('I HAD FUN ONCE, IT WAS AWFUL', by='grumpycat')
say("I HAD FUN ONCE, IT WAS AWFUL", by = "grumpycat")
```

### Bunny Holding a sign

```{r}
say(by='signbunny')
say(by = "signbunny")
```

### Fish

```{r}
say(by='fish')
say(by = "fish")
```

### R fortunes

```{r}
say('fortune','cat')
say("fortune", "cat")
```

You can also pick a particular fortune by number or regex search - if the `fortune` parameter is not `NULL` you don't have pass anything to the `what` parameter (the 1st parameter)

```{r}
say(fortune=100)
say(fortune = 100)
```

```{r}
say(fortune='whatever')
say(fortune = "whatever")
```

### Trilobite

```{r}
say("Hi there :)", by='trilobite')
say("Hi there :)", by = "trilobite")
```

### Shark

```{r}
say('Q: What do you call a solitary shark\nA: A lone shark', by='shark')
say("Q: What do you call a solitary shark\nA: A lone shark", by = "shark")
```

### Buffalo

```{r}
say('Q: What do you call a single buffalo?\nA: A buffalonely', by='buffalo')
say("Q: What do you call a single buffalo?\nA: A buffalonely", by = "buffalo")
```

### Clippy

```{r}
say(fortune=59, by="clippy")
say(fortune = 59, by = "clippy")
```

### Yoda
Expand All @@ -336,7 +344,7 @@ say("fortune", by = "yoda")
### Bats!

```{r}
say("hi, i'm a bat", by="bat")
say("hi, i'm a bat", by = "bat")
```

See also `bat2`
Expand Down Expand Up @@ -406,15 +414,15 @@ endless_horse()

```{r}
library("magrittr")
"I HAD FUN ONCE, IT WAS AWFUL" %>% say('grumpycat')
"I HAD FUN ONCE, IT WAS AWFUL" %>% say("grumpycat")
```

### I just want the animals!

Okay, hold your endless horses. Just use the exported vector `animals`, and you can select the animal you want by name.

```{r}
animals['clippy']
animals["clippy"]
```

## Meta
Expand Down
9 changes: 5 additions & 4 deletions tests/testthat/test-say.R
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@ test_that("say works with colors", {
)

expect_equal(
say(what = "rms", by = "rms",
what_color = yellow$bgMagenta$bold,
by_color = cyan$italic),
say(
what = "rms", by = "rms",
what_color = yellow$bgMagenta$bold,
by_color = cyan$italic
),
say(what = "rms", by = "rms", type = "print")
)

Expand Down Expand Up @@ -57,4 +59,3 @@ test_that("say fails with certain characters on windows", {
skip_on_os(c("mac", "linux", "solaris"))
expect_error(say("Hi", by = "longcat"), "If you're on Windows, you can't use")
})

Loading

0 comments on commit 2f648f2

Please sign in to comment.