Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions .vscode/extensions.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"recommendations": [
"ms-vscode.cpptools",
"ms-vscode.cpp-devtools",
"ms-vscode.cpptools-extension-pack",
"ms-vscode.cpptools-themes",
"ms-vscode.cmake-tools",
Expand All @@ -9,6 +10,11 @@
"xaver.clang-format",
"foxundermoon.shell-format",
"timonwong.shellcheck",
"redhat.vscode-yaml"
"redhat.vscode-yaml",
"jasonnutter.vscode-codeowners",
"github.vscode-github-actions",
"ms-python.python",
"ms-python.vscode-pylance",
"ms-python.vscode-python-envs"
]
}
}
12 changes: 9 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
cmake_minimum_required(VERSION 3.20)

project(cpp-workspace VERSION 0.2.1 LANGUAGES CXX)
project(cpp-workspace VERSION 0.3.0 LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
Expand All @@ -14,16 +14,22 @@ set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
add_executable(cpp-workspace-app
src/main.cpp
src/interface/progress_spinner.cpp
src/interface/user-interface.cpp
src/interface/user_interface.cpp
src/ollama/ollama_client.cpp
src/utils/text-utils.cpp
src/storage/qdrant_client.cpp
src/utils/curl_utils.cpp
src/utils/text_utils.cpp
src/utils/system_utils.cpp
src/utils/embedding_utils.cpp
src/utils/http_utils.hpp
)

target_include_directories(cpp-workspace-app
PRIVATE
${CMAKE_SOURCE_DIR}/src
${CMAKE_SOURCE_DIR}/src/interface
${CMAKE_SOURCE_DIR}/src/ollama
${CMAKE_SOURCE_DIR}/src/storage
${CMAKE_SOURCE_DIR}/src/utils
)

Expand Down
27 changes: 23 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,10 @@ In order to run your own copy of the project one must fulfill the following requ
🏆 **Visual Studio Code** - recommended for all operating systems

Suggested extensions:
- C/C++ Extension Pack (Microsoft)
- CMake Tools (Microsoft)
- CMake (twxs)
- [C/C++ Extension Pack](https://marketplace.visualstudio.com/items?itemName=ms-vscode.cpptools-extension-pack)
- [CMake Tools](https://marketplace.visualstudio.com/items?itemName=ms-vscode.cmake-tools)

🆗 **Visual Studio** - for Windows development
See [`.vscode/extensions`](.vscode/extensions.json) for the complete list.

### Core dependencies

Expand Down Expand Up @@ -120,6 +119,26 @@ cz c

## FAQ

### Commands

1. Install

```bash
bash ./tools/shell/install.sh --help
```

2. Build

```bash
bash ./tools/shell/build.sh --help
```

3. Format

```bash
bash ./tools/shell/format.sh
```

### Manual build

```bash
Expand Down
79 changes: 65 additions & 14 deletions src/interface/progress_spinner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,51 @@
#include <iostream>
#include <thread>

/**
* Constructs a ProgressSpinner in manual mode.
*
* Creates a spinner that displays to stdout without UI integration.
* Must be manually started with start().
*/
ProgressSpinner::ProgressSpinner() : ui(nullptr), active(false) {
}

/**
* Constructs a ProgressSpinner with optional UI integration.
*
* @param user_interface Pointer to a UserInterface instance for rendering,
* or nullptr for manual mode (CLI spinner).
* @param activate If true and user_interface is not nullptr, automatically
* starts the spinner. Default: false.
*
* @note The UserInterface pointer must remain valid for the lifetime of
* the ProgressSpinner object.
*/
ProgressSpinner::ProgressSpinner(UserInterface* user_interface, bool activate) : ui(user_interface), active(false) {
if (activate && ui != nullptr) {
start();
}
}

/**
* Destructs the ProgressSpinner, ensuring the spinner thread is stopped.
*
* Calls stop() to cleanly shut down the spinner thread.
*/
ProgressSpinner::~ProgressSpinner() {
stop();
}

/**
* Implementation details:
* - In manual mode, renders spinner characters and progress directly to stdout.
* - In interactive mode, delegates animation and progress updates to the UserInterface.
* - Resets completion_shown flag and initializes progress to 0%.
* Starts the spinner animation on a background thread.
*
* - If no UserInterface is set, displays an animated spinner (| / - \) to stdout.
* - If a UserInterface is set, delegates animation to the UI via start_spinner()
* and advance_spinner_frame().
*
* Sets progress to 0% and resets the completion_shown flag.
*
* @note Safe to call multiple times; subsequent calls are no-ops if already running.
*/
void ProgressSpinner::start() {
this->completion_shown = false;
Expand All @@ -39,7 +66,7 @@ void ProgressSpinner::start() {
std::cout << "\r" << spinner_chars[i++ % 4] << " Loading..." << std::flush;
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
printf("\n");
UserInterface::print_message_noninteractive("\n");
});
} else {
this->ui->start_spinner();
Expand All @@ -54,8 +81,12 @@ void ProgressSpinner::start() {
}

/**
* Sets active flag to false, waits for the spinner thread to finish,
* and notifies the UserInterface to stop its spinner if present.
* Stops the spinner animation and cleans up the background thread.
*
* Waits for the spinner thread to finish (via join()) and notifies the
* UserInterface to stop its spinner, if set.
*
* @note Safe to call multiple times or if start() was never called.
*/
void ProgressSpinner::stop() {
this->active = false;
Expand All @@ -68,10 +99,17 @@ void ProgressSpinner::stop() {
}

/**
* Implementation notes:
* - In manual mode, displays progress percentage if total is known, otherwise shows spinner.
* - In interactive mode, updates both spinner frame and progress bar in the UI.
* - Returns 0 to allow CURL to continue operation.
* Internal progress callback for CURL integration.
*
* Processes download progress information and updates the spinner or UI accordingly.
* Displays a progress bar (▰▰▰▱) with percentage if total size is known.
*
* @param dltotal Total bytes to download (0 if unknown).
* @param dlnow Bytes downloaded so far.
*
* @return 0 to continue the CURL operation, non-zero to abort.
*
* @note This method is called frequently by CURL; keep it lightweight.
*/
int ProgressSpinner::progress_callback_impl(curl_off_t dltotal, curl_off_t dlnow) {
if (this->ui == nullptr || !this->ui->is_running()) {
Expand All @@ -98,10 +136,23 @@ int ProgressSpinner::progress_callback_impl(curl_off_t dltotal, curl_off_t dlnow
}

/**
* Static callback adapter that casts the clientp void pointer to a ProgressSpinner
* instance and delegates to the progress_callback_impl() method.
* Static CURL callback adapter for progress tracking.
*
* This static method serves as the callback function for CURL's
* CURLOPT_XFERINFOFUNCTION option. It casts the clientp parameter to a
* ProgressSpinner instance and delegates to progress_callback_impl().
*
* @param clientp Opaque pointer to the ProgressSpinner instance (passed via
* CURLOPT_XFERINFODATA).
* @param dltotal Total bytes to download (0 if unknown).
* @param dlnow Bytes downloaded so far.
* @param ultotal Total bytes to upload (unused).
* @param ulnow Bytes uploaded so far (unused).
*
* @return 0 to continue the CURL operation, 1 to abort if clientp is nullptr.
*
* @return 0 to continue the operation, 1 if clientp is nullptr.
* @note CURL calls this frequently; ensure your clientp points to a valid
* ProgressSpinner instance.
*/
int ProgressSpinner::progress_callback(void* clientp, curl_off_t dltotal, curl_off_t dlnow, curl_off_t ultotal,
curl_off_t ulnow) {
Expand Down
123 changes: 7 additions & 116 deletions src/interface/progress_spinner.hpp
Original file line number Diff line number Diff line change
@@ -1,55 +1,19 @@
#pragma once

#include "user-interface.hpp"
#include "user_interface.hpp"
#include <atomic>
#include <curl/curl.h>
#include <iostream>
#include <thread>

/**
* Displays a progress indicator for asynchronous operations, with optional UI integration.
* Displays and manages an animated progress spinner with optional download tracking.
*
* ProgressSpinner supports two modes:
* - **Manual mode**: Displays an animated spinner to stdout for CLI applications.
* - **Interactive mode**: Integrates with a UserInterface instance for display within
* a graphical or TUI framework.
*
* The spinner runs on a separate thread and updates independently of the main operation.
* Designed to work seamlessly with CURL operations via the static progress_callback method.
*
* @note Thread-safe: Uses std::atomic for synchronization without locks.
*
* @example Manual mode (CLI spinner):
* @code
* ProgressSpinner spinner;
* spinner.start();
* curl_easy_perform(curl_handle);
* spinner.stop();
* @endcode
*
* @example Interactive mode (with UserInterface):
* @code
* UserInterface ui;
* ProgressSpinner spinner(&ui, true); // auto-starts
* // UI displays progress and spinner animation
* curl_easy_perform(curl_handle);
* spinner.stop();
* @endcode
*
* @example CURL integration with progress tracking:
* @code
* ProgressSpinner spinner(&ui);
* spinner.start();
*
* CURL* curl = curl_easy_init();
* curl_easy_setopt(curl, CURLOPT_XFERINFOFUNCTION, ProgressSpinner::progress_callback);
* curl_easy_setopt(curl, CURLOPT_XFERINFODATA, &spinner);
* curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0L);
* curl_easy_perform(curl);
*
* spinner.stop();
* curl_easy_cleanup(curl);
* @endcode
* Runs a spinner animation in a dedicated background thread, providing visual feedback
* during network operations. Supports two modes: UI-driven (displays via UserInterface)
* and manual mode (spinner_thread active but output deferred). Integrates with CURL
* progress callbacks to track download progress. Thread-safe via atomic flags for
* state management (active, completion_shown).
*/
class ProgressSpinner {
private:
Expand All @@ -74,91 +38,18 @@ class ProgressSpinner {
std::atomic<bool> completion_shown{false};

public:
/**
* Constructs a ProgressSpinner in manual mode.
*
* Creates a spinner that displays to stdout without UI integration.
* Must be manually started with start().
*/
ProgressSpinner();

/**
* Constructs a ProgressSpinner with optional UI integration.
*
* @param user_interface Pointer to a UserInterface instance for rendering,
* or nullptr for manual mode (CLI spinner).
* @param activate If true and user_interface is not nullptr, automatically
* starts the spinner. Default: false.
*
* @note The UserInterface pointer must remain valid for the lifetime of
* the ProgressSpinner object.
*/
explicit ProgressSpinner(UserInterface* user_interface, bool activate = false);

/**
* Destructs the ProgressSpinner, ensuring the spinner thread is stopped.
*
* Calls stop() to cleanly shut down the spinner thread.
*/
~ProgressSpinner();

/**
* Starts the spinner animation on a background thread.
*
* - If no UserInterface is set, displays an animated spinner (| / - \) to stdout.
* - If a UserInterface is set, delegates animation to the UI via start_spinner()
* and advance_spinner_frame().
*
* Sets progress to 0% and resets the completion_shown flag.
*
* @note Safe to call multiple times; subsequent calls are no-ops if already running.
*/
void start();

/**
* Stops the spinner animation and cleans up the background thread.
*
* Waits for the spinner thread to finish (via join()) and notifies the
* UserInterface to stop its spinner, if set.
*
* @note Safe to call multiple times or if start() was never called.
*/
void stop();

/**
* Internal progress callback for CURL integration.
*
* Processes download progress information and updates the spinner or UI accordingly.
* Displays a progress bar (▰▰▰▱) with percentage if total size is known.
*
* @param dltotal Total bytes to download (0 if unknown).
* @param dlnow Bytes downloaded so far.
*
* @return 0 to continue the CURL operation, non-zero to abort.
*
* @note This method is called frequently by CURL; keep it lightweight.
*/
int progress_callback_impl(curl_off_t dltotal, curl_off_t dlnow);

/**
* Static CURL callback adapter for progress tracking.
*
* This static method serves as the callback function for CURL's
* CURLOPT_XFERINFOFUNCTION option. It casts the clientp parameter to a
* ProgressSpinner instance and delegates to progress_callback_impl().
*
* @param clientp Opaque pointer to the ProgressSpinner instance (passed via
* CURLOPT_XFERINFODATA).
* @param dltotal Total bytes to download (0 if unknown).
* @param dlnow Bytes downloaded so far.
* @param ultotal Total bytes to upload (unused).
* @param ulnow Bytes uploaded so far (unused).
*
* @return 0 to continue the CURL operation, 1 to abort if clientp is nullptr.
*
* @note CURL calls this frequently; ensure your clientp points to a valid
* ProgressSpinner instance.
*/
static int progress_callback(void* clientp, curl_off_t dltotal, curl_off_t dlnow, curl_off_t ultotal,
curl_off_t ulnow);
};
Loading
Loading