Created
August 31, 2023 18:13
-
-
Save EmilHvitfeldt/b2abfc2ade98e40544b5775510dd6777 to your computer and use it in GitHub Desktop.
Custom Progress tracker using {cli}
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
``` r | |
tracker <- tibble::tribble( | |
~Part, ~Total, ~draft, ~nice, ~done, ~Progress, ~percentage, | |
"Part 1", 5, 1, 1, 0, 0, " 40.00%", | |
"Part 2", 7, 1, 1, 0, 0, " 40.00%", | |
"Part 3", 8, 1, 1, 0, 1, " 40.00%", | |
) | |
progress_col <- cli::make_ansi_style("hotpink") | |
draft_col <- cli::make_ansi_style("orange") | |
nice_col <- cli::make_ansi_style("#cccc00") | |
done_col <- cli::make_ansi_style("#4F9A05") | |
not_done_col <- cli::make_ansi_style("grey90") | |
make_line <- function(info, text_pad, progress_width = 50) { | |
draft <- ceiling((info$draft / info$Total) * progress_width) | |
progress <- ceiling((info$Progress / info$Total) * progress_width) | |
not_done <- progress_width - draft - progress | |
cli::cli_text( | |
info$Part, | |
strrep("\u00a0", text_pad - stringr::str_length(info$Part)), | |
draft_col(strrep("█", draft)), | |
progress_col(strrep("█", progress)), | |
not_done_col(strrep("█", not_done)), | |
"\u00a0", | |
formatC(info$draft, width = 2), | |
"\u00a0/\u00a0", | |
formatC(info$Total, width = 2), | |
" |", | |
stringr::str_replace( | |
formatC(info$draft / info$Total * 100, width = 6, digits = 2, format = "f"), | |
" ", "\u00a0" | |
), | |
"%" | |
) | |
} | |
cli::cli({ | |
cli::cli_h1("Legend") | |
cli::cli_text( | |
"Not Done = ", not_done_col("███"), | |
", ", | |
"Progress = ", progress_col("███"), | |
", ", | |
"Draft = ", draft_col("███"), | |
", ", | |
"Nice = ", nice_col("███"), | |
", ", | |
"Done = ", done_col("███") | |
) | |
text_pad <- max(stringr::str_length(tracker$Part)) + 1 | |
cli::cli_h1("Sections") | |
for (i in seq_len(nrow(tracker))) { | |
make_line(tracker[i, ], text_pad) | |
} | |
cli::cli_h1("Total") | |
tracker |> | |
dplyr::summarise( | |
Part = "Total", | |
Total = sum(Total), | |
draft = sum(draft), | |
Progress = sum(Progress), | |
) |> | |
make_line(text_pad) | |
}) | |
#> | |
#> ── Legend ────────────────────────────────────────────────────────────────────── | |
#> Not Done = ███, Progress = ███, Draft = ███, Nice = ███, Done = ███ | |
#> | |
#> ── Sections ──────────────────────────────────────────────────────────────────── | |
#> Part 1 ██████████████████████████████████████████████████ 1 / 5 | 20.00% | |
#> Part 2 ██████████████████████████████████████████████████ 1 / 7 | 14.29% | |
#> Part 3 ██████████████████████████████████████████████████ 1 / 8 | 12.50% | |
#> | |
#> ── Total ─────────────────────────────────────────────────────────────────────── | |
#> Total ██████████████████████████████████████████████████ 3 / 20 | 15.00% | |
``` | |
<sup>Created on 2023-08-31 with [reprex v2.0.2](https://reprex.tidyverse.org)</sup> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment