-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheck_value.cpp
More file actions
160 lines (151 loc) · 3.65 KB
/
check_value.cpp
File metadata and controls
160 lines (151 loc) · 3.65 KB
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
#include "anscii.h"
#include "ttyspeed.h"
#include <string.h>
#include <string>
int ttySpeed::check(char c) {
if (c < 32 || c > 122) {
non_ascii(c);
return 0;
}
// For normal characters
if (c == text[index]) { // Character matches
correct();
return 0;
} else {
incorrect();
}
return 0;
}
void ttySpeed::correct() {
if (y == height__)
return;
if (x <= width__ - 1) { // Is before the border line
if (text[index] != ' ')
printf("%s%c%s", theme.correct, text[index], theme.reset);
else
printf("%s·%s", theme.space, theme.reset);
fflush(stdout);
*(arr + index) = 1; // 1 correct 0 not touched 2 incorrect
x++; // cursor
index++;
count++;
if (x == width__) {
mv_nextline();
mv_right(1);
}
}
}
void ttySpeed::incorrect() {
if (y == height__)
return;
if (x <= width__ - 1) { // Is before the border line
if (text[index] != ' ') {
printf("%s%c%s", theme.incorrect, text[index], theme.reset);
} else {
printf("%s%s·%s", __STRIKE_, theme.incorrect, theme.reset);
}
*(arr + index) = 2;
x++;
fflush(stdout);
index++;
count++;
if (x == width__) {
mv_nextline();
mv_right(1);
}
}
}
void ttySpeed::non_ascii(char a) {
switch (a) {
case 127:
handle_backspace();
break;
case 8:
ctrl_backspace();
break;
}
return;
}
void ttySpeed::handle_backspace() {
if (index == 0)
return;
/* if (text[index - 1] == ' ')
return;
*/
backspace_count++;
if (x <= ((_border) ? 2 : 1)) {
if (y == ((_border) ? 2 : 1)) // No place to move Y-axis
return;
else {
mv_xy(width__ - 1, y - 1); // Move to (end-1)col of previous
if (text[index - 1] != ' ') {
printf("%s%c%s", theme.text, text[index - 1], theme.reset);
} else {
printf("%s·%s", theme.space, theme.reset);
}
this->x = this->x + 1;
mv_left(1);
index--;
*(arr + index) = 0;
fflush(stdout);
return;
}
}
mv_left(1);
if (text[index - 1] != ' ') {
printf("%s%c%s", theme.text, text[index - 1], theme.reset);
} else {
printf("%s·%s", theme.space, theme.reset);
}
this->x = this->x + 1;
fflush(stdout);
index--;
*(arr + index) = 0;
mv_left(1);
return;
}
void ttySpeed::ctrl_backspace() { return; }
void ttySpeed::printText(std::string text, int x, int y) {
// Set the values of properties
this->text = text;
this->str_len = text.length();
this->arr = new int[str_len];
memset(this->arr, 0, str_len);
printf("\033[%d;%dH", y, x); // Move to x,y
this->y = y;
this->x = x;
int width = (_border) ? width__ - 2 : width__;
int text_len = (int)text.length();
for (int i = 0; i < text_len; ++i) {
if (i % width == 0) {
mv_nextline();
usleep(26000);
if (this->y == height__) {
break;
}
mv_right(1);
// (text[i] == ' ') ? printf("⎵") : printf("%c", text[i]);·
(text[i] == ' ') ? printf("%s·%s", theme.dimtext, theme.reset)
: printf("%s%c%s", theme.text, text[i], theme.reset);
} else {
if (text[i] == '\n') {
printf("%s↵%s", theme.text, theme.reset);
mv_nextline();
if (this->y == height__) {
break;
}
mv_right(1);
} else {
if (text[i] == ' ') {
// printf("⎵");
// printf("·");
printf("%s·%s", theme.dimtext, theme.reset);
} else {
printf("%s%c%s", theme.text, text[i], theme.reset);
}
}
}
}
mv_xy(x, y + 1); // Because printing will start from y+1 insted of y;
fflush(stdout);
}