82 lines
2.7 KiB
C++
82 lines
2.7 KiB
C++
#include "Tui.h"
|
|
|
|
#include <iostream>
|
|
#include <optional>
|
|
#include <ostream>
|
|
#include <utility>
|
|
|
|
#include "Command.h"
|
|
|
|
Tui::Tui(Page page) : m_page(std::move(page)) {
|
|
}
|
|
|
|
|
|
std::optional<int> 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 <number>, s <number> : 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]<typename T0>(T0 &&arg) {
|
|
using T = std::decay_t<T0>;
|
|
|
|
if constexpr (std::is_same_v<T, Command::None>) {
|
|
std::cout << "Invalid command!" << std::endl;
|
|
} else if constexpr (std::is_same_v<T, Command::Next>) {
|
|
m_page += 1;
|
|
std::cout << m_page.str_pretty() << std::endl;
|
|
} else if constexpr (std::is_same_v<T, Command::Previous>) {
|
|
m_page -= 1;
|
|
std::cout << m_page.str_pretty() << std::endl;
|
|
} else if constexpr (std::is_same_v<T, Command::Refresh>) {
|
|
m_page.refresh();
|
|
std::cout << m_page.str_pretty() << std::endl;
|
|
} else if constexpr (std::is_same_v<T, Command::Exit>) {
|
|
should_exit = true;
|
|
} else if constexpr (std::is_same_v<T, Command::Help>) {
|
|
printHelp();
|
|
} else if constexpr (std::is_same_v<T, Command::Seek>) {
|
|
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;
|
|
}
|
|
}
|