blob: 85289b698f11b9ef3a6d8ac9f521199bba87d321 (
plain)
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
|
#define MAX_ARGS 16
#define pos(x, y) (x) + (y) * window_w
struct ArgList {
int len, cap;
int *args;
ArgList(int cap) : len(0), cap(cap) {
args = new int[cap];
for (int i = 0; i < cap; args[i++] = 0);
}
~ArgList() { delete[] args; }
int pop() {
return args[--len];
}
void push(int value) {
args[len++] = value;
}
};
struct Client {
int sockfd;
Point cursor;
Point window_start;
ArgList args;
Client(const Buffer &b) :
cursor(b.storage, 0),
window_start(cursor),
args(MAX_ARGS)
{}
void show() {
int window_h = args.pop();
int window_w = args.pop();
char *view = new char[window_w * window_h];
Point window_end(window_start);
for (int i = 0; i < window_h; i++) {
for (int j = 0; j < window_w; j++) {
if (window_end == cursor) {
view[pos(j, i)] = '$';
} else {
view[pos(j, i)] = window_end.element();
}
if (window_end.element() == '\n') {
for (int k = j + 1; k < window_w; k++) {
view[pos(k, i)] = 0;
}
j = window_w;
} else if (window_end.element() == '\t') {
for (int k = j + 1; k < j + 8; k++) {
view[pos(k, i)] = 0;
}
j = j + 7;
}
window_end++;
}
}
write(sockfd, view, window_w * window_h);
delete[] view;
}
void move() {
int target = args.pop();
while (target > 0) {
cursor++;
target--;
}
while (target < 0) {
cursor--;
target++;
}
}
void push() {
cursor.push(args.pop());
}
void pop() {
cursor.pop();
}
};
|