#ifndef READLINE_HPP #define READLINE_HPP #include #include using CodepointString = std::basic_string; class ReadlineDevice { public: using print_callback = void (*)(const CodepointString &text); private: print_callback print_cb_; CodepointString desired_command_; CodepointString current_command_; CodepointString desired_prompt_; CodepointString current_prompt_; char32_t readline_lastc_; void erase_command(); void echo_command(); public: ReadlineDevice(); // The callback must be set before using the readline device. void set_print_callback(print_callback cb); // change the prompt. void set_prompt(const CodepointString &prompt); void set_prompt_utf8(const std::string &prompt); // Use this to print anything on the console. void print(const CodepointString &cps); // Whenever the user types a character, call 'putcode'. If the code is // newline, this returns the line of text that was entered, including the // newline. Otherwise returns empty string. Backspace is handled here. CodepointString putcode(char32_t codepoint); // This can be used to convert a codepoint string into a // UTF8-string. static std::string to_utf8(const CodepointString &cps); // This can be used to convert UTF8 to a codepoint string. // Some of the bytes may not be consumed, if the source contains // a partial utf-8 sequence. Returns the Codepoint string and the // number of bytes consumed. static CodepointString from_utf8(std::string_view source, int *consumed); }; #endif // READLINE_HPP