-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
176 lines (155 loc) · 3.57 KB
/
main.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
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
165
166
167
168
169
170
171
172
173
174
175
176
#include <ncurses.h>
#include <stdio.h>
#include <stdlib.h>
#define ESCAPE 033
#define BACKSPACE 074 // <
#define ENTER 044 // $
#define BACKLINE 0136 // ^
struct letter {
size_t index;
char content;
char pad[7];
};
void backspace(char *src, struct letter *dest, size_t srclen) {
int y, x;
getyx(stdscr, y, x);
char tbrestored;
tbrestored = mvinch(y, x - 1) & A_CHARTEXT;
for (size_t i = 0; i < srclen; i++) {
if (tbrestored == dest[i].content) {
src[dest[i].index] = dest[i].content;
dest[i].content = 0;
dest[i].index = 0;
move(0, 0);
printw("%s", src);
move(y, x);
mvaddch(y, x - 1, '*');
move(y, x - 1);
refresh();
break;
}
}
refresh();
}
void enter(size_t *al, int *linepos, char **src, int row) {
int y, x;
getyx(stdscr, y, x);
if (y < row - 1) {
(*al)++;
move(0, 0);
printw("%s", src[y]);
move(y + 1, linepos[y]);
refresh();
linepos[y - 1] = x;
}
}
void backline(size_t *al, int *linepos, char **src) {
int y, x;
getyx(stdscr, y, x);
if (y > 1) {
(*al)--;
move(0, 0);
printw("%s", src[y - 2]);
move(y - 1, linepos[y - 2]);
refresh();
linepos[y - 1] = x;
}
}
void type(char ch, char *src, struct letter *dest, size_t srclen) {
if (ch == '.') { // forbidding the usage of the char '.'
return; // it is used as placeholder in the first line.
}
int y, x;
for (size_t i = 0; i < srclen; i++) {
if (ch == src[i]) {
addch(ch);
src[i] = '.';
int y, x;
getyx(stdscr, y, x);
move(0, 0);
printw("%s", src);
move(y, x);
dest[i].content = ch;
dest[i].index = i;
refresh();
getyx(stdscr, y, x);
break;
}
}
refresh();
}
int main(void) {
size_t line_length = 196;
char line[line_length + 2];
printf("type the line you want to variate: ");
fgets(line, line_length + 2, stdin);
size_t real_length = 0;
for (size_t i = 0; i < line_length + 2; i++) {
if (line[i] != '\n') {
real_length++;
} else {
break;
}
}
int last_key = ENTER;
initscr();
raw();
noecho();
unsigned int row, col;
getmaxyx(stdscr, row, col);
char *source[row - 1];
for (size_t i = 0; i < row - 1; i++) {
source[i] = malloc((real_length + 1) * sizeof(char));
}
for (size_t i = 0; i < row - 1; i++) {
for (size_t k = 0; k < real_length; k++) {
source[i][k] = line[k];
}
}
for (size_t i = 0; i < row - 1; i++) {
source[i][real_length] = 0;
}
struct letter(*page[row - 1])[real_length];
for (size_t i = 0; i < row - 1; i++) {
page[i] = malloc(sizeof(struct letter[real_length]));
}
for (size_t i = 0; i < row - 1; i++) {
for (size_t k = 0; k < real_length; k++) {
(*page[i])[k].index = 0;
(*page[i])[k].content = 0;
}
}
int linepos[row - 1];
for (size_t i = 0; i < row - 1; i++) {
linepos[i] = 0;
}
printw("%s\n", source[0]);
int y, x;
getyx(stdscr, y, x);
size_t activeline = 0;
while (last_key != ESCAPE) {
last_key = getch();
switch (last_key) {
case ESCAPE:
endwin();
break;
case BACKSPACE:
backspace(source[activeline], *page[activeline], real_length);
break;
case ENTER:
enter(&activeline, linepos, source, row);
break;
case BACKLINE:
backline(&activeline, linepos, source);
break;
default:
type(last_key, source[activeline], *page[activeline], real_length);
break;
}
}
for (size_t i = 0; i < row - 1; i++) {
free(source[i]);
free(page[i]);
}
return EXIT_SUCCESS;
}