interface/progress_spinner.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "progress_spinner.hpp" | ||
| 2 | #include <atomic> | ||
| 3 | #include <chrono> | ||
| 4 | #include <curl/curl.h> | ||
| 5 | #include <iostream> | ||
| 6 | #include <thread> | ||
| 7 | |||
| 8 | /** | ||
| 9 | * Constructs a ProgressSpinner in manual mode. | ||
| 10 | * | ||
| 11 | * Creates a spinner that displays to stdout without UI integration. | ||
| 12 | * Must be manually started with start(). | ||
| 13 | */ | ||
| 14 | ✗ | ProgressSpinner::ProgressSpinner() : ui(nullptr), active(false) { | |
| 15 | ✗ | } | |
| 16 | |||
| 17 | /** | ||
| 18 | * Constructs a ProgressSpinner with optional UI integration. | ||
| 19 | * | ||
| 20 | * @param user_interface Pointer to a UserInterface instance for rendering, | ||
| 21 | * or nullptr for manual mode (CLI spinner). | ||
| 22 | * @param activate If true and user_interface is not nullptr, automatically | ||
| 23 | * starts the spinner. Default: false. | ||
| 24 | * | ||
| 25 | * @note The UserInterface pointer must remain valid for the lifetime of | ||
| 26 | * the ProgressSpinner object. | ||
| 27 | */ | ||
| 28 | ✗ | ProgressSpinner::ProgressSpinner(std::shared_ptr<UserInterface> user_interface, bool activate) | |
| 29 | ✗ | : ui(user_interface), active(false) { | |
| 30 | ✗ | if (activate && ui != nullptr) { | |
| 31 | ✗ | start(); | |
| 32 | } | ||
| 33 | ✗ | } | |
| 34 | |||
| 35 | /** | ||
| 36 | * Destructs the ProgressSpinner, ensuring the spinner thread is stopped. | ||
| 37 | * | ||
| 38 | * Calls stop() to cleanly shut down the spinner thread. | ||
| 39 | */ | ||
| 40 | ✗ | ProgressSpinner::~ProgressSpinner() { | |
| 41 | ✗ | stop(); | |
| 42 | ✗ | } | |
| 43 | |||
| 44 | /** | ||
| 45 | * Starts the spinner animation on a background thread. | ||
| 46 | * | ||
| 47 | * - If no UserInterface is set, displays an animated spinner (| / - \) to stdout. | ||
| 48 | * - If a UserInterface is set, delegates animation to the UI via start_spinner() | ||
| 49 | * and advance_spinner_frame(). | ||
| 50 | * | ||
| 51 | * Sets progress to 0% and resets the completion_shown flag. | ||
| 52 | * | ||
| 53 | * @note Safe to call multiple times; subsequent calls are no-ops if already running. | ||
| 54 | */ | ||
| 55 | ✗ | void ProgressSpinner::start() { | |
| 56 | ✗ | this->completion_shown = false; | |
| 57 | ✗ | if (this->ui != nullptr) { | |
| 58 | ✗ | this->ui->set_progress(0); | |
| 59 | } | ||
| 60 | ✗ | this->active = true; | |
| 61 | |||
| 62 | ✗ | if (this->ui == nullptr || !this->ui->backend_running()) { | |
| 63 | ✗ | this->spinner_thread = std::thread([this]() { | |
| 64 | ✗ | const char spinner_chars[] = {'|', '/', '-', '\\'}; | |
| 65 | ✗ | int i = 0; | |
| 66 | ✗ | while (this->active) { | |
| 67 | ✗ | fprintf(stdout, "\r%c Loading...", spinner_chars[i++ % 4]); | |
| 68 | ✗ | fflush(stdout); | |
| 69 | ✗ | std::this_thread::sleep_for(std::chrono::milliseconds(100)); | |
| 70 | } | ||
| 71 | ✗ | UserInterface::print_message_noninteractive("\n"); | |
| 72 | ✗ | }); | |
| 73 | } else { | ||
| 74 | ✗ | this->ui->start_spinner(); | |
| 75 | |||
| 76 | ✗ | this->spinner_thread = std::thread([this]() { | |
| 77 | ✗ | while (this->active) { | |
| 78 | ✗ | ui->advance_spinner_frame(); | |
| 79 | ✗ | std::this_thread::sleep_for(std::chrono::milliseconds(100)); | |
| 80 | } | ||
| 81 | ✗ | }); | |
| 82 | } | ||
| 83 | ✗ | } | |
| 84 | |||
| 85 | /** | ||
| 86 | * Stops the spinner animation and cleans up the background thread. | ||
| 87 | * | ||
| 88 | * Waits for the spinner thread to finish (via join()) and notifies the | ||
| 89 | * UserInterface to stop its spinner, if set. | ||
| 90 | * | ||
| 91 | * @note Safe to call multiple times or if start() was never called. | ||
| 92 | */ | ||
| 93 | ✗ | void ProgressSpinner::stop() { | |
| 94 | ✗ | this->active = false; | |
| 95 | ✗ | if (this->spinner_thread.joinable()) { | |
| 96 | ✗ | this->spinner_thread.join(); | |
| 97 | } | ||
| 98 | ✗ | if (this->ui != nullptr) { | |
| 99 | ✗ | this->ui->stop_spinner(); | |
| 100 | } | ||
| 101 | ✗ | } | |
| 102 | |||
| 103 | /** | ||
| 104 | * Internal progress callback for CURL integration. | ||
| 105 | * | ||
| 106 | * Processes download progress information and updates the spinner or UI accordingly. | ||
| 107 | * Displays a progress bar (▰▰▰▱) with percentage if total size is known. | ||
| 108 | * | ||
| 109 | * @param dltotal Total bytes to download (0 if unknown). | ||
| 110 | * @param dlnow Bytes downloaded so far. | ||
| 111 | * | ||
| 112 | * @return 0 to continue the CURL operation, non-zero to abort. | ||
| 113 | * | ||
| 114 | * @note This method is called frequently by CURL; keep it lightweight. | ||
| 115 | */ | ||
| 116 | ✗ | int ProgressSpinner::progress_callback_impl(curl_off_t dltotal, curl_off_t dlnow) { | |
| 117 | ✗ | if (this->ui == nullptr || !this->ui->backend_running()) { | |
| 118 | ✗ | const char spinner_chars[] = {'|', '/', '-', '\\'}; | |
| 119 | |||
| 120 | ✗ | if (dltotal > 0) { | |
| 121 | ✗ | int progress = (dlnow * 100) / dltotal; | |
| 122 | ✗ | fprintf(stdout, "\r▰▰▰▱ Progress %d%% ", progress); | |
| 123 | ✗ | fflush(stdout); | |
| 124 | } else { | ||
| 125 | static int i = 0; | ||
| 126 | ✗ | fprintf(stdout, "\r%c Loading...", spinner_chars[i++ % 4]); | |
| 127 | ✗ | fflush(stdout); | |
| 128 | } | ||
| 129 | ✗ | return 0; | |
| 130 | } | ||
| 131 | |||
| 132 | ✗ | this->ui->advance_spinner_frame(); | |
| 133 | |||
| 134 | ✗ | if (dltotal > 0) { | |
| 135 | ✗ | int progress = (dlnow * 100) / dltotal; | |
| 136 | ✗ | this->ui->set_progress(progress); | |
| 137 | } | ||
| 138 | |||
| 139 | ✗ | return 0; | |
| 140 | } | ||
| 141 | |||
| 142 | /** | ||
| 143 | * Static CURL callback adapter for progress tracking. | ||
| 144 | * | ||
| 145 | * This static method serves as the callback function for CURL's | ||
| 146 | * CURLOPT_XFERINFOFUNCTION option. It casts the clientp parameter to a | ||
| 147 | * ProgressSpinner instance and delegates to progress_callback_impl(). | ||
| 148 | * | ||
| 149 | * @param clientp Opaque pointer to the ProgressSpinner instance (passed via | ||
| 150 | * CURLOPT_XFERINFODATA). | ||
| 151 | * @param dltotal Total bytes to download (0 if unknown). | ||
| 152 | * @param dlnow Bytes downloaded so far. | ||
| 153 | * @param ultotal Total bytes to upload (unused). | ||
| 154 | * @param ulnow Bytes uploaded so far (unused). | ||
| 155 | * | ||
| 156 | * @return 0 to continue the CURL operation, 1 to abort if clientp is nullptr. | ||
| 157 | * | ||
| 158 | * @note CURL calls this frequently; ensure your clientp points to a valid | ||
| 159 | * ProgressSpinner instance. | ||
| 160 | */ | ||
| 161 | ✗ | int ProgressSpinner::progress_callback(void* clientp, curl_off_t dltotal, curl_off_t dlnow, curl_off_t ultotal, | |
| 162 | curl_off_t ulnow) { | ||
| 163 | ✗ | ProgressSpinner* self = static_cast<ProgressSpinner*>(clientp); | |
| 164 | ✗ | if (self == nullptr) { | |
| 165 | ✗ | return 1; | |
| 166 | } | ||
| 167 | ✗ | return self->progress_callback_impl(dltotal, dlnow); | |
| 168 | } | ||
| 169 |