Debian Codegraph @

R/spinner.R

raw
1
2 spinner <- function(label, n) {
3
4 .label <- label
5 .n <- n
6 .i <- 0L
7 .cursor <- TRUE
8
9 reset <- function(label, n) {
10 .label <<- label
11 .n <<- n
12 .i <<- 0L
13 }
14
15 update <- function(items) {
16
17 if (.cursor) {
18 cat("\033[?25l", file = stdout())
19 .cursor <<- FALSE
20 }
21
22 max <- 4L
23 if (length(items) > max)
24 items <- c(head(items, max), "...")
25
26 detail <- paste(items, collapse = ", ")
27 width <- getOption("width", 80L)
28
29 msg <- sprintf(" (%i/%i) %s: %s", .i, .n, .label, detail)
30 printf("\r%s", format(strtrim(msg, width), width = width))
31 flush(stdout())
32
33 }
34
35 clear <- function() {
36 width <- getOption("width", 80L)
37 printf("\r%s\r", strrep(" ", width))
38 }
39
40 tick <- function() {
41 .i <<- .i + 1L
42 }
43
44 done <- function() {
45 .i >= .n
46 }
47
48 restore <- function() {
49 if (!.cursor) {
50 cat("\033[?25h", file = stdout())
51 .cursor <<- TRUE
52 }
53 }
54
55 list(
56 reset = reset,
57 update = update,
58 clear = clear,
59 tick = tick,
60 done = done,
61 restore = restore
62 )
63
64 }