forked from aedobbyn/multicolor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
README.Rmd
215 lines (147 loc) · 6.99 KB
/
README.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
---
output: github_document
---
[![Travis build status](https://travis-ci.org/aedobbyn/multicolor.svg?branch=master)](https://travis-ci.org/aedobbyn/multicolor)
[![AppVeyor build status](https://ci.appveyor.com/api/projects/status/github/aedobbyn/multicolor?branch=master&svg=true)](https://ci.appveyor.com/project/aedobbyn/multicolor)
[![Coverage status](https://codecov.io/gh/aedobbyn/multicolor/branch/master/graph/badge.svg)](https://codecov.io/github/aedobbyn/multicolor?branch=master)
[![CRAN_Status_Badge](http://www.r-pkg.org/badges/version/multicolor)](https://cran.r-project.org/package=multicolor)
[![rstudio mirror
downloads](https://cranlogs.r-pkg.org/badges/grand-total/multicolor)](https://github.com/r-hub/cranlogs.app)
```{r, echo = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "README-",
eval = FALSE
)
```
# multicolor `r emo::ji("art")` <img src="./man/img/egret.jpg" alt="egret" height="225px" align="right">
Apply multiple colors to your messages, warnings, and errors. Built on the [`crayon`](https://github.com/r-lib/crayon) package. Pairs nicely with [`cowsay`](https://github.com/sckott/cowsay). The [blog post](https://dobb.ae/2018/07/19/how-does-multicolor-actually-work/) has a bit of backstory and walkthrough of how it works.
**`r emo::ji("sparkles")` New `r emo::ji("sparkles")`:** *`multicolor` 0.1.2 now supports RMarkdown with `type = "rmd"`. See package vignette `vignette("rmd", "multicolor")` for examples.*
<p align="left">
<img src="https://media.giphy.com/media/8YNM4kP4NnFrvkIhVM/giphy.gif" alt="whale" height="310px">
</p>
<br>
### Installation
Stable, from CRAN:
```{r}
install.packages("multicolor")
```
or the development version from GitHub:
```{r, eval=FALSE}
devtools::install_github("aedobbyn/multicolor")
```
*Note*: colors cannot be applied in the R GUI and certain other environments. RStudio or any terminal should work fine<sup>1</sup>. [RMarkdown](https://github.com/aedobbyn/multicolor/blob/dev/vignettes/rmd.Rmd) is also in play `r emo::ji("thumbsup")`.
<br>
### Usage
```{r}
library(multicolor)
```
Supply a character vector of colors to `colors`. This defaults to `"rainbow"`, i.e., `c("red", "orange", "yellow", "green", "blue", "purple")`.
```{r example}
multi_color("Hello world")
```
<p align="left">
<img src="./man/img/hello_world.jpg" alt="hello_world" height="22px">
</p>
If you want to unveil your creation slowly (as @cortinah's whale gif above), use **`crawl`**. You can vary the speed with the `pause` argument.
<p align="left">
<img src="https://media.giphy.com/media/7Wsh1CNjLSS0ddgSnl/giphy.gif" height="310px" alt="chicken">
</p>
<br>
#### Options
The text supplied will be divided into even(ish) chunks of those colors when **`recycle_chars`** is false. If it's true, each color will apply to a single character, and the `colors` vector will be recycled over the length of the input string supplied.
Any character vector of R colors or hex values are fair game.
<p align="left">
<img src="./man/img/plasma_daemon.png" alt="hello_world" height="600px">
</p>
The default **`direction`** is vertical, but horizontal is also an option by setting the `direction` param to "horizontal".
The default **`type`** messages the result. If you want the bare string back with color encodings attached, use `type = "string"`, which looks like:
```{r}
multi_color("Why are avocado pits so big?",
type = "string")
```
<p align="left">
<img src="./man/img/avocado_q.jpg" alt="avocado_q" height="37px">
</p>
So you can ask <a href="https://youtu.be/B759dzymyoc?t=14s" target="_blank">important questions</a> `r emo::ji("index_pointing_up")`and answer them colorfully `r emo::ji("point_down")`:
```{r}
multi_color("The wild avocado grows in subtropical jungles, so the new sprout has to get several feet tall before it can share sunlight (to make food) with its neighbors. Until it grows out of their shadows, it relies on nutrients in the seed, so it'd better be big.",
sample(colors(),
sample(10, 1)))
```
<p align="left">
<img src="./man/img/avocado_a.jpg" alt="avocado_a" height="55px">
</p>
<br>
### ASCII art with [`cowsay`](https://github.com/sckott/cowsay)
All `cowsay` animals are exported in `multicolor::things`, but to get the animals to speak, you need [`cowsay`](https://github.com/sckott/cowsay).
```{r}
library(cowsay)
say(what = "holygrail",
by = "yoda",
what_color = "olivedrab",
by_color = colors()[which(grepl("green", colors()))])
```
<p align="left">
<img src="./man/img/yoda.jpg" alt="yoda" height="460px">
</p>
Error in style:
```{r}
my_error <- multi_color("An unknown error has occurred.",
type = "string")
stop(my_error)
```
<p align="left">
<img src="./man/img/err.jpg" alt="error" height="17px">
</p>
And with character:
```{r}
my_msg <-
say(what = "Error: something went horribly wrong",
by = "rms",
what_color = viridisLite::magma(5)[3],
by_color = viridisLite::magma(10),
type = "string")
e <- simpleError(my_msg)
tryCatch(log("foo"), error = function(e) message(my_msg))
```
<p align="left">
<img src="./man/img/rms_error.jpg" alt="rms" height="380px">
</p>
Or just send messages to your users that they'll want to read.
```{r}
this_variable <- "foo"
this_option <- "bar"
say(what =
glue::glue("Aha, I see you set {this_variable} to {this_option}. Excellent choice."),
by = "owl",
what_color = c("seagreen3", "turquoise3", "seagreen3"),
by_color = c("turquoise3", "seagreen3", "turquoise3"))
```
<p align="left">
<img src="./man/img/foo_to_bar.jpg" alt="foo_to_bar" height="210px">
</p>
<br>
### Reshaping Text
`multicolor` includes functions to reshape text strings to create visually interesting designs. `center_string` centers text strings within the console, while `triangle_string` can create upward and downward pointing triangles of varying width. These can be combined with `multi_color` to create visuals such as these:
```{r}
triangle_string(ipsum, step = 4, maxlen = 11, display = TRUE) %>%
center_string() %>%
multi_color(direction = "horizontal", viridis::plasma(n = 6, direction = -1, begin = 0.3))
```
<p align="left">
<img src="./man/img/lorem_triangle_up.png" alt="triangle_up" height="210px">
</p>
```{r}
triangle_string(ipsum, step = -4, maxlen = 56, display = TRUE) %>%
center_string() %>%
multi_color(direction = "horizontal", viridis::inferno(n = 8, direction = -1, begin = 0.3))
```
<p align="left">
<img src="./man/img/lorem_triangle_down.png" alt="triangle_down" height="235px">
</p>
<br>
That's it! PRs & bug reports v welcome. `r emo::ji("art")`
<br>
<sup>1</sup> If coloring isn't possible, you'll get a warning on load and every time `multi_color` evaluates. The `type` argument will auto-set to `"string"`. To check how these environments are determined, see [`multicolor:::use_color`](https://github.com/aedobbyn/multicolor/blob/dev/R/utils.R#L13). If using `multicolor` in another package, you might consider inserting a replacement for the case when this evaluates to `FALSE`.