:load command inside interactive mode

This commit is contained in:
Robert Bendun 2022-10-11 10:06:42 +02:00
parent d784da1e8d
commit cfa0513226

View File

@ -95,6 +95,7 @@ void print_repl_help()
":help - prints this help message\n"
":!<command> - allows for execution of any shell command\n"
":clear - clears screen\n"
":load <file> - loads file into Musique session\n"
;
}
@ -224,6 +225,46 @@ bool is_tty()
#endif
}
/// Handles commands inside REPL session (those starting with ':')
///
/// Returns if one of command matched
static Result<bool> handle_repl_session_commands(std::string_view command, Runner &runner)
{
if (command == "exit") {
std::exit(0);
}
if (command == "clear") {
std::cout << "\x1b[1;1H\x1b[2J" << std::flush;
return true;
}
if (command.starts_with('!')) {
// TODO Maybe use different shell invoking mechanism then system
// since on Windows it uses cmd.exe and not powershell which is
// strictly superior
Ignore(system(command.data() + 1));
return true;
}
if (command == "help") {
print_repl_help();
return true;
}
if (command.starts_with("load")) {
command = command.substr("load"sv.size());
trim(command);
std::ifstream source_file{std::string(command)};
eternal_sources.emplace_back(std::istreambuf_iterator<char>(source_file), std::istreambuf_iterator<char>());
Lines::the.add_file(std::string(command), eternal_sources.back());
Try(runner.run(eternal_sources.back(), command));
return true;
}
return false;
}
/// Fancy main that supports Result forwarding on error (Try macro)
static std::optional<Error> Main(std::span<char const*> args)
{
@ -371,11 +412,9 @@ static std::optional<Error> Main(std::span<char const*> args)
if (command.starts_with(':')) {
command.remove_prefix(1);
if (command == "exit") { break; }
if (command == "clear") { std::cout << "\x1b[1;1H\x1b[2J" << std::flush; continue; }
if (command.starts_with('!')) { Ignore(system(command.data() + 1)); continue; }
if (command == "help") { print_repl_help(); continue; }
if (!Try(handle_repl_session_commands(command, runner))) {
std::cerr << "musique: error: unrecognized REPL command '" << command << '\'' << std::endl;
}
continue;
}