aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/user_input.c73
1 files changed, 19 insertions, 54 deletions
diff --git a/lib/user_input.c b/lib/user_input.c
index 3f358fa..416b59f 100644
--- a/lib/user_input.c
+++ b/lib/user_input.c
@@ -1,32 +1,6 @@
#include "spm.h"
-/**
- * Basic case-insensitive interactive choice function
- * @param answer
- * @param answer_default
- * @return
- */
-int spm_user_yesno(int answer, int empty_input_is_yes) {
- int result = 0;
- answer = tolower(answer);
-
- if (answer == 'y') {
- result = 1;
- } else if (answer == 'n') {
- result = 0;
- } else {
- if (empty_input_is_yes) {
- result = 1;
- } else {
- result = -1;
- }
- }
-
- return result;
-}
-
int spm_prompt_user(const char *msg, int empty_input_is_yes) {
- int user_choice = 0;
int status_choice = 0;
char ch_yes = 'y';
char ch_no = 'n';
@@ -38,38 +12,29 @@ int spm_prompt_user(const char *msg, int empty_input_is_yes) {
}
printf("\n%s [%c/%c] ", msg, ch_yes, ch_no);
- while ((user_choice = getchar())) {
- status_choice = spm_user_yesno(user_choice, 1);
- if (status_choice == 0) { // No
- break;
- } else if (status_choice == 1) { // Yes
- break;
- } else { // Only triggers when spm_user_yesno's second argument is zero
- puts("Please answer 'y' or 'n'...");
- }
- }
- puts("");
-
- return status_choice;
-}
+ char ch[2] = {0,};
+ int input_count = 0;
+ while (scanf("%c", ch) == 1) {
+ ch[1] = '\0';
-void spm_user_yesno_test() {
- int choice;
- int status;
- while ((choice = getchar())) {
- status = spm_user_yesno(choice, 1);
- if (status == -1) {
- puts("Please answer Y or N");
+ if (input_count != 0) {
+ input_count = 0;
+ ch[0] = '\0';
continue;
}
- else if (status == 0) {
- puts("You answered no");
- break;
- }
- else if (status == 1) {
- puts("You answered yes");
- break;
+
+ if ((isempty(ch) && empty_input_is_yes != 0)) {
+ return 1;
+ } else if ((isempty(ch) && empty_input_is_yes == 0)) {
+ return 0;
+ } else if (tolower(ch[0]) == tolower(ch_yes)) { // Yes
+ return 1;
+ } else if (tolower(ch[0]) == tolower(ch_no)) { // No
+ return 0;
+ } else {
+ printf("Please answer '%c' or '%c'...\n", tolower(ch_yes), tolower(ch_no));
}
+ input_count++;
}
}