#include <cassert>
#include <iostream>
#include <sstream>
#include <string>
void New(int delay, std::string message)
{
std::cout << "TODO: New" << std::endl;
}
void List()
{
std::cout << "TODO: List" << std::endl;
}
void Cancel(int index)
{
std::cout << "TODO: Cancel" << std::endl;
}
void Exit()
{
std::cout << "TODO: Exit" << std::endl;
}
enum class CommandType
{
None,
New,
List,
Cancel,
Exit
};
struct Command
{
CommandType type{};
int delay{};
std::string message;
int index{};
Command() = default;
Command(CommandType type) : type(type) {}
Command(CommandType type, int delay, const std::string& message) : type(type), delay(delay), message(message) {}
Command(CommandType type, int index) : type(type), index(index) {}
};
Command ReadCommand()
{
std::string input;
std::getline(std::cin, input);
std::stringstream parser(input);
std::string command;
if (parser >> command)
{
if (command == "exit")
return Command(CommandType::Exit);
else if (command == "list")
return Command(CommandType::List);
else if (command == "cancel")
{
int index = 0;
if (parser >> index)
return Command(CommandType::Cancel, index);
else
{
std::cerr << "Usage: cancel index" << std::endl
<< " index : index of the item to remove" << std::endl;
}
}
else if (command == "new")
{
int delay = 0;
if (parser >> delay)
{
if (delay > 0)
{
std::string message;
std::getline(parser, message);
if (!message.empty())
message = message.substr(1);
return Command(CommandType::New, delay, message);
}
else
std::cerr << "Delay must be positive" << std::endl;
}
else
{
std::cerr << "Usage: new delay message" << std::endl
<< " delay : positive delay in seconds" << std::endl
<< " message : message to show without quotes" << std::endl;
}
}
else
{
std::cerr << "Unknown command" << std::endl;
}
}
return Command(CommandType::None);
}
int main()
{
std::cout
<< "Commands:" << std::endl
<< " new <delay> <message>" << std::endl
<< " Schedule a notification with given message and delay in seconds" << std::endl
<< " delay : positive delay in seconds" << std::endl
<< " message : message to show without quotes" << std::endl
<< " list" << std::endl
<< " Show the list of active notifications" << std::endl
<< " cancel <index>" << std::endl
<< " Cancel active notification with given index" << std::endl
<< " index : index in the previously shown list" << std::endl
<< " exit" << std::endl
<< " Exit application" << std::endl;
while (true)
{
std::cout << std::endl << "> ";
const auto command = ReadCommand();
switch (command.type)
{
case CommandType::None:
continue;
case CommandType::Exit:
Exit();
return 0;
case CommandType::New:
New(command.delay, command.message);
break;
case CommandType::List:
List();
break;
case CommandType::Cancel:
Cancel(command.index);
break;
default:
assert(0);
}
}
return 0;
}