r-lib / cli

Tools for making beautiful & useful command line interfaces
https://cli.r-lib.org/
Other
625 stars 66 forks source link

cli::pb_current_name #655

Closed tvedebrink closed 7 months ago

tvedebrink commented 7 months ago

Would it be possible to add an option to print the name of the current element if provided?

I have a made an example in purrr where the name is fetched from an external vector:

mtcars_cyl <- mtcars %>% split(.$cyl)

mtcars_cyl_names <- mtcars_cyl %>% names()

mtcars_cyl %>% 
  map_int(~ {
    Sys.sleep(1)
    .x %>% nrow()
  }, .progress = list(format = "{cli::pb_bar} [Cyl: {mtcars_cyl_names[{cli::pb_current}]}]", clear = FALSE)
  )
■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■  [Cyl: 8]
 4  6  8 
11  7 14 

Thanks for your help in advance!

gaborcsardi commented 7 months ago

This is something that would need fixing in purrr I am afraid, because purrr knows about the vectors it is iterating over, cli does not.

But I am not even sure what the best practice is to iterate over a named vector in purrr.