-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
145 lines (126 loc) · 2.64 KB
/
main.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
#include <cstdlib>
#include <iostream>
#include <cstdio>
#include <fstream>
#include <cstring>
#include <string>
#include <unistd.h>
#include "Line.h"
#include "LinkedNode.h"
#include "LinkedList.h"
#include "Editor.h"
using namespace std;
void usage()
{
cerr << "Usage:\n\r";
cerr << "\t./hw1 [--record \x1b[4mfilename\x1b[0m|--replay \x1b[4mfilename\x1b[0m]\n\r";
}
void print(Editor &editor)
{
// clear screen
cout << "\x1b[H\x1b[2J";
int ci = editor.getCursorRow();
int cj = editor.getCursorCol();
int sz = editor.getLength();
for (int i = 0; i < sz; ++i) {
cout << editor.getLine(i);
//new line
cout << "\n\r";
}
// move cursor to [ci;cj]
cout << "\x1b[" << ci+1 << ";" << cj+1 << "H";
}
int parse(Editor &edi, ostream &out, istream &in)
{
char c;
in.get(c);
out << c;
switch (c) {
case -1: // EOF
case 3: // CTRL + C
return 0;
case 26: // CTRL + Z
edi.undo();
break;
case 13: // ENTER
edi.insertLine();
//cout << curr;
break;
case 127: // BACKSPACE
edi.backspace();
break;
case '\x1b':
in.get(c);
out << c;
in.get(c);
out << c;
switch (c) {
case 51: // DEL
edi.del();
in.get(c);
out << c;
break;
case 65: // UP ARROW
edi.moveCursorUp();
break;
case 66: // DOWN ARROW
edi.moveCursorDown();
break;
case 67: // RIGHT ARROW
edi.moveCursorRight();
break;
case 68: // LEFT ARROW
edi.moveCursorLeft();
break;
}
break;
default:
edi.insertChar(c);
}
return 1;
}
void loop(ostream &out, istream &in, float sleep_sec)
{
cout << "\x1b[?1049h" << endl << endl;
Editor edi;
while (true) {
print(edi);
if (!parse(edi, out, in))
break;
usleep(sleep_sec * 1e6);
}
cout << "\x1b[?1049l";
}
int main(int argc, char *argv[])
{
system("stty raw < /dev/tty");
system("stty -echo < /dev/tty");
setbuf(stdout, NULL);
if (argc == 1) {
loop(cout, cin, 0);
} else if (argc == 3) {
if (strcmp(argv[1], "--record") == 0) {
ofstream file(argv[2]);
if (file.is_open()) {
loop(file, cin, 0);
file.close();
} else {
cerr << "Output file " << string(argv[2]) << " could not be opened.\n\r";
}
} else if (strcmp(argv[1], "--replay") == 0) {
ifstream in(argv[2]);
if (in.is_open()) {
loop(cout, in, 0.5);
in.close();
} else {
cerr << "Input file " << string(argv[2]) << " could not be opened.\n\r";
}
} else {
cerr << "Second argument should be either '--record' or '--replay'. However, '" << string(argv[1]) <<"' is found.\n\r";
}
} else {
usage();
}
system("stty cooked < /dev/tty");
system("stty echo < /dev/tty");
}