#include "Tui.h" #include #include #include #include #include "Command.h" Tui::Tui(Page page) : m_page(std::move(page)) { } std::optional get_page_number(const std::string_view line) { const size_t space = line.find_first_of(' '); if (space == std::string::npos) return std::nullopt; int number = 0; const size_t size = line.size(); for (size_t i = space; i < size; i++) { const char c = line[i]; if (!std::isdigit(c)) return std::nullopt; number = number * 10 + (c - '0'); } return number; } void printHelp() { std::cout << "Available Commands:\n" << " r, refresh : Refresh the current view.\n" << " n, >, next : Move to the next item.\n" << " p, <, previous : Move to the previous item.\n" << " x, exit, q, quit : Exit the application.\n" << " seek , s : Seek to the specified number.\n" << " help, h, ? : Display this help message." << std::endl; } void Tui::run() { std::cout << m_page.str_pretty() << std::endl; for (;;) { std::cout << "Enter a command: "; std::flush(std::cout); const Command::Command command = Command::readCommand(); bool should_exit = false; auto visitor = [this, &should_exit](T0 &&arg) { using T = std::decay_t; if constexpr (std::is_same_v) { std::cout << "Invalid command!" << std::endl; } else if constexpr (std::is_same_v) { m_page += 1; std::cout << m_page.str_pretty() << std::endl; } else if constexpr (std::is_same_v) { m_page -= 1; std::cout << m_page.str_pretty() << std::endl; } else if constexpr (std::is_same_v) { m_page.refresh(); std::cout << m_page.str_pretty() << std::endl; } else if constexpr (std::is_same_v) { should_exit = true; } else if constexpr (std::is_same_v) { printHelp(); } else if constexpr (std::is_same_v) { const Command::Seek &seek = arg; const int number = seek.number; m_page = Page(number); std::cout << m_page.str_pretty() << std::endl; } }; std::visit(visitor, command); if (should_exit) return; } }