52 lines
1.8 KiB
C++
52 lines
1.8 KiB
C++
#include "Command.h"
|
|
|
|
#include <iostream>
|
|
|
|
|
|
namespace Command {
|
|
Command readCommand() {
|
|
if (!std::cin)
|
|
return {Exit{}};
|
|
std::string line;
|
|
if (!std::getline(std::cin, line))
|
|
return {Exit{}};
|
|
|
|
// Trim leading and trailing whitespaces
|
|
constexpr std::string_view TO_TRIM = " \t\n\r";
|
|
const size_t start = line.find_first_not_of(TO_TRIM);
|
|
const size_t end = line.find_last_not_of(TO_TRIM);
|
|
if (start == std::string::npos) return None{};
|
|
const std::string_view line_trimmed(line.data() + start, end - start + 1);
|
|
|
|
using namespace std::string_view_literals;
|
|
if (line_trimmed == "r"sv || line_trimmed == "refresh"sv)
|
|
return Refresh{};
|
|
if (line_trimmed == "n"sv || line_trimmed == ">"sv || line_trimmed == "next"sv)
|
|
return Next{};
|
|
if (line_trimmed == "p"sv || line_trimmed == "<"sv || line_trimmed == "previous"sv)
|
|
return Previous{};
|
|
if (line_trimmed == "x"sv || line_trimmed == "exit"sv || line_trimmed == "q"sv || line_trimmed == "quit"sv)
|
|
return Exit{};
|
|
if (line_trimmed == "help"sv || line_trimmed == "?"sv || line_trimmed == "?"sv)
|
|
return Help{};
|
|
|
|
if (line_trimmed.starts_with("seek") || line.starts_with("s ")) {
|
|
const size_t space = line_trimmed.find(' ');
|
|
if (space == std::string::npos)
|
|
throw NoNumberException();
|
|
int number = 0;
|
|
const size_t size = line.size();
|
|
for (size_t i = space + 1; i < size; ++i) {
|
|
const char c = line_trimmed[i];
|
|
if (!std::isdigit(c))
|
|
throw NoNumberException();
|
|
number = number * 10 + (c - '0');
|
|
}
|
|
|
|
return {Seek{number}};
|
|
}
|
|
|
|
return {None{}};
|
|
}
|
|
}
|