### Install the library on *nix Source: https://github.com/amokhuginnsson/replxx/blob/master/README.md Install the compiled library to the default system location. ```bash sudo make install ``` -------------------------------- ### Install libreplxx-dev Source: https://github.com/amokhuginnsson/replxx/wiki/How-to-Install-replxx-static-lib-deb-from-PPA Installs the static development library package. ```bash sudo apt install libreplxx-dev ``` -------------------------------- ### Initialize Replxx Instance and Main Input Loop Source: https://context7.com/amokhuginnsson/replxx/llms.txt Initializes a Replxx instance, installs a window change handler, configures history size and uniqueness, and enters a main input loop. Handles EOF and adds non-empty input to history. ```cpp #include "replxx.hxx" using Replxx = replxx::Replxx; int main() { // Create the Replxx instance Replxx rx; // Install window change handler for terminal resize events rx.install_window_change_handler(); // Configure settings rx.set_max_history_size(128); rx.set_max_hint_rows(3); rx.set_unique_history(true); // Main input loop for (;;) { char const* input = rx.input("\x1b[1;32mreplxx\x1b[0m> "); if (input == nullptr) { break; // EOF received } if (strlen(input) > 0) { rx.history_add(input); rx.print("You entered: %s\n", input); } } return 0; } ``` -------------------------------- ### Install the library to a custom location Source: https://github.com/amokhuginnsson/replxx/blob/master/README.md Use the DESTDIR variable to specify a custom installation path. ```bash make DESTDIR=/tmp install ``` -------------------------------- ### Install Library Targets Source: https://github.com/amokhuginnsson/replxx/blob/master/CMakeLists.txt Installs the 'replxx' library and its associated targets to the appropriate directories based on the build type. ```cmake install( TARGETS replxx EXPORT replxx-targets RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} ) ``` -------------------------------- ### List installed files Source: https://github.com/amokhuginnsson/replxx/wiki/How-to-Install-replxx-static-lib-deb-from-PPA Displays the contents of the deb package to verify installed paths and files. ```bash $ dpkg-deb -c ./libreplxx-dev_0.0.4-1_amd64.deb drwxr-xr-x root/root 0 2022-11-01 14:18 ./ drwxr-xr-x root/root 0 2022-11-01 14:18 ./usr/ drwxr-xr-x root/root 0 2022-11-01 14:18 ./usr/include/ -rw-r--r-- root/root 24898 2021-10-20 14:53 ./usr/include/replxx.h -rw-r--r-- root/root 22575 2021-10-20 14:53 ./usr/include/replxx.hxx drwxr-xr-x root/root 0 2022-11-01 14:18 ./usr/lib/ drwxr-xr-x root/root 0 2022-11-01 14:18 ./usr/lib/x86_64-linux-gnu/ -rw-r--r-- root/root 556514 2022-11-01 14:18 ./usr/lib/x86_64-linux-gnu/libreplxx.a drwxr-xr-x root/root 0 2022-11-01 14:18 ./usr/share/ drwxr-xr-x root/root 0 2022-11-01 14:18 ./usr/share/cmake/ drwxr-xr-x root/root 0 2022-11-01 14:18 ./usr/share/cmake/replxx/ -rw-r--r-- root/root 1382 2022-11-01 14:18 ./usr/share/cmake/replxx/replxx-config-version.cmake -rw-r--r-- root/root 483 2022-11-01 14:18 ./usr/share/cmake/replxx/replxx-config.cmake -rw-r--r-- root/root 850 2022-11-01 14:18 ./usr/share/cmake/replxx/replxx-targets-none.cmake -rw-r--r-- root/root 3535 2022-11-01 14:18 ./usr/share/cmake/replxx/replxx-targets.cmake drwxr-xr-x root/root 0 2022-11-01 14:18 ./usr/share/doc/ drwxr-xr-x root/root 0 2022-11-01 14:18 ./usr/share/doc/libreplxx-dev/ -rw-r--r-- root/root 106 2022-11-01 14:18 ./usr/share/doc/libreplxx-dev/README.Debian -rw-r--r-- root/root 140 2022-11-01 14:18 ./usr/share/doc/libreplxx-dev/changelog.Debian.gz -rw-r--r-- root/root 7276 2022-11-01 14:18 ./usr/share/doc/libreplxx-dev/copyright ``` -------------------------------- ### Install Configuration Files Source: https://github.com/amokhuginnsson/replxx/blob/master/CMakeLists.txt Installs the generated CMake configuration files ('replxx-config.cmake' and 'replxx-config-version.cmake') for package management. ```cmake install( FILES "${PROJECT_BINARY_DIR}/replxx-config-version.cmake" "${PROJECT_BINARY_DIR}/replxx-config.cmake" DESTINATION ${CMAKE_INSTALL_DATADIR}/cmake/replxx ) ``` -------------------------------- ### Install Export Targets Source: https://github.com/amokhuginnsson/replxx/blob/master/CMakeLists.txt Installs the export set for 'replxx-targets' to enable CMake to find the package. ```cmake install( EXPORT replxx-targets NAMESPACE replxx:: DESTINATION ${CMAKE_INSTALL_DATADIR}/cmake/replxx ) ``` -------------------------------- ### Target Include Directories Source: https://github.com/amokhuginnsson/replxx/blob/master/CMakeLists.txt Configures include directories for the 'replxx' target, distinguishing between build and installation paths. ```cmake target_include_directories( replxx PUBLIC $ $ PRIVATE $ ) ``` -------------------------------- ### Conditional Build Options Source: https://github.com/amokhuginnsson/replxx/blob/master/CMakeLists.txt Defines options for building examples and generating package configuration files, controlled by the build type and source directory. ```cmake cmake_dependent_option( REPLXX_BUILD_EXAMPLES "Build the examples" ON "CMAKE_SOURCE_DIR STREQUAL PROJECT_SOURCE_DIR" OFF ) cmake_dependent_option( REPLXX_BUILD_PACKAGE "Generate package target" ON "CMAKE_SOURCE_DIR STREQUAL PROJECT_SOURCE_DIR" OFF ) ``` -------------------------------- ### C API Initialization and Main Loop Source: https://context7.com/amokhuginnsson/replxx/llms.txt Initialize the Replxx library, configure settings like history size and hint rows, load history, and enter the main input loop. Handles terminal resize and potential signal interruptions. ```c #include #include #include #include "replxx.h" int main(void) { // Initialize replxx Replxx* replxx = replxx_init(); // Install window change handler for terminal resize replxx_install_window_change_handler(replxx); // Configure settings replxx_set_max_history_size(replxx, 100); replxx_set_max_hint_rows(replxx, 3); replxx_set_unique_history(replxx, 1); // Load history replxx_history_load(replxx, "./history.txt"); printf("Type 'quit' to exit\n"); // Main loop while (1) { char const* result = NULL; // Handle EAGAIN (signal interrupted) do { result = replxx_input(replxx, "\x1b[1;32mreplxx\x1b[0m> "); } while ((result == NULL) && (errno == EAGAIN)); if (result == NULL) { printf("\n"); break; } if (strcmp(result, "quit") == 0) { replxx_history_add(replxx, result); break; } if (*result != '\0') { replxx_print(replxx, "You entered: %s\n", result); replxx_history_add(replxx, result); } } // Save history replxx_history_save(replxx, "./history.txt"); // Cleanup replxx_end(replxx); return 0; } ``` -------------------------------- ### Manage Key Bindings and Input State in C Source: https://context7.com/amokhuginnsson/replxx/llms.txt Demonstrates how to define custom key handlers using ReplxxState and bind them alongside built-in actions. ```c #include #include #include #include #include "replxx.h" // Custom handler to convert input to uppercase ReplxxActionResult uppercase_handler(int code, void* userData) { Replxx* replxx = (Replxx*)userData; ReplxxState state; // Get current state replxx_get_state(replxx, &state); // Duplicate and transform text char* text = strdup(state.text); for (int i = 0; text[i]; i++) { text[i] = toupper(text[i]); } // Set modified state state.text = text; replxx_set_state(replxx, &state); free(text); return REPLXX_ACTION_RESULT_CONTINUE; } // Custom handler using built-in action ReplxxActionResult word_delete_handler(int code, void* userData) { Replxx* replxx = (Replxx*)userData; return replxx_invoke(replxx, REPLXX_ACTION_KILL_TO_BEGINING_OF_WORD, 0); } int main(void) { Replxx* replxx = replxx_init(); // Bind built-in actions by name replxx_bind_key_internal(replxx, REPLXX_KEY_BACKSPACE, "delete_character_left_of_cursor"); replxx_bind_key_internal(replxx, REPLXX_KEY_DELETE, "delete_character_under_cursor"); replxx_bind_key_internal(replxx, REPLXX_KEY_LEFT, "move_cursor_left"); replxx_bind_key_internal(replxx, REPLXX_KEY_RIGHT, "move_cursor_right"); replxx_bind_key_internal(replxx, REPLXX_KEY_UP, "history_previous"); replxx_bind_key_internal(replxx, REPLXX_KEY_DOWN, "history_next"); replxx_bind_key_internal(replxx, REPLXX_KEY_TAB, "complete_line"); replxx_bind_key_internal(replxx, REPLXX_KEY_CONTROL('R'), "history_incremental_search"); replxx_bind_key_internal(replxx, REPLXX_KEY_CONTROL('L'), "clear_screen"); // Bind custom handlers replxx_bind_key(replxx, REPLXX_KEY_F2, uppercase_handler, replxx); replxx_bind_key(replxx, REPLXX_KEY_CONTROL('W'), word_delete_handler, replxx); // Emulate key presses programmatically // replxx_emulate_key_press(replxx, 'H'); // replxx_emulate_key_press(replxx, 'i'); printf("Press F2 to uppercase, Ctrl+W to delete word\n"); while (1) { char const* input = replxx_input(replxx, "> "); if (input == NULL) break; if (*input != '\0') { replxx_print(replxx, "Input: %s\n", input); replxx_history_add(replxx, input); } } replxx_end(replxx); return 0; } ``` -------------------------------- ### Create build directory on *nix Source: https://github.com/amokhuginnsson/replxx/blob/master/README.md Prepare a build directory for the compilation process. ```bash mkdir -p build && cd build ``` -------------------------------- ### Create build directory on Windows Source: https://github.com/amokhuginnsson/replxx/blob/master/README.md Prepare a build directory using the MS-DOS command prompt. ```cmd md build cd build ``` -------------------------------- ### Build the library on *nix Source: https://github.com/amokhuginnsson/replxx/blob/master/README.md Compile the library using CMake and make. ```bash cmake -DCMAKE_BUILD_TYPE=Release .. && make ``` -------------------------------- ### Build and Configure Replxx with CMake Source: https://context7.com/amokhuginnsson/replxx/llms.txt Provides shell commands for building the library and a snippet for integrating it into a project's CMakeLists.txt. ```bash # Clone the repository git clone https://github.com/AmokHuginnsson/replxx.git cd replxx # Create build directory mkdir -p build && cd build # Configure and build cmake -DCMAKE_BUILD_TYPE=Release .. make # Install (optional) sudo make install # Or install to custom location make DESTDIR=/opt/replxx install ``` ```cmake # CMakeLists.txt for your application cmake_minimum_required(VERSION 3.5) project(myapp) # Find replxx (if installed) find_package(replxx REQUIRED) # Or use as subdirectory ``` -------------------------------- ### Add PPA and update package list Source: https://github.com/amokhuginnsson/replxx/wiki/How-to-Install-replxx-static-lib-deb-from-PPA Configures the system to use the siliconja/replxx PPA and refreshes the local package index. ```bash sudo add-apt-repository ppa:siliconja/replxx sudo apt update ``` -------------------------------- ### Bind Custom Key Handlers in C++ Source: https://context7.com/amokhuginnsson/replxx/llms.txt Demonstrates binding built-in actions via string identifiers and custom C++ functions to specific key combinations. ```cpp #include "replxx.hxx" #include #include using Replxx = replxx::Replxx; // Custom key handler that shows a message Replxx::ACTION_RESULT show_message( Replxx& rx, std::string message, char32_t code ) { rx.invoke(Replxx::ACTION::CLEAR_SELF, 0); rx.print("%s\n", message.c_str()); rx.invoke(Replxx::ACTION::REPAINT, 0); return Replxx::ACTION_RESULT::CONTINUE; } // Custom handler to convert line to uppercase Replxx::ACTION_RESULT uppercase_line(Replxx& rx, char32_t code) { Replxx::State state = rx.get_state(); std::string text(state.text()); for (char& c : text) { c = toupper(c); } rx.set_state(Replxx::State(text.c_str(), state.cursor_position())); return Replxx::ACTION_RESULT::CONTINUE; } int main() { Replxx rx; using namespace std::placeholders; // Bind built-in actions to keys by name rx.bind_key_internal(Replxx::KEY::BACKSPACE, "delete_character_left_of_cursor"); rx.bind_key_internal(Replxx::KEY::DELETE, "delete_character_under_cursor"); rx.bind_key_internal(Replxx::KEY::LEFT, "move_cursor_left"); rx.bind_key_internal(Replxx::KEY::RIGHT, "move_cursor_right"); rx.bind_key_internal(Replxx::KEY::UP, "history_previous"); rx.bind_key_internal(Replxx::KEY::DOWN, "history_next"); rx.bind_key_internal(Replxx::KEY::HOME, "move_cursor_to_begining_of_line"); rx.bind_key_internal(Replxx::KEY::END, "move_cursor_to_end_of_line"); rx.bind_key_internal(Replxx::KEY::TAB, "complete_line"); // Bind Ctrl+R for incremental history search rx.bind_key_internal(Replxx::KEY::control('R'), "history_incremental_search"); // Bind Ctrl+W to kill word rx.bind_key_internal(Replxx::KEY::control('W'), "kill_to_begining_of_word"); // Bind Ctrl+U to kill line rx.bind_key_internal(Replxx::KEY::control('U'), "kill_to_begining_of_line"); // Bind Ctrl+K to kill to end of line rx.bind_key_internal(Replxx::KEY::control('K'), "kill_to_end_of_line"); // Bind Ctrl+Y to yank (paste from kill ring) rx.bind_key_internal(Replxx::KEY::control('Y'), "yank"); // Bind Ctrl+L to clear screen rx.bind_key_internal(Replxx::KEY::control('L'), "clear_screen"); // Bind custom handler to F1 rx.bind_key(Replxx::KEY::F1, std::bind(&show_message, std::ref(rx), "Help: Type commands and press Enter", _1)); // Bind custom handler to F2 (uppercase current line) rx.bind_key(Replxx::KEY::F2, std::bind(&uppercase_line, std::ref(rx), _1)); // Bind custom handler to Ctrl+Enter for commit rx.bind_key_internal(Replxx::KEY::control(Replxx::KEY::ENTER), "commit_line"); std::cout << "Press F1 for help, F2 to uppercase line\n"; for (;;) { char const* input = rx.input("> "); if (input == nullptr) break; if (strlen(input) > 0) { rx.history_add(input); } } return 0; } ``` -------------------------------- ### Configure Multiline Input and Hooks in C++ Source: https://context7.com/amokhuginnsson/replxx/llms.txt Shows how to enable multiline editing, bracketed paste, and register modification callbacks for input processing. ```cpp #include "replxx.hxx" #include using Replxx = replxx::Replxx; // Modify callback to update prompt with input length void modify_hook(std::string& line, int& cursorPosition) { // This callback is invoked on every modification // Can be used to transform input or update state } int main() { Replxx rx; // Enable multiline indentation (indent continuation lines) rx.set_indent_multiline(true); // Enable bracketed paste mode for proper multiline paste rx.enable_bracketed_paste(); // Register modify callback (optional) rx.set_modify_callback(modify_hook); // Set case-insensitive completion and history search rx.set_ignore_case(true); std::cout << "Multiline mode enabled. Press Ctrl+Enter to submit.\n"; std::cout << "Use Enter for new line, arrow keys to navigate.\n\n"; for (;;) { char const* input = rx.input("\x1b[1;32mmulti\x1b[0m> "); if (input == nullptr) { break; } std::string text(input); if (text == ".quit") { break; } if (!text.empty()) { rx.history_add(text); // Count lines int lines = 1; for (char c : text) { if (c == '\n') lines++; } rx.print("Received %d line(s):\n%s\n", lines, text.c_str()); } } // Disable bracketed paste before exit rx.disable_bracketed_paste(); return 0; } ``` -------------------------------- ### Implement History Scanning and Persistence in C Source: https://context7.com/amokhuginnsson/replxx/llms.txt Shows how to iterate through history entries and manage history files using sync, load, and save operations. ```c #include #include #include "replxx.h" void show_history(Replxx* replxx) { int size = replxx_history_size(replxx); printf("History contains %d entries:\n", size); // Start history scan ReplxxHistoryScan* scan = replxx_history_scan_start(replxx); ReplxxHistoryEntry entry; int index = 0; // Iterate through history while (replxx_history_scan_next(replxx, scan, &entry) == 0) { replxx_print(replxx, "%4d [%s]: %s\n", index++, entry.timestamp, entry.text); } // Stop scan replxx_history_scan_stop(replxx, scan); } int main(void) { Replxx* replxx = replxx_init(); char const* history_file = "./replxx_history.txt"; // Configure history replxx_set_max_history_size(replxx, 500); replxx_set_unique_history(replxx, 1); // Load existing history if (replxx_history_load(replxx, history_file) == 0) { printf("History loaded from %s\n", history_file); } while (1) { char const* input = replxx_input(replxx, "> "); if (input == NULL) break; if (strcmp(input, "/history") == 0) { show_history(replxx); } else if (strcmp(input, "/clear") == 0) { replxx_history_clear(replxx); printf("History cleared\n"); } else if (strcmp(input, "/quit") == 0) { replxx_history_add(replxx, input); break; } else if (*input != '\0') { replxx_history_add(replxx, input); } } // Sync history (load, merge, save) replxx_history_sync(replxx, history_file); // Alternative: just save without merge // replxx_history_save(replxx, history_file); replxx_end(replxx); return 0; } ``` -------------------------------- ### Configure Package Configuration File Source: https://github.com/amokhuginnsson/replxx/blob/master/CMakeLists.txt Generates the 'replxx-config.cmake' file from a template for package management. ```cmake configure_package_config_file( "${PROJECT_SOURCE_DIR}/replxx-config.cmake.in" "${PROJECT_BINARY_DIR}/replxx-config.cmake" INSTALL_DESTINATION ${CMAKE_INSTALL_DATADIR}/cmake/replxx NO_CHECK_REQUIRED_COMPONENTS_MACRO NO_SET_AND_CHECK_MACRO ) ``` -------------------------------- ### Set Hint and Highlighter Callbacks in C Source: https://context7.com/amokhuginnsson/replxx/llms.txt Register custom hint and syntax highlighting callbacks to enhance user experience. The hint callback suggests completions, while the highlighter callback colors input based on character type. ```c #include #include #include #include "replxx.h" char* examples[] = {"help", "history", "quit", NULL}; // Hint callback void hint_hook( char const* context, replxx_hints* hints, int* contextLen, ReplxxColor* color, void* userData ) { char** words = (char**)userData; int len = strlen(context); int start = len; while (start > 0 && context[start - 1] != ' ') { start--; } char const* prefix = context + start; int prefixLen = len - start; *contextLen = prefixLen; if (prefixLen < 1) return; int matchCount = 0; for (int i = 0; words[i] != NULL; i++) { if (strncmp(words[i], prefix, prefixLen) == 0) { replxx_add_hint(hints, words[i]); matchCount++; } } // Set color based on match count *color = (matchCount == 1) ? REPLXX_COLOR_GREEN : REPLXX_COLOR_GRAY; } // Highlighter callback void highlighter_hook( char const* input, ReplxxColor* colors, int size, void* userData ) { for (int i = 0; i < size; i++) { if (isdigit(input[i])) { colors[i] = REPLXX_COLOR_BRIGHTMAGENTA; } else if (input[i] == '"' || input[i] == '\'') { colors[i] = REPLXX_COLOR_BRIGHTGREEN; } else if (ispunct(input[i])) { colors[i] = REPLXX_COLOR_BRIGHTBLUE; } } } int main(void) { Replxx* replxx = replxx_init(); // Register callbacks replxx_set_hint_callback(replxx, hint_hook, examples); replxx_set_highlighter_callback(replxx, highlighter_hook, NULL); // Configure hints replxx_set_max_hint_rows(replxx, 4); replxx_set_hint_delay(replxx, 100); while (1) { char const* input = replxx_input(replxx, "> "); if (input == NULL) break; if (*input != '\0') { replxx_history_add(replxx, input); } } replxx_end(replxx); return 0; } ``` -------------------------------- ### Link replxx in CMake Source: https://context7.com/amokhuginnsson/replxx/llms.txt Use these commands in your CMakeLists.txt to link the replxx library. Define REPLXX_STATIC if you are performing static linking. ```cmake add_subdirectory(replxx) add_executable(myapp main.cpp) target_link_libraries(myapp PRIVATE replxx::replxx) # For static linking, define REPLXX_STATIC target_compile_definitions(myapp PRIVATE REPLXX_STATIC) ``` -------------------------------- ### Write Package Version File Source: https://github.com/amokhuginnsson/replxx/blob/master/CMakeLists.txt Creates a 'replxx-config-version.cmake' file to specify the package version compatibility. ```cmake write_basic_package_version_file( "${PROJECT_BINARY_DIR}/replxx-config-version.cmake" COMPATIBILITY AnyNewerVersion ) ``` -------------------------------- ### Generate Visual Studio solution files Source: https://github.com/amokhuginnsson/replxx/blob/master/README.md Generate project files for Visual Studio using CMake for 32-bit or 64-bit architectures. ```bash cmake -G "Visual Studio 12 2013" -DCMAKE_BUILD_TYPE=Release .. ``` ```bash cmake -G "Visual Studio 12 2013 Win64" -DCMAKE_BUILD_TYPE=Release .. ``` -------------------------------- ### Define replxx Library Source: https://github.com/amokhuginnsson/replxx/blob/master/CMakeLists.txt Adds the 'replxx' library target and creates an alias 'replxx::replxx'. ```cmake add_library(replxx ${replxx-sources}) add_library(replxx::replxx ALIAS replxx) ``` -------------------------------- ### Set Target Properties Source: https://github.com/amokhuginnsson/replxx/blob/master/CMakeLists.txt Configures properties for the 'replxx' target, including versioning and postfixes for different build types. ```cmake set_target_properties(replxx PROPERTIES VERSION ${PROJECT_VERSION}) set_property(TARGET replxx PROPERTY DEBUG_POSTFIX -d) set_property(TARGET replxx PROPERTY RELWITHDEBINFO_POSTFIX -rd) set_property(TARGET replxx PROPERTY MINSIZEREL_POSTFIX) if ( NOT BUILD_SHARED_LIBS AND MSVC ) set_property(TARGET replxx PROPERTY OUTPUT_NAME replxx-static) endif() ``` -------------------------------- ### Project Information Variables Source: https://github.com/amokhuginnsson/replxx/blob/master/CMakeLists.txt Sets variables for project URL, display name, and contact information. ```cmake set(REPLXX_URL_INFO_ABOUT "https://github.com/AmokHuginnsson/replxx") set(REPLXX_DISPLAY_NAME "replxx") set(REPLXX_CONTACT "amok@codestation.org") ``` -------------------------------- ### Target Link Options and Libraries Source: https://github.com/amokhuginnsson/replxx/blob/master/CMakeLists.txt Sets link options and libraries for the 'replxx' target, including debug symbols, coverage flags, and the Threads library. Handles different CMake versions for link options. ```cmake if (NOT CMAKE_VERSION VERSION_LESS 3.13) target_link_options( replxx PRIVATE $<$,$>:-g -ggdb -g3 -ggdb3> $<${coverage-config}:--coverage> $<${is-msvc}:/ignore:4099> ) else() # "safest" way prior to 3.13 target_link_libraries( replxx PRIVATE $<${coverage-config}:--coverage> $<${is-msvc}:/ignore:4099> ) endif() target_link_libraries(replxx PUBLIC Threads::Threads) ``` -------------------------------- ### Register Syntax Highlighter Callback Source: https://context7.com/amokhuginnsson/replxx/llms.txt Register a callback function to provide custom syntax highlighting for input. The callback receives the input string and a color buffer to fill based on patterns like numbers and strings. ```cpp #include "replxx.hxx" #include #include using Replxx = replxx::Replxx; // Keyword to color mapping std::unordered_map keywords = { {"select", Replxx::Color::BRIGHTBLUE}, {"from", Replxx::Color::BRIGHTBLUE}, {"where", Replxx::Color::BRIGHTBLUE}, {"insert", Replxx::Color::BRIGHTGREEN}, {"update", Replxx::Color::YELLOW}, {"delete", Replxx::Color::BRIGHTRED} }; // Highlighter callback function void highlighter_hook( std::string const& context, Replxx::colors_t& colors ) { // Highlight numbers in yellow std::regex number_regex("[0-9]+"); std::smatch match; std::string str = context; size_t pos = 0; while (std::regex_search(str, match, number_regex)) { pos += match.prefix().length(); for (size_t i = 0; i < match[0].length(); ++i) { colors[pos + i] = Replxx::Color::YELLOW; } pos += match[0].length(); str = match.suffix(); } // Highlight strings in green std::regex string_regex("\"[^\"]*\"|'[^']*'"); str = context; pos = 0; while (std::regex_search(str, match, string_regex)) { pos += match.prefix().length(); for (size_t i = 0; i < match[0].length(); ++i) { colors[pos + i] = Replxx::Color::BRIGHTGREEN; } pos += match[0].length(); str = match.suffix(); } } int main() { Replxx rx; // Register the highlighter callback rx.set_highlighter_callback(highlighter_hook); // Enable colors (default is enabled) rx.set_no_color(false); for (;;) { char const* input = rx.input("> "); if (input == nullptr) break; if (strlen(input) > 0) { rx.history_add(input); } } return 0; } ``` -------------------------------- ### Target Compile Options Source: https://github.com/amokhuginnsson/replxx/blob/master/CMakeLists.txt Configures compiler-specific options for the 'replxx' target, including optimization levels, debug symbols, and warnings. ```cmake target_compile_options( replxx PRIVATE $<$,${compiler-id-clang-or-gnu}>:-fomit-frame-pointer> $<$,${compiler-id-clang-or-gnu}>:-Os> $<$,$>:-g -ggdb -g3 -ggdb3> $<${coverage-config}:-O0 --coverage> $<${coverage-config}:-fno-inline -fno-default-inline> $<${coverage-config}:-fno-inline-small-functions> $<${compiler-id-clang-or-gnu}:-Wall -Wextra> $<$:-Wno-unknown-pragmas> ) ``` -------------------------------- ### Set Contextual Hint Callback Source: https://context7.com/amokhuginnsson/replxx/llms.txt Registers a hint callback function that provides contextual hints displayed as the user types. Configures hint display settings like maximum rows and delay. ```cpp #include "replxx.hxx" #include #include using Replxx = replxx::Replxx; std::vector commands = { ".help", ".history", ".quit", ".exit", ".clear" }; // Hint callback function Replxx::hints_t hint_hook( std::string const& context, int& contextLen, Replxx::Color& color ) { Replxx::hints_t hints; // Only show hints if user has typed at least 2 characters std::string prefix = context.substr(context.length() - contextLen); if (prefix.size() < 2) { return hints; } // Find matching hints for (auto const& cmd : commands) { if (cmd.compare(0, prefix.size(), prefix) == 0) { hints.push_back(cmd); } } // Set hint color to green if exactly one match if (hints.size() == 1) { color = Replxx::Color::GREEN; } else { color = Replxx::Color::GRAY; } return hints; } int main() { Replxx rx; // Register the hint callback rx.set_hint_callback(hint_hook); // Configure hint display rx.set_max_hint_rows(3); // Show up to 3 hint rows rx.set_hint_delay(100); // 100ms delay before showing hints for (;;) { char const* input = rx.input("> "); if (input == nullptr) break; if (strlen(input) > 0) { rx.history_add(input); } } return 0; } ``` -------------------------------- ### Include CMake Helper Modules Source: https://github.com/amokhuginnsson/replxx/blob/master/CMakeLists.txt Includes standard CMake modules for package configuration, dependent options, and generating export headers. ```cmake include(CMakePackageConfigHelpers) include(CMakeDependentOption) include(GenerateExportHeader) include(GNUInstallDirs) ``` -------------------------------- ### Default Build Type Source: https://github.com/amokhuginnsson/replxx/blob/master/CMakeLists.txt Sets the default build type to 'Release' if it is not already defined, and warns the user. ```cmake if (NOT CMAKE_BUILD_TYPE) message(AUTHOR_WARNING "CMAKE_BUILD_TYPE not set. Defaulting to Release") set(CMAKE_BUILD_TYPE Release) endif() ``` -------------------------------- ### Target Compile Definitions Source: https://github.com/amokhuginnsson/replxx/blob/master/CMakeLists.txt Sets compile definitions for the 'replxx' target, including static/shared library flags and Windows-specific warnings. ```cmake target_compile_definitions( replxx PUBLIC $<$>:REPLXX_STATIC> $<$:REPLXX_BUILDING_DLL> PRIVATE $<$:_CRT_SECURE_NO_WARNINGS=1 /ignore:4503> ) ``` -------------------------------- ### Set Minimum CMake Version and Project Details Source: https://github.com/amokhuginnsson/replxx/blob/master/CMakeLists.txt Specifies the minimum required CMake version and defines project metadata like name, version, and languages. ```cmake cmake_minimum_required(VERSION 3.5) project( replxx # HOMEPAGE_URL "https://github.com/AmokHuginnsson/replxx" # DESCRIPTION "replxx - Read Evaluate Print Loop library" VERSION 0.0.4 LANGUAGES CXX C ) ``` -------------------------------- ### Find Source Files Source: https://github.com/amokhuginnsson/replxx/blob/master/CMakeLists.txt Uses file(GLOB) to find all source files matching the defined patterns. ```cmake file(GLOB replxx-sources ${replxx-source-patterns}) ``` -------------------------------- ### C++ Rich Color Highlighting Source: https://context7.com/amokhuginnsson/replxx/llms.txt Implement custom syntax highlighting with rich terminal colors, including 256-color support, backgrounds, and text attributes. This function is designed to be passed to Replxx's set_highlighter_callback. ```cpp #include "replxx.hxx" using Replxx = replxx::Replxx; using namespace replxx::color; void highlighter_with_rich_colors( std::string const& context, Replxx::colors_t& colors ) { for (size_t i = 0; i < context.length(); ++i) { char c = context[i]; if (isdigit(c)) { // Yellow number with bold colors[i] = bold(Replxx::Color::YELLOW); } else if (c == '"' || c == '"') { // Green string delimiter with underline colors[i] = underline(Replxx::Color::BRIGHTGREEN); } else if (ispunct(c)) { // Use 6x6x6 RGB color for punctuation (orange) colors[i] = rgb666(5, 3, 0); } else if (isupper(c)) { // Use grayscale for uppercase (bright gray) colors[i] = grayscale(20); } } // Combine foreground and background colors // Example: red text on yellow background if (context.find("error") != std::string::npos) { size_t pos = context.find("error"); for (size_t i = 0; i < 5; ++i) { colors[pos + i] = Replxx::Color::BRIGHTRED | bg(Replxx::Color::YELLOW); } } // Use bold + underline + color combination if (context.find("warning") != std::string::npos) { size_t pos = context.find("warning"); for (size_t i = 0; i < 7; ++i) { colors[pos + i] = bold(underline(Replxx::Color::YELLOW)); } } } int main() { Replxx rx; rx.set_highlighter_callback(highlighter_with_rich_colors); // Demonstrate available basic colors rx.print("Basic colors:\n"); rx.print(" BLACK, RED, GREEN, BROWN, BLUE, MAGENTA, CYAN, LIGHTGRAY\n"); rx.print(" GRAY, BRIGHTRED, BRIGHTGREEN, YELLOW, BRIGHTBLUE\n"); rx.print(" BRIGHTMAGENTA, BRIGHTCYAN, WHITE, DEFAULT\n\n"); rx.print("Extended colors:\n"); rx.print(" rgb666(r,g,b) - 6x6x6 RGB cube (r,g,b: 0-5)\n"); rx.print(" grayscale(level) - 24 grayscale levels (0-23)\n\n"); rx.print("Attributes:\n"); rx.print(" bold(color), underline(color), bg(color)\n\n"); for (;;) { char const* input = rx.input("> "); if (input == nullptr) break; if (strlen(input) > 0) { rx.history_add(input); } } return 0; } ``` -------------------------------- ### Set Completion Callback in C Source: https://context7.com/amokhuginnsson/replxx/llms.txt Register a custom completion callback to populate the completions list based on user input context. The callback receives the current input context and a list to add completions to. User data can be passed for custom logic. ```c #include #include #include "replxx.h" char* examples[] = { "help", "history", "quit", "exit", "clear", "select", "insert", "update", "delete", NULL }; void completion_hook( char const* context, replxx_completions* completions, int* contextLen, void* userData ) { char** words = (char**)userData; // Find the start of the current word int len = strlen(context); int start = len; while (start > 0 && context[start - 1] != ' ') { start--; } char const* prefix = context + start; int prefixLen = len - start; *contextLen = prefixLen; // Add matching completions for (int i = 0; words[i] != NULL; i++) { if (strncmp(words[i], prefix, prefixLen) == 0) { // Add plain completion replxx_add_completion(completions, words[i]); // Or add colored completion // replxx_add_color_completion(completions, words[i], REPLXX_COLOR_GREEN); } } } int main(void) { Replxx* replxx = replxx_init(); // Register completion callback with user data replxx_set_completion_callback(replxx, completion_hook, examples); // Configure completion behavior replxx_set_word_break_characters(replxx, " \t\n"); replxx_set_completion_count_cutoff(replxx, 50); replxx_set_double_tab_completion(replxx, 0); replxx_set_complete_on_empty(replxx, 1); while (1) { char const* input = replxx_input(replxx, "> "); if (input == NULL) break; if (*input != '\0') { replxx_history_add(replxx, input); } } replxx_end(replxx); return 0; } ``` -------------------------------- ### Generate Export Header Source: https://github.com/amokhuginnsson/replxx/blob/master/CMakeLists.txt Generates a header file for exporting symbols from the replxx library. ```cmake generate_export_header(replxx) ``` -------------------------------- ### Source File Patterns Source: https://github.com/amokhuginnsson/replxx/blob/master/CMakeLists.txt Defines the patterns for source files to be included in the build. Includes an adjustment for CMake versions greater than 3.11. ```cmake set(replxx-source-patterns "src/*.cpp" "src/*.cxx") if (CMAKE_VERSION VERSION_GREATER 3.11) list(INSERT replxx-source-patterns 0 CONFIGURE_DEPENDS) endif() ``` -------------------------------- ### Set C++ and C Standard Versions Source: https://github.com/amokhuginnsson/replxx/blob/master/CMakeLists.txt Configures the C++ and C language standards to be used for compilation. Defaults to C++11 and C99 if not already defined. ```cmake if (NOT DEFINED CMAKE_CXX_STANDARD) set(CMAKE_CXX_STANDARD 11) endif() if (NOT DEFINED CMAKE_C_STANDARD) set(CMAKE_C_STANDARD 99) endif() ``` -------------------------------- ### Find Required Packages Source: https://github.com/amokhuginnsson/replxx/blob/master/CMakeLists.txt Locates and loads the 'Threads' package, which is necessary for multi-threaded operations. ```cmake find_package(Threads) ``` -------------------------------- ### Set Tab Completion Callback Source: https://context7.com/amokhuginnsson/replxx/llms.txt Registers a completion callback function that provides suggestions based on the current input context. Configures tab completion behavior like word break characters and double-tab completion. ```cpp #include "replxx.hxx" #include #include using Replxx = replxx::Replxx; // Define available commands for completion std::vector commands = { ".help", ".history", ".quit", ".exit", ".clear", "hello", "world", "database", "select", "insert" }; // Completion callback function Replxx::completions_t completion_hook( std::string const& context, int& contextLen ) { Replxx::completions_t completions; // Get the prefix to match std::string prefix = context.substr(context.length() - contextLen); // Find matching completions for (auto const& cmd : commands) { if (cmd.compare(0, prefix.size(), prefix) == 0) { // Add completion with optional color completions.emplace_back(cmd, Replxx::Color::DEFAULT); } } return completions; } int main() { Replxx rx; // Register the completion callback rx.set_completion_callback(completion_hook); // Configure completion behavior rx.set_word_break_characters(" \n\t.,-%!;:=*~^'"/?<>|[](){}"); rx.set_completion_count_cutoff(128); // Paginate if more than 128 completions rx.set_double_tab_completion(false); // Complete on single tab rx.set_complete_on_empty(true); // Show completions on empty input rx.set_beep_on_ambiguous_completion(false); for (;;) { char const* input = rx.input("> "); if (input == nullptr) break; if (strlen(input) > 0) { rx.history_add(input); } } return 0; } ``` -------------------------------- ### Manage Command History Source: https://context7.com/amokhuginnsson/replxx/llms.txt Configure and manage command history, including setting maximum size, enabling unique entries, loading from and saving to files, and clearing history. Supports scanning history entries. ```cpp #include "replxx.hxx" #include #include #include using Replxx = replxx::Replxx; int main() { Replxx rx; std::string history_file = "./history.txt"; // Set maximum history size rx.set_max_history_size(1000); // Enable unique history (no duplicate entries) rx.set_unique_history(true); // Load history from file std::ifstream ifs(history_file); if (ifs.is_open()) { rx.history_load(ifs); ifs.close(); } for (;;) { char const* input = rx.input("> "); if (input == nullptr) break; std::string cmd(input); if (cmd == ".history") { // Scan and display history Replxx::HistoryScan hs = rx.history_scan(); int index = 0; while (hs.next()) { std::cout << std::setw(4) << index++ << ": " << hs.get().text() << std::endl; } } else if (cmd == ".clear_history") { rx.history_clear(); std::cout << "History cleared" << std::endl; } else if (cmd == ".quit") { rx.history_add(cmd); break; } else if (!cmd.empty()) { rx.history_add(cmd); std::cout << "Executed: " << cmd << std::endl; } } // Save history to file (merges with existing) rx.history_sync(history_file); // Alternative: save without merging // std::ofstream ofs(history_file); // rx.history_save(ofs); return 0; } ``` -------------------------------- ### Windows Export Symbols and Standard Requirements Source: https://github.com/amokhuginnsson/replxx/blob/master/CMakeLists.txt Enables exporting all symbols on Windows for shared libraries and enforces C++ standard compliance. ```cmake set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON) set(CMAKE_CXX_STANDARD_REQUIRED ON) set(CMAKE_CXX_EXTENSIONS ON) ``` -------------------------------- ### Coverage Build Configuration Source: https://github.com/amokhuginnsson/replxx/blob/master/CMakeLists.txt Sets a variable to determine if coverage compilation flags should be applied, based on build configuration and compiler. ```cmake set(coverage-config $,$>) ``` -------------------------------- ### Compiler Detection Source: https://github.com/amokhuginnsson/replxx/blob/master/CMakeLists.txt Sets boolean variables to detect if the compiler is Clang, MSVC, or GNU, and a combined variable for Clang or GNU. ```cmake set(is-clang $,$>) set(is-msvc $) set(is-gnu $) set(compiler-id-clang-or-gnu $) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.