Luna/libos/src/Prompt.cpp

25 lines
669 B
C++
Raw Normal View History

2023-07-08 16:28:04 +00:00
#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;
}
}