-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathusethis-ui.Rmd
More file actions
383 lines (260 loc) · 7.41 KB
/
usethis-ui.Rmd
File metadata and controls
383 lines (260 loc) · 7.41 KB
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
---
title: "From usethis::ui functions to cli"
author: "Gábor Csárdi"
date: "`r Sys.Date()`"
description: >
Transition from usethis to cli.
output:
rmarkdown::html_document:
toc: true
toc_depth: 2
editor_options:
markdown:
wrap: sentence
---
```{r}
#| include: false
#| cache: false
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
out.width = "100%",
cache = TRUE
)
asciicast::init_knitr_engine(
echo = TRUE,
echo_input = FALSE,
startup = quote({
library(cli)
library(usethis)
set.seed(1)
})
)
```
# Introduction
```{r}
#| label: setup
library(cli)
library(usethis)
```
We'll show how to transition from the `usethis::ui_*` functions to cli 2.0.0.
# How to
## `usethis::ui_code()`
### Usage
usethis::ui_code(x)
### Example
```{asciicast}
ui_todo("Redocument with {ui_code('devtools::document()')}")
```
### With cli
In general inline code formatting can be done with inline styles in cli.
The default theme has a `"code"` class, but it also one for functions, so
this can be either of:
```{asciicast}
cli_ul("Redocument with {.code devtools::document()}")
```
```{asciicast}
cli_ul("Redocument with {.fun devtools::document}")
```
## `usethis::ui_code_block()`
### Usage
usethis::ui_code_block(x, copy = interactive(), .envir = parent.frame())
### Example
```{asciicast}
#| asciicast_rows: !expr length(format(cli::cli_code)) + 2
ui_code_block("{format(cli_code)}")
```
### With cli
`cli_code()` produces similar output and it also syntax highlight R code:
```{asciicast}
cli_code(format(cli_code))
```
However, cli does not copy stuff to the clipboard, so this has to be done
separately.
Another difference is that it also does not run glue substitutions on the
code text, so if you want that to happen you'll need to do that before the
cli call.
## `usethis::ui_done()`
### Usage
usethis::ui_done(x, .envir = parent.frame())
### Example
```{asciicast}
name <- "VignetteBuilder"
value <- "knitr, rmarkdown"
ui_done("Setting {ui_field(name)} field in DESCRIPTION to {ui_value(value)}")
```
### With cli
This is probably closest to `cli_alert_success()`:
```{asciicast}
cli_alert_success("Setting {.field {name}} field in DESCRIPTION to {.val {value}}")
```
If you want to handle success and failure, then maybe the `cli_process_*()`
functions are a better fit:
```{asciicast}
#| label: process
#| asciicast_at: "all"
#| asciicast_end_wait: 5
#| asciicast_cursor: FALSE
#| fig-alt: "First shows the task ('Setting...') in an 'i' (info) line. Then, when the task is done, the 'i' is replaced with a tick mark and 'done' added to the end of the line."
tryCatch({
cli_process_start("Setting {.field {name}} field in DESCRIPTION to {.val {value}}")
Sys.sleep(1) # <- do the task here, we just sleep
cli_process_done() },
error = function(err) {
cli_process_failed()
cli_alert_danger("Failed to ...")
}
)
```
## `usethis::ui_field()`
### Usage
usethis::ui_field(x)
### Example
```{asciicast}
name <- "VignetteBuilder"
value <- "knitr, rmarkdown"
ui_done("Setting {ui_field(name)} field in DESCRIPTION to {ui_value(value)}")
```
### With cli
cli has a `"field"` class for inline styling:
```{asciicast}
cli_alert_success("Setting {.field {name}} field in DESCRIPTION to {.val {value}}")
```
Just like `usethis::ui_field()` and similar usethis functions, cli collapses
inline vectors, before styling:
```{asciicast}
name <- c("Depends", "Imports", "Suggests")
ui_done("Setting the {ui_field(name)} field(s) in DESCRIPTION")
```
```{asciicast}
cli_alert_success("Setting the {.field {name}} field{?s} in DESCRIPTION")
```
cli also helps you with the correct pluralization:
```{asciicast}
name <- "Depends"
cli_alert_success("Setting the {.field {name}} field{?s} in DESCRIPTION")
```
## `usethis::ui_info()`
### Usage
usethis::ui_info((x, .envir = parent.frame())
### Example
```{asciicast}
ui_info("No labels need renaming")
```
### With cli
This is simply `cli_alert_info()`:
```{asciicast}
cli_alert_info("No labels need renaming")
```
## `usethis::ui_line()`
### Usage
usethis::ui_line(x, .envir = parent.frame())
### Example
```{asciicast}
ui_line("No matching issues/PRs found.")
```
### With cli
This is just a line of text, so `cli_text()` is fine for this.
One difference is that `cli_text()` will automatically wrap the long lines.
```{asciicast}
cli_text("No matching issues/PRs found.")
```
## `usethis::ui_nope()`
### Usage
ui_nope(x, .envir = parent.frame())
### With cli
cli does not support user input currently, so this has to stay in usethis.
## `usethis::ui_oops()`
### Usage
usethis::ui_oops(x, .envir = parent.frame())
### Example
```{asciicast}
ui_oops("Can't validate token. Is the network reachable?")
```
### With cli
This is mostly just `cli_alert_danger()`, but for see also the
`cli_process_*()` alternatives at `usethis::ui_done()`.
## `usethis::ui_path()`
### Usage
usethis::ui_path(x, base = NULL)
### Example
`ui_path()` formats paths as relative to the project or the supplied base
directory, and also appends a `/` to directories.
```{asciicast}
logo_path <- file.path("man", "figures", "logo.svg")
img <- "/tmp/some-image.svg"
ui_done("Copied {ui_path(img)} to {ui_path(logo_path)}")
```
### With cli
cli does not do any of these, but it does have inline markup for files and
paths:
```{asciicast}
cli_alert_success("Copied {.file {img}} to {.file {logo_path}}")
```
## `usethis::ui_stop()`
### Usage
usethis::ui_stop(x, .envir = parent.frame())
### Example
`ui_stop()` does glue substitution on the string, and then calls `stop()` to
throw an error.
```{asciicast}
#| asciicast_rows = 2
ui_stop("Could not copy {ui_path(img)} to {ui_path(logo_path)}, file already exists")
```
### With cli
`cli_abort()` does the same and is formatted using `cli_bullets()`.
```{asciicast}
cli_abort(c(
"Could not copy {.file {img}} to {.file {logo_path}}, file already exists",
"i" = "You can set {.arg overwrite = TRUE} to avoid this error"
))
```
## `usethis::ui_todo()`
### Usage
usethis::ui_todo(x, .envir = parent.frame())
### Example
```{asciicast}
ui_todo("Redocument with {ui_code('devtools::document()')}")
```
### With cli
This is a bullet, so either `cli_ul()` or `cli_alert_info()` should be
appropriate:
```{asciicast}
cli_ul("Redocument with {.fun devtools::document}")
```
## `usethis::ui_value()`
### Usage
usethis::ui_value(x)
### Example
```{asciicast}
name <- "VignetteBuilder"
value <- "knitr, rmarkdown"
ui_done("Setting {ui_field(name)} field in DESCRIPTION to {ui_value(value)}")
```
### With cli
The `"value"` inline class is appropriate for this.
```{asciicast}
cli_alert_success("Setting {.field {name}} field in DESCRIPTION to {.val {value}}")
```
## `usethis::ui_warn()`
### Usage
usethis::ui_warn(x, .envir = parent.frame())
### Example
`ui_warn()` does glue substitution on the string, and then calls `warning()` to throw a warning.
```{asciicast}
#| asciicast_rows = 2
ui_warn("Could not copy {ui_path(img)} to {ui_path(logo_path)}, file already exists")
```
### With cli
`cli_warn()` does the same and is formatted using `cli_bullets()`.
```{asciicast}
cli_warn(c(
"Could not copy {.file {img}} to {.file {logo_path}}, file already exists",
"i" = "You can set {.arg overwrite = TRUE} to avoid this warning"
))
```
## `usethis::ui_yeah()`
### Usage
ui_yeah(x, .envir = parent.frame())
### With cli
cli does not support user input currently, so this has to stay in usethis.