-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinterface.cpp
164 lines (154 loc) · 2.92 KB
/
interface.cpp
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#include "interface.h"
#include "logger.h"
#include <bits/stdc++.h>
#ifdef _WIN
# include <conio.h>
#else
# include <sys/ioctl.h>
#endif
namespace MLib
{
bool askYN(const std::string& question, bool plain = false)
{
#ifndef INTERACTIVE
plain = true;
#endif
char answer;
std::cout << question << " [y/n]: ";
std::cout.flush();
for (;;) {
if (plain) {
mLib.makeNormalConsole();
answer = tolower(getCharB());
} else {
answer = tolower(getCharNB());
}
if (answer == 'y') {
Log(LOG_verbose, true) << "askYN \"" << question << "\": true";
if (plain) {
getCharNB();
mLib.makeRawConsole();
} else
std::cout << '\n';
return true;
}
if (answer == 'n') {
Log(LOG_verbose, true) << "askYN \"" << question << "\": false";
if (plain) {
getCharNB();
mLib.makeRawConsole();
} else
std::cout << '\n';
return false;
}
if (plain) {
std::cout << "Invalid option!\n"
<< question << " [y/n]: ";
}
}
}
long readInt(std::string& query, bool plain = false)
{
#ifndef INTERACTIVE
plain = true;
#endif
long num = 0;
bool negative = false;
char c;
if (plain) {
mLib.makeNormalConsole();
std::cout << query << "> ";
fflush(stdout);
while (scanf("%ld", &num) != 1)
getCharB();
mLib.makeRawConsole();
} else {
for (;;) {
std::cout << '\r' << query << "> " << (negative ? '-' : ' ') << num << " ";
fflush(stdout);
c = getCharB();
if (c == '-') {
negative = !negative;
} else if (c == '\r' || c == '\n') {
std::cout << '\n';
break;
} else if (c == '\b') {
num /= 10;
} else if (c >= '0' && c <= '9') {
num *= 10;
num += c - '0';
}
}
if (negative)
num = -num;
}
Log(LOG_verbose, true) << "readInt \"" << query << "\": " << num;
return num;
}
// TODO
// std::string readStr(std::string& query, size_t maxLen, bool plain = false)
// {
// #ifndef INTERACTIVE
// plain = true;
// #endif
// return "";
// }
// TODO
// long ioSelection(std::string& title, std::vector<std::string>& options, bool plain = false)
// {
// #ifndef INTERACTIVE
// plain = true;
// #endif
// return 0;
// }
int getCharB()
{
int res;
#ifdef _WIN
# ifdef INTERACTIVE
res = _getch();
if (res == 0 || res == 0xe0) { // Multichar command
res <<= 8;
res |= _getch();
}
# else
res = getchar();
if (res == 0 || res == 0xe0) { // Multichar command
res <<= 8;
res |= getchar();
}
# endif
#else
int chr;
res = getchar();
if (res == 0x1b) { // ESC sequence
res = getchar();
if (res != '[') {
Log(LOG_error, true) << "Escape sequence 2nd char is not '[', but '" << res << "'";
return 0;
}
res = 0x1b5b;
do {
chr = getchar();
res = ((res << 8) & ~0xff) | chr;
} while (chr < 0x40 || chr > 0x7E);
}
#endif
return res;
}
int getCharNB()
{
#ifdef _WIN
if (!kbhit())
return 0;
#else
int n;
if (ioctl(fileno(stdin), FIONREAD, &n) != 0 || n <= 0)
return 0;
#endif
return getCharB();
}
void clearConsole()
{
}
} // namespace MLib