2023-05-18 17:14:55 -04:00
|
|
|
|
|
|
|
|
#ifndef READLINE_HPP
|
|
|
|
|
#define READLINE_HPP
|
|
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
|
#include <string_view>
|
2023-05-19 00:23:23 -04:00
|
|
|
#include "drvutil.hpp"
|
2023-05-18 17:14:55 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
class ReadlineDevice {
|
|
|
|
|
public:
|
2023-05-19 00:23:23 -04:00
|
|
|
using print_callback = void (*)(const std::u32string &text);
|
2023-05-18 17:14:55 -04:00
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
print_callback print_cb_;
|
2023-05-19 00:23:23 -04:00
|
|
|
std::u32string desired_command_;
|
|
|
|
|
std::u32string current_command_;
|
|
|
|
|
std::u32string desired_prompt_;
|
|
|
|
|
std::u32string current_prompt_;
|
2023-05-18 17:14:55 -04:00
|
|
|
char32_t readline_lastc_;
|
|
|
|
|
|
|
|
|
|
void erase_command();
|
2023-05-18 22:09:54 -04:00
|
|
|
void echo_command();
|
|
|
|
|
|
2023-05-18 17:14:55 -04:00
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
// The callback must be set before using the readline device.
|
|
|
|
|
void set_print_callback(print_callback cb);
|
|
|
|
|
|
|
|
|
|
// change the prompt.
|
2023-05-19 00:23:23 -04:00
|
|
|
void set_prompt(const std::u32string &prompt);
|
2023-05-18 17:14:55 -04:00
|
|
|
|
|
|
|
|
// Use this to print anything on the console.
|
2023-05-19 00:23:23 -04:00
|
|
|
void print(const std::u32string &cps);
|
2023-05-18 17:14:55 -04:00
|
|
|
|
|
|
|
|
// 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.
|
2023-05-19 00:23:23 -04:00
|
|
|
std::u32string putcode(char32_t codepoint);
|
2023-05-18 17:14:55 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#endif // READLINE_HPP
|