Skip to content

Commit a027109

Browse files
committed
fix #99 fix endless_horse, shes alive again!
1 parent b57b4f7 commit a027109

File tree

2 files changed

+49
-46
lines changed

2 files changed

+49
-46
lines changed

R/endless_horse.R

Lines changed: 44 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#'
55
#' @export
66
#' @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.
88
#' Default: `TRUE`
99
#' @param wait How long to wait between leg segments (time grows
1010
#' geometrically after the first iteration in order to keep the horse
@@ -20,58 +20,60 @@
2020
#' text color or
2121
#' \href{https://github.com/r-lib/crayon#styles}{\code{crayon style function}}
2222
#' to color your steed.
23-
#' @examples \dontrun{
24-
#' endless_horse()
23+
#' @examples
2524
#' 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
4945
} else {
50-
horse_color <- function(x) x
46+
what_color <- check_color(what_color)
47+
horse_color <- check_color(horse_color)
5148
}
5249

53-
5450
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
5952
what_pos_end <- what_pos_start + 3
6053

6154
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"
6561
)
6662

6763
message(horse)
6864
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+
)
7678
}
7779
}

man/endless_horse.Rd

Lines changed: 5 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)