#include #include #include #include #include "server/commands/CommandEditor.h" std::mutex CommandEditor::queueMutex; RingBuffer 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 lg(queueMutex); return queue.read(); } void CommandEditor::onLineRead(char* line) { std::lock_guard 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(); }