update: better way to handle yes no prompt

This commit is contained in:
ngn
2024-05-02 00:50:53 +03:00
parent 013ecfb3e7
commit 0d00678c30
6 changed files with 63 additions and 46 deletions

View File

@ -95,7 +95,7 @@ void details(const char *msg, ...) {
va_list args;
va_start(args, msg);
printf(" "COLOR_RESET);
printf(" " COLOR_RESET);
vprintf(msg, args);
printf(COLOR_RESET "\n");
@ -139,18 +139,32 @@ void input(const char *msg, ...) {
}
bool yesno(const char *msg){
char question[strlen(msg)+12], inp[2] = {'n','\0'}, c;
sprintf(question, "%s [y/n]? ", msg);
if(env_mc_yes())
return true;
char *yes[] = {_("y"), _("Y")};
char *no[] = {_("n"), _("N")};
char question[strlen(msg)+12], c;
sprintf(question, _("%s [y/N] "), msg);
while(true){
input(question);
scanf("%c", &c);
inp[0] = c;
if(eq(inp, _("y")) || eq(inp, _("Y")))
return true;
else if(eq(inp, _("n")) || eq(inp, _("N")))
int c = getchar();
if(c == '\n')
return false;
getchar();
for(int i = 0; i < sizeof(yes)/sizeof(char*); i++){
if(yes[i][0] == c)
return true;
}
for(int i = 0; i < sizeof(no)/sizeof(char*); i++){
if(no[i][0] == c)
return false;
}
error(_("Please answer with y/n"));
}