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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
|
#include "editor.h"
#include "utils.h"
#include <fstream>
#include <limits>
#include <iostream>
Editor::Editor(std::vector<std::string>* contents, const std::string& filename):
m_current_line(0), m_filename(filename), m_stopped(false) {
m_contents = contents;
}
int Editor::run_editor() {
std::vector<std::string>* c = m_contents;
while (!m_stopped) {
print_contents_on_line(m_current_line);
char input = Utils::getch();
switch (input) {
case 'e':
edit_command();
break;
case 'm':
move_to_command();
break;
case 'w':
write_command();
break;
case 'q':
quit_command();
break;
case 'd':
delete_and_clamp_command();
break;
case 'a':
append_after_command();
break;
case 'b':
prepend_before_command();
break;
default:
std::cout << "Not a recognized command " << input << ".\n";
break;
}
}
return 0;
}
int Editor::print_contents_on_line(int line) const {
int output_length = m_contents->size();
bool truncated = false;
if ((int) m_contents->size() > line + OUTPUT_LINE_MAX) {
output_length = line + OUTPUT_LINE_MAX;
truncated = true;
}
for (int i = line; i < output_length; i++) {
std::cout << i << " " << m_contents->at(i) << '\n';
}
if (truncated) {
std::cout << "...\n";
}
return 0;
}
int Editor::edit_command() {
std::cout << "Edit which line? ";
int line;
std::cin >> line;
if (std::cin.fail() || line < 0) {
std::cout << "Not a valid line number!\n";
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(),'\n');
return 1;
}
return edit_line(line);
}
int Editor::edit_line(int line) {
if (line >= m_contents->size()) {
m_contents->resize(line + 1, "");
}
std::cout << "> ";
std::string input;
std::cin.ignore(std::numeric_limits<std::streamsize>::max(),'\n');
std::getline(std::cin, input);
std::cin.clear();
(*m_contents)[line] = input;
return 0;
}
int Editor::move_to_command() {
std::cout << "Move to which line? ";
int line;
std::cin >> line;
if (std::cin.fail() || line < 0 || line >= m_contents->size()) {
std::cout << "Not a valid line number!\n";
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(),'\n');
return 1;
}
return move_to_line(line);
}
int Editor::move_to_line(int line) {
m_current_line = line;
return 0;
}
int Editor::write_command() {
std::cout << "Write to file? [y/n] ";
char answer = Utils::getch();
if (answer == 'y') {
return write();
}
return 0;
}
int Editor::write() {
std::ofstream file;
file.open(m_filename);
if (file.fail()) {
std::cout << "Can't open file " << m_filename << " for writing!";
return 1;
}
for (int i = 0; i < m_contents->size(); i++) {
file << m_contents->at(i) << '\n';
}
file.close();
return 0;
}
int Editor::quit_command() {
std::cout << "Really quit? [y/n] ";
char answer = Utils::getch();
if (answer == 'y') {
m_stopped = true;
std::cout << "\n";
}
return 0;
}
int Editor::delete_and_clamp_command() {
std::cout << "Delete and clamp which line? ";
int line;
std::cin >> line;
if (std::cin.fail() || line < 0 || line >= m_contents->size()) {
std::cout << "Not a valid line number!\n";
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(),'\n');
return 1;
}
return delete_and_clamp(line);
}
int Editor::delete_and_clamp(int line) {
m_contents->erase(m_contents->begin() + line);
return 0;
}
int Editor::append_after_command() {
std::cout << "Append after which line? ";
int line;
std::cin >> line;
if (std::cin.fail() || line < 0 || line >= m_contents->size()) {
std::cout << "Not a valid line number!\n";
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(),'\n');
return 1;
}
return append_after_line(line);
}
int Editor::append_after_line(int line) {
m_contents->insert(m_contents->begin() + line + 1, "");
return edit_line(line + 1);
}
int Editor::prepend_before_command() {
std::cout << "Prepend before which line? ";
int line;
std::cin >> line;
if (std::cin.fail() || line < 0 || line >= m_contents->size()) {
std::cout << "Not a valid line number!\n";
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(),'\n');
return 1;
}
return prepend_before_line(line);
}
int Editor::prepend_before_line(int line) {
m_contents->insert(m_contents->begin() + line, "");
return edit_line(line);
}
|