#include "request.hpp" #include namespace remaining { namespace { std::string response(const std::string &opcode, int result) { return opcode + " " + std::to_string(result); } bool command_id(const std::vector &words, int &id) { return words.size() == 2 && parse_signed(words[1], id); } bool command_priority(const std::vector &words, int &id, int &priority) { return words.size() == 3 && parse_signed(words[1], id) && parse_signed(words[2], priority); } bool command_duration(const std::vector &words, int &id, std::uint64_t &duration, int &priority) { return words.size() == 4 && parse_signed(words[1], id) && parse_duration(words[2], duration) && parse_signed(words[3], priority); } } // namespace Console::Console(Manager &manager) : manager_(manager) { } bool Console::execute(const std::string &line, std::string &output) { const auto words = split_words(line); if (words.empty() || words[0].front() == '#') { output.clear(); return true; } return dispatch(words, output); } bool Console::dispatch(const std::vector &words, std::string &output) { const std::string &op = words[0]; int id = 0; int priority = 0; std::uint64_t duration = 0; if (op == "A") { if (!command_duration(words, id, duration, priority)) { return false; } output = response("A", manager_.add(id, duration, priority)); return true; } if (op == "M") { if (!command_duration(words, id, duration, priority)) { return false; } output = response("M", manager_.amend(id, duration, priority)); return true; } if (op == "D") { int new_id = 0; if (!command_priority(words, id, new_id)) { return false; } output = response("D", manager_.duplicate(id, new_id)); return true; } if (op == "T") { if (!command_priority(words, id, priority)) { return false; } output = response("T", manager_.reprioritize(id, priority)); return true; } if (op == "C") { if (!command_id(words, id)) { return false; } output = response("C", manager_.cancel(id)); return true; } if (op == "R") { if (!command_id(words, id)) { return false; } output = response("R", manager_.run(id)); return true; } if (op == "Q") { if (!command_id(words, id)) { return false; } Request request{}; const int result = manager_.query(id, request); output = result == 0 ? "Q 0 " + format_request(request) : response("Q", result); return true; } if (op == "N") { if (words.size() != 1) { return false; } int selected = -1; const int result = manager_.run_next(selected); output = "N " + std::to_string(selected) + " " + std::to_string(result); return true; } if (op == "B") { if (words.size() != 1) { return false; } unsigned count = 0; const int result = manager_.run_batch(count); output = "B " + std::to_string(count) + " " + std::to_string(result); return true; } if (op == "L") { if (words.size() != 1) { return false; } output = "L " + format_list(manager_.list()); return true; } if (op == "O") { if (words.size() != 1) { return false; } output = "O " + format_order(manager_.pending_order()); return true; } if (op == "S") { if (words.size() != 1) { return false; } output = "S " + format_totals(manager_.totals()); return true; } if (op == "P") { if (words.size() != 1) { return false; } output = "P " + std::to_string(manager_.purge()); return true; } if (op == "Z") { if (words.size() != 1) { return false; } manager_.reset(); output = "Z 0"; return true; } return false; } } // namespace remaining