Luna/libos/src/Prompt.cpp
apio 5c9503ac71
All checks were successful
continuous-integration/drone/push Build is passing
libos+cp: Add prompt functionality
2023-07-08 18:30:39 +02:00

25 lines
669 B
C++

#include <luna/String.h>
#include <os/File.h>
#include <os/Prompt.h>
namespace os
{
bool conditional_prompt(StringView prompt, DefaultValue fallback)
{
bool default_yes = fallback == DefaultYes;
os::print("%s [%s] ", prompt.chars(), default_yes ? "Y/n" : "y/N");
auto line_or_error = os::File::standard_input()->read_line();
if (line_or_error.has_error()) return default_yes;
auto line = line_or_error.release_value();
line.trim("\n");
if (line.view() == "y" || line.view() == "Y") return true;
if (line.view() == "n" || line.view() == "N") return false;
return default_yes;
}
}