Created
January 2, 2022 02:18
-
-
Save lox/2e33c4dfbd3bb524c3dca42fa69b615f to your computer and use it in GitHub Desktop.
Thinking on an API for spinner/progress bar library
This file contains hidden or 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
| // Spinners and Progress bars are the same thing. | |
| // They are assembled with a go template with some built in functions or templated strings | |
| // for things like messages before and after. | |
| // Render a spinner that looks like: | |
| // ⡀ Loading blah... [3s] | |
| spinner := indicate.New(context.Background(), | |
| `{{ spinner "⠁⠂⠄⡀⢀⠠⠐⠈ " }} {{ template "message" }} [{{ elapsed }}]`). | |
| WithDefaults(indicate.SpinnerDefaults). | |
| Start() | |
| for i := 0 ; i < 100; i++ { | |
| spinner.Update("message", "Loading...") | |
| spinner.Increment() | |
| } | |
| spinner.FinishAndClear() | |
| // Render a progress bar that looks like | |
| // Crawling... 45m [########> ] 3s | |
| progress := indicate.New(context.Background(), | |
| `{{ template "prefix" }} {{ elapsed }} {{ progress "[#> ]" }} {{ eta }}`). | |
| WithTotal(100). | |
| WithTemplateFunc("prefix", func(state indicate.State) string { | |
| return fmt.Sprintf("[%d/%d]", state.Current, state.Total) | |
| }). | |
| Start() | |
| for i := 0 ; i < 100; i++ { | |
| progress.Update("prefix", "Crawling... ") | |
| progress.Increment() | |
| } | |
| progress.FinishAndClear() | |
| // Printing and re-rendering the indicator after the output | |
| progress.Println("blah") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.