-
Notifications
You must be signed in to change notification settings - Fork 0
/
ask.c
76 lines (50 loc) · 1.09 KB
/
ask.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include <stdlib.h>
#include <string.h>
#include "ask.h"
#include "display.h"
int ask_opt(const char *question, struct option options[])
{
int letters[26], i, j, k;
for(i = 0; i < 26; i++) letters[i] = -1;
menu_clear();
menu_printf("%s\n\n", question);
for(j = 0; options[j].text; j++) {
if(!options[j].letter) {
for(i = 0; i < 26 && letters[i] != -1; i++);
if(i == 26) {
/* TODO: error handling */
exit(1);
}
letters[i] = j;
options[j].letter = i + 97;
}
menu_printf("%c) %s\n", options[j].letter, options[j].text);
}
menu_show();
do {
k = display_getch();
if(k == 32) k = -1;
else if(k >= 65 && k <= 90) k = letters[k - 65];
else if(k >= 97 && k <= 122) k = letters[k - 97];
else k = -2;
} while(k == -2);
menu_hide();
return k;
}
char *ask_str(const char *question, const char *def)
{
char *str;
msg_printf("%s [%s]", question, def);
str = msg_getstr();
msg_clear();
if (strlen(str)==0) {
free(str);
return strdup(def);
}
return str;
}
int ask_key(const char *question)
{
msg_printf("%s", question);
return display_getch();
}