| 1 | #ifndef CLI_PROGRESS_BAR_H |
| 2 | #define CLI_PROGRESS_BAR_H |
| 3 | |
| 4 | /** |
| 5 | * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 6 | * SPDX-License-Identifier: Apache-2.0. |
| 7 | */ |
| 8 | |
| 9 | #include <aws/common/common.h> |
| 10 | #include <aws/common/string.h> |
| 11 | |
| 12 | struct progress_listener_group; |
| 13 | struct progress_listener; |
| 14 | |
| 15 | /** |
| 16 | * Creates parent container for progress bars. It's rendered as a single block, and in order to work correctly |
| 17 | * must be the last thing currently rendered on the terminal. It will render all progress bars at ~25 FPS |
| 18 | * if you call progress_listener_group_run_background_render_thread(). Otherwise, you can always call |
| 19 | * progress_listener_group_render() manually. |
| 20 | */ |
| 21 | struct progress_listener_group *progress_listener_group_new(struct aws_allocator *allocator); |
| 22 | |
| 23 | /** |
| 24 | * Wait on any background thread resources to clean up, then delete the group. |
| 25 | */ |
| 26 | void progress_listener_group_delete(struct progress_listener_group *group); |
| 27 | |
| 28 | /** |
| 29 | * Render the current state of the progress bars in this group. Please keep in mind. This works as long as this is the |
| 30 | * last block of text currently rendered on the terminal (the cursor position should be immediately after the last line |
| 31 | * of this group. |
| 32 | */ |
| 33 | void progress_listener_group_render(struct progress_listener_group *group); |
| 34 | |
| 35 | /** |
| 36 | * Initiates a background thread to run progress_listener_group_render at ~25 FPS |
| 37 | */ |
| 38 | void progress_listener_group_run_background_render_thread(struct progress_listener_group *group); |
| 39 | |
| 40 | /** |
| 41 | * Creates a new progress bar and returns a listener back for updating state, labels, and progress. |
| 42 | * @param group group to render the progress bar into. |
| 43 | * @param label label (what are you tracking progress for?) |
| 44 | * @param state_name name of the state (In progress, success, failed etc...). |
| 45 | * @param max_value The 100% value of the progress you're tracking |
| 46 | */ |
| 47 | struct progress_listener *progress_listener_new( |
| 48 | struct progress_listener_group *group, |
| 49 | struct aws_string *label, |
| 50 | struct aws_string *state_name, |
| 51 | uint64_t max_value); |
| 52 | |
| 53 | /** |
| 54 | * Update the state of the progress bar. |
| 55 | */ |
| 56 | void progress_listener_update_state(struct progress_listener *listener, struct aws_string *state_name); |
| 57 | |
| 58 | /** |
| 59 | * Update the progress of the progress bar. |
| 60 | * @param progress_update amount to increment the progress by. |
| 61 | */ |
| 62 | void progress_listener_update_progress(struct progress_listener *listener, uint64_t progress_update); |
| 63 | |
| 64 | void progress_listener_reset_progress(struct progress_listener *listener); |
| 65 | |
| 66 | void progress_listener_update_max_value(struct progress_listener *listener, uint64_t max_value); |
| 67 | |
| 68 | /** |
| 69 | * Update the label for the progress bar. |
| 70 | */ |
| 71 | void progress_listener_update_label(struct progress_listener *listener, struct aws_string *new_label); |
| 72 | |
| 73 | /** |
| 74 | * Render just the bar. This will not render in place and you probably should rely on the group render |
| 75 | * to handle this for you. |
| 76 | */ |
| 77 | void progress_listener_render(struct progress_listener *listener); |
| 78 | |
| 79 | #endif /* CLI_PROGRESS_BAR_H */ |