-
Notifications
You must be signed in to change notification settings - Fork 0
/
NcursesEditorView.cpp
308 lines (240 loc) · 7.97 KB
/
NcursesEditorView.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
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
// NcursesEditorView.cpp
//
// ICS 45C Spring 2022
// Project #4: People Just Love to Play with Words
//
// DO NOT MODIFY THIS FILE
//
// There are some low-level gymnastics being done here, particularly in dealing
// with problems like resizing the terminal window and refreshing various areas
// of the view. The resizing functionality, in particular, required the use of
// signal handlers from the C Standard Library.
#include <csignal>
#include <sstream>
#include <string>
#include <ncurses.h>
#include "NcursesEditorView.hpp"
NcursesEditorView::NcursesEditorView(EditorModel& model)
: model{model}, topVisibleLine{1}, leftVisibleColumn{1}
{
}
namespace
{
NcursesEditorView* registeredView = nullptr;
void handleWindowSizeChange(int signum)
{
if (registeredView != nullptr)
{
endwin();
::refresh();
registeredView->refresh();
}
}
void registerSignalHandlers(NcursesEditorView* view)
{
registeredView = view;
std::signal(SIGWINCH, handleWindowSizeChange);
}
void unregisterSignalHandlers()
{
std::signal(SIGWINCH, SIG_DFL);
registeredView = nullptr;
}
}
void NcursesEditorView::start()
{
initscr();
raw();
noecho();
keypad(stdscr, 1);
registerSignalHandlers(this);
}
void NcursesEditorView::stop()
{
unregisterSignalHandlers();
echo();
endwin();
}
namespace
{
struct Coordinate
{
int x;
int y;
};
struct Area
{
Coordinate tlxy;
Coordinate brxy;
};
struct EditorAreas
{
Area lineNumberArea;
Area statusBarArea;
Area editorArea;
Area scrollArea;
};
EditorAreas determineEditorAreas(int lineCount)
{
int xsize;
int ysize;
getmaxyx(stdscr, ysize, xsize);
int lineNumberLength = std::to_string(lineCount).length();
EditorAreas areas;
areas.lineNumberArea.tlxy = {0, 0};
areas.lineNumberArea.brxy = {lineNumberLength + 2, ysize - 3};
areas.statusBarArea.tlxy = {lineNumberLength + 3, ysize - 2};
areas.statusBarArea.brxy = {xsize - 1, ysize - 1};
areas.editorArea.tlxy = {lineNumberLength + 3, 0};
areas.editorArea.brxy = {xsize - 1, ysize - 3};
areas.scrollArea.tlxy = {0, ysize - 2};
areas.scrollArea.brxy = {lineNumberLength + 2, ysize - 1};
return areas;
}
void redrawLineNumberArea(
const Area& lineNumberArea, const EditorModel& model,
int topVisibleLine)
{
int currentLine = topVisibleLine;
for (int y = lineNumberArea.tlxy.y; y <= lineNumberArea.brxy.y; ++y)
{
mvaddch(y, lineNumberArea.brxy.x, '|');
for (int x = lineNumberArea.tlxy.x; x <= lineNumberArea.brxy.x - 1; ++x)
{
mvaddch(y, x, ' ');
}
if (currentLine <= model.lineCount())
{
std::string lineNumber = std::to_string(currentLine);
mvaddstr(
y, lineNumberArea.brxy.x - 1 - lineNumber.length(),
lineNumber.c_str());
}
++currentLine;
}
}
void redrawStatusBar(const Area& statusBarArea, const EditorModel& model)
{
for (int y = statusBarArea.tlxy.y; y <= statusBarArea.brxy.y; ++y)
{
for (int x = statusBarArea.tlxy.x; x <= statusBarArea.brxy.x; ++x)
{
if (y == statusBarArea.tlxy.y)
{
mvaddch(y, x, '-');
}
else
{
mvaddch(y, x, ' ');
}
}
}
std::ostringstream pos;
pos << "Ln " << model.cursorLine() << " Col " << model.cursorColumn();
std::string posStr = pos.str();
mvaddstr(
statusBarArea.brxy.y, statusBarArea.brxy.x - posStr.length(),
posStr.c_str());
if (model.currentErrorMessage().length() > 0)
{
int maxErrorMessageWidth =
(statusBarArea.brxy.x - statusBarArea.tlxy.x + 1) - (posStr.length() + 4);
mvaddstr(
statusBarArea.brxy.y, statusBarArea.tlxy.x,
model.currentErrorMessage().substr(0, maxErrorMessageWidth).c_str());
}
}
void redrawScrollArea(const Area& scrollArea, int leftVisibleColumn)
{
for (int y = scrollArea.tlxy.y; y <= scrollArea.brxy.y; ++y)
{
if (y == scrollArea.brxy.y)
{
for (int x = scrollArea.tlxy.x; x <= scrollArea.brxy.x - 1; ++x)
{
mvaddch(y, x, leftVisibleColumn == 1 ? ' ' : '<');
}
mvaddch(y, scrollArea.brxy.x, ' ');
}
else
{
for (int x = scrollArea.tlxy.x; x <= scrollArea.brxy.x; ++x)
{
mvaddch(y, x, ' ');
}
}
}
}
void forceCursorVisible(
const Area& editorArea, const EditorModel& model,
int& topVisibleLine, int& leftVisibleColumn)
{
int bottomVisibleLine = topVisibleLine + (editorArea.brxy.y - editorArea.tlxy.y);
if (model.cursorLine() < topVisibleLine)
{
topVisibleLine = model.cursorLine();
}
else if (model.cursorLine() > bottomVisibleLine)
{
topVisibleLine = model.cursorLine() - (editorArea.brxy.y - editorArea.tlxy.y);
}
int rightVisibleColumn = leftVisibleColumn + (editorArea.brxy.x - editorArea.tlxy.x);
if (model.cursorColumn() < leftVisibleColumn)
{
leftVisibleColumn = model.cursorColumn();
}
else if (model.cursorColumn() > rightVisibleColumn)
{
leftVisibleColumn = model.cursorColumn() - (editorArea.brxy.x - editorArea.tlxy.x);
}
}
void placeText(
const Area& editorArea, const EditorModel& model,
int& topVisibleLine, int& leftVisibleColumn)
{
int currentLine = topVisibleLine;
for (int y = editorArea.tlxy.y; y <= editorArea.brxy.y; ++y)
{
if (currentLine <= model.lineCount())
{
int visibleColumns = editorArea.brxy.x - editorArea.tlxy.x + 1;
const std::string& lineText = model.line(currentLine);
std::string displayText = leftVisibleColumn <= lineText.length()
? lineText.substr(leftVisibleColumn - 1, visibleColumns)
: "";
mvaddstr(y, editorArea.tlxy.x, displayText.c_str());
for (int x = editorArea.tlxy.x + displayText.length(); x <= editorArea.brxy.x; ++x)
{
mvaddch(y, x, ' ');
}
}
else
{
for (int x = editorArea.tlxy.x; x <= editorArea.brxy.x; ++x)
{
mvaddch(y, x, ' ');
}
}
++currentLine;
}
}
void placeCursor(
const Area& editorArea, const EditorModel& model,
int topVisibleLine, int leftVisibleColumn)
{
int cursorRelativeLine = model.cursorLine() - topVisibleLine;
int cursorRelativeColumn = model.cursorColumn() - leftVisibleColumn;
move(cursorRelativeLine + editorArea.tlxy.y, cursorRelativeColumn + editorArea.tlxy.x);
}
}
void NcursesEditorView::refresh()
{
EditorAreas areas = determineEditorAreas(model.lineCount());
forceCursorVisible(areas.editorArea, model, topVisibleLine, leftVisibleColumn);
placeText(areas.editorArea, model, topVisibleLine, leftVisibleColumn);
redrawLineNumberArea(areas.lineNumberArea, model, topVisibleLine);
redrawStatusBar(areas.statusBarArea, model);
redrawScrollArea(areas.scrollArea, leftVisibleColumn);
placeCursor(areas.editorArea, model, topVisibleLine, leftVisibleColumn);
::refresh();
}