|
4 | 4 | #' |
5 | 5 | #' @export |
6 | 6 | #' @param what (character) What do you want to say? See details. |
7 | | -#' @param endless (logical) Should horse be enless, you better say yes. |
| 7 | +#' @param endless (logical) Should horse be endless, you better say yes. |
8 | 8 | #' Default: `TRUE` |
9 | 9 | #' @param wait How long to wait between leg segments (time grows |
10 | 10 | #' geometrically after the first iteration in order to keep the horse |
|
20 | 20 | #' text color or |
21 | 21 | #' \href{https://github.com/r-lib/crayon#styles}{\code{crayon style function}} |
22 | 22 | #' to color your steed. |
23 | | -#' @examples \dontrun{ |
24 | | -#' endless_horse() |
| 23 | +#' @examples |
25 | 24 | #' endless_horse(endless = FALSE) |
26 | | -#' } |
27 | | -endless_horse <- function(what = "Hello world!", endless = TRUE, wait = 0.5, |
28 | | - what_color = NULL, horse_color = NULL) { |
29 | | - if (!is_null(what_color) & !(inherits(what_color, c("crayon", "character")))) { |
30 | | - abort("what_color must be of class character or crayon") |
31 | | - } |
32 | | - |
33 | | - if (!is_null(horse_color) & !(inherits(horse_color, c("crayon", "character")))) { |
34 | | - abort("by_color must be of class character or crayon") |
35 | | - } |
36 | | - |
37 | | - if (!is_null(what_color) & is_character(what_color)) { |
38 | | - what_color <- crayon::make_style(what_color) |
39 | | - } else if (!is_null(what_color) & is.function(what_color)) { |
40 | | - what_color <- what_color |
41 | | - } else { |
42 | | - what_color <- function(x) x |
43 | | - } |
44 | | - |
45 | | - if (!is_null(horse_color) & is_character(horse_color)) { |
46 | | - horse_color <- crayon::make_style(horse_color) |
47 | | - } else if (!is_null(horse_color) & is.function(horse_color)) { |
48 | | - horse_color <- horse_color |
| 25 | +#' @examplesIf interactive() |
| 26 | +#' endless_horse() |
| 27 | +#' endless_horse(what_color = crayon::bgBlue) |
| 28 | +endless_horse <- function( |
| 29 | + what = "Hello world!", |
| 30 | + endless = TRUE, |
| 31 | + wait = 0.5, |
| 32 | + what_color = NULL, |
| 33 | + horse_color = NULL |
| 34 | +) { |
| 35 | + if ( |
| 36 | + crayon::has_color() == FALSE && |
| 37 | + (!is_null(what_color) || !is_null(horse_color)) |
| 38 | + ) { |
| 39 | + inform(c( |
| 40 | + "Colors cannot be applied in this environment :(", |
| 41 | + "Try using a terminal or RStudio/Positron/etc." |
| 42 | + )) |
| 43 | + what_color <- NULL |
| 44 | + horse_color <- NULL |
49 | 45 | } else { |
50 | | - horse_color <- function(x) x |
| 46 | + what_color <- check_color(what_color) |
| 47 | + horse_color <- check_color(horse_color) |
51 | 48 | } |
52 | 49 |
|
53 | | - |
54 | 50 | horse <- get_who("endlesshorse", NULL) |
55 | | - |
56 | | - what_pos_start <- |
57 | | - regexpr("%s", horse)[1] - 1 |
58 | | - |
| 51 | + what_pos_start <- regexpr("%s", horse)[1] - 1 |
59 | 52 | what_pos_end <- what_pos_start + 3 |
60 | 53 |
|
61 | 54 | horse <- paste0( |
62 | | - horse_color(substr(horse, 1, what_pos_start)), |
63 | | - what_color(what), |
64 | | - horse_color(substr(horse, what_pos_end, nchar(horse))) |
| 55 | + c( |
| 56 | + color_text(bubble_say(what), what_color), |
| 57 | + bubble_tail(horse, thought_sym = "\\"), |
| 58 | + color_text(substr(horse, what_pos_end, nchar(horse)), horse_color) |
| 59 | + ), |
| 60 | + collapse = "\n" |
65 | 61 | ) |
66 | 62 |
|
67 | 63 | message(horse) |
68 | 64 | while (endless) { |
69 | | - tryCatch(interrupt = function(e) { |
70 | | - endless <<- FALSE |
71 | | - }, { |
72 | | - Sys.sleep(wait) |
73 | | - message(horse_color(" | | | | || |")) |
74 | | - wait <- wait * 1.1 |
75 | | - }) |
| 65 | + tryCatch( |
| 66 | + interrupt = function(e) { |
| 67 | + endless <<- FALSE |
| 68 | + }, |
| 69 | + { |
| 70 | + Sys.sleep(wait) |
| 71 | + message(color_text( |
| 72 | + " | | | | || |", |
| 73 | + horse_color |
| 74 | + )) |
| 75 | + wait <- wait * 1.1 |
| 76 | + } |
| 77 | + ) |
76 | 78 | } |
77 | 79 | } |
0 commit comments