Command done

This commit is contained in:
2025-01-24 12:35:54 +01:00
parent 28613af006
commit 40d98cdbb6
8 changed files with 195 additions and 6 deletions

51
src/Command.cpp Normal file
View File

@@ -0,0 +1,51 @@
#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{}};
}
}