123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960 |
- #include <readline/readline.h>
- #include <readline/history.h>
- #include <poll.h>
- #include <unistd.h>
- #include "server/commands/CommandEditor.h"
- std::mutex CommandEditor::queueMutex;
- RingBuffer<String, 5> CommandEditor::queue;
- CommandEditor::CommandEditor() : running(true), readThread(&CommandEditor::loop, this) {
- rl_bind_key('\t', rl_insert);
- }
- CommandEditor::~CommandEditor() {
- running = false;
- readThread.join();
- rl_callback_handler_remove();
- rl_clear_visible_line();
- }
- void CommandEditor::preTick() const {
- rl_clear_visible_line();
- }
- void CommandEditor::postTick() const {
- rl_forced_update_display();
- }
- bool CommandEditor::hasCommand() const {
- return queue.canRead();
- }
- String CommandEditor::readCommand() {
- std::lock_guard<std::mutex> lg(queueMutex);
- return queue.read();
- }
- void CommandEditor::onLineRead(char* line) {
- std::lock_guard<std::mutex> lg(queueMutex);
- add_history(line);
- queue.write(line);
- free(line);
- }
- void CommandEditor::loop() {
- rl_callback_handler_install("> ", onLineRead);
- rl_set_keyboard_input_timeout(1);
- while(running) {
- struct pollfd fds;
- fds.fd = STDIN_FILENO;
- fds.events = POLLIN;
- fds.revents = 0;
- int result = poll(&fds, 1, 1);
- if(result > 0) {
- rl_callback_read_char();
- }
- }
- rl_callback_handler_remove();
- }
|