-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGrid.cpp
More file actions
305 lines (260 loc) · 9.62 KB
/
Copy pathGrid.cpp
File metadata and controls
305 lines (260 loc) · 9.62 KB
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
#include "Grid.hpp"
Grid::Grid(int cols, int rows) : m_cols(cols), m_rows(rows) {
if (m_cols < 1) m_cols = 1;
if (m_rows < 1) m_rows = 1;
m_cells.resize(m_cols * m_rows);
m_row_wrapped.resize(m_rows, false);
}
void Grid::resize(int cols, int rows, int& cursor_x, int& cursor_y) {
if (cols < 1) cols = 1;
if (rows < 1) rows = 1;
if (cols == m_cols && rows == m_rows) return;
// Extract logical lines
std::vector<std::vector<Cell>> logical_lines;
std::vector<Cell> current_line;
int logical_line_idx = 0;
int current_logical_len = 0;
int c_logic_line = -1;
int c_logic_offset = 0;
for (const auto& hline : m_history) {
logical_lines.push_back(hline.first);
// Wait, m_history stores physical lines! We need to extract logical lines from m_history too.
}
// But since m_history stores physical lines just like m_cells, let's treat them all as a single contiguous array of rows!
int total_old_rows = m_history.size() + m_rows;
for (int y = 0; y < total_old_rows; ++y) {
bool is_history = (y < (int)m_history.size());
int local_y = is_history ? y : (y - m_history.size());
int last_char_col = m_cols - 1;
while (last_char_col >= 0) {
char c = is_history ? m_history[local_y].first[last_char_col].character
: m_cells[local_y * m_cols + last_char_col].character;
if (c != ' ' && c != 0) break;
last_char_col--;
}
bool wrapped = is_history ? m_history[local_y].second : m_row_wrapped[local_y];
int copy_len = wrapped ? m_cols : (last_char_col + 1);
if (!is_history && local_y == cursor_y) {
c_logic_line = logical_line_idx;
c_logic_offset = current_logical_len + cursor_x;
}
current_logical_len += copy_len;
for (int x = 0; x < copy_len; ++x) {
current_line.push_back(is_history ? m_history[local_y].first[x]
: m_cells[local_y * m_cols + x]);
}
if (!wrapped) {
logical_lines.push_back(current_line);
current_line.clear();
logical_line_idx++;
current_logical_len = 0;
}
}
// Strip trailing empty logical lines after the cursor
// This prevents empty space at the bottom of the screen from forcing text into history
while (logical_lines.size() > (size_t)(c_logic_line + 1)) {
if (logical_lines.back().empty()) {
logical_lines.pop_back();
} else {
break;
}
}
if (!current_line.empty()) {
logical_lines.push_back(current_line);
if (c_logic_line == -1 && cursor_y >= m_rows) {
c_logic_line = logical_line_idx;
c_logic_offset = cursor_x;
}
} else if (c_logic_line == -1) {
c_logic_line = logical_line_idx;
c_logic_offset = cursor_x;
}
// Reflow into new grid and new history
std::deque<std::pair<std::vector<Cell>, bool>> new_history;
std::vector<Cell> new_cells(cols * rows);
std::vector<bool> new_wrapped(rows, false);
int new_y = 0;
int new_c_x = 0;
int new_c_y = 0;
int current_logic_line_idx = 0;
// Calculate total new lines
int total_new_lines = 0;
for (const auto& line : logical_lines) {
int len = line.size();
if (len == 0) total_new_lines++;
else total_new_lines += (len + cols - 1) / cols;
}
// We only keep the last `rows` lines in new_cells, the rest goes to new_history
int lines_to_history = total_new_lines - rows;
if (lines_to_history < 0) lines_to_history = 0;
// We try to keep the bottom aligned, so we might need to skip top logical lines if they don't fit.
// A better approach is to reflow from the top, and if it exceeds rows, we scroll up later.
// For simplicity, we just reflow from top to bottom.
for (const auto& line : logical_lines) {
int chars_written = 0;
int line_len = line.size();
if (line_len == 0) {
if (current_logic_line_idx == c_logic_line) {
new_c_y = new_y - lines_to_history;
new_c_x = c_logic_offset;
}
if (new_y < lines_to_history) {
new_history.push_back({std::vector<Cell>(cols), false});
} else {
int ty = new_y - lines_to_history;
new_wrapped[ty] = false;
}
new_y++;
current_logic_line_idx++;
continue;
}
while (chars_written < line_len) {
int chunk = std::min(cols, line_len - chars_written);
bool wrapped = (chars_written + chunk < line_len);
if (current_logic_line_idx == c_logic_line) {
if (c_logic_offset >= chars_written && c_logic_offset < chars_written + chunk) {
new_c_y = new_y - lines_to_history;
new_c_x = c_logic_offset - chars_written;
} else if (c_logic_offset >= line_len && chars_written + chunk == line_len) {
new_c_y = new_y - lines_to_history;
new_c_x = c_logic_offset - chars_written;
}
}
if (new_y < lines_to_history) {
std::vector<Cell> hline(cols);
for (int x = 0; x < chunk; ++x) {
hline[x] = line[chars_written + x];
}
new_history.push_back({hline, wrapped});
} else {
int ty = new_y - lines_to_history;
if (ty < rows) {
for (int x = 0; x < chunk; ++x) {
new_cells[ty * cols + x] = line[chars_written + x];
}
new_wrapped[ty] = wrapped;
}
}
chars_written += chunk;
new_y++;
}
current_logic_line_idx++;
}
if (c_logic_line >= current_logic_line_idx) {
new_c_y = new_y - lines_to_history;
new_c_x = c_logic_offset;
}
if (new_history.size() > (size_t)m_max_history) {
int excess = new_history.size() - m_max_history;
for (int i=0; i<excess; i++) new_history.pop_front();
}
if (new_c_y < 0) new_c_y = 0;
if (new_c_x < 0) new_c_x = 0;
if (new_c_y >= rows) new_c_y = rows - 1;
if (new_c_x >= cols) new_c_x = cols - 1;
cursor_x = new_c_x;
cursor_y = new_c_y;
m_cols = cols;
m_rows = rows;
m_cells = std::move(new_cells);
m_row_wrapped = std::move(new_wrapped);
m_history = std::move(new_history);
if (m_scroll_offset > (int)m_history.size()) {
m_scroll_offset = m_history.size();
}
}
Cell& Grid::getCell(int x, int y) {
if (x < 0) x = 0;
if (y < 0) y = 0;
if (x >= m_cols) x = m_cols - 1;
if (y >= m_rows) y = m_rows - 1;
return m_cells[y * m_cols + x];
}
const Cell& Grid::getCell(int x, int y) const {
if (x < 0) x = 0;
if (y < 0) y = 0;
if (x >= m_cols) x = m_cols - 1;
if (y >= m_rows) y = m_rows - 1;
return m_cells[y * m_cols + x];
}
const Cell& Grid::getVisibleCell(int x, int y) const {
if (x < 0) x = 0;
if (x >= m_cols) x = m_cols - 1;
int eff_y = y - m_scroll_offset;
if (eff_y < 0) {
int hist_idx = m_history.size() + eff_y;
if (hist_idx < 0) hist_idx = 0;
if (hist_idx >= (int)m_history.size()) hist_idx = m_history.size() - 1;
return m_history[hist_idx].first[x];
} else {
if (eff_y >= m_rows) eff_y = m_rows - 1;
return m_cells[eff_y * m_cols + x];
}
}
void Grid::clear() {
for (auto& cell : m_cells) {
cell = Cell(); // Reset to default
}
for (int y = 0; y < m_rows; ++y) {
m_row_wrapped[y] = false;
}
m_history.clear();
m_scroll_offset = 0;
}
void Grid::scrollUp() {
// Push top row to history
std::vector<Cell> top_row(m_cols);
for (int x = 0; x < m_cols; ++x) {
top_row[x] = m_cells[x];
}
m_history.push_back({top_row, m_row_wrapped[0]});
if (m_history.size() > (size_t)m_max_history) {
m_history.pop_front();
}
if (m_scroll_offset > 0) {
m_scroll_offset++;
if (m_scroll_offset > (int)m_history.size()) {
m_scroll_offset = m_history.size();
}
}
// Move all rows up by 1
for (int y = 1; y < m_rows; ++y) {
for (int x = 0; x < m_cols; ++x) {
m_cells[(y - 1) * m_cols + x] = m_cells[y * m_cols + x];
}
m_row_wrapped[y - 1] = m_row_wrapped[y];
}
// Clear the bottom row
for (int x = 0; x < m_cols; ++x) {
m_cells[(m_rows - 1) * m_cols + x] = Cell();
}
m_row_wrapped[m_rows - 1] = false;
}
void Grid::scrollDown() {
// Move all rows down by 1
for (int y = m_rows - 2; y >= 0; --y) {
for (int x = 0; x < m_cols; ++x) {
m_cells[(y + 1) * m_cols + x] = m_cells[y * m_cols + x];
}
m_row_wrapped[y + 1] = m_row_wrapped[y];
}
// Clear the top row
for (int x = 0; x < m_cols; ++x) {
m_cells[x] = Cell();
}
m_row_wrapped[0] = false;
}
bool Grid::isWrapped(int y) const {
if (y < 0 || y >= m_rows) return false;
return m_row_wrapped[y];
}
void Grid::setWrapped(int y, bool wrapped) {
if (y >= 0 && y < m_rows) {
m_row_wrapped[y] = wrapped;
}
}
void Grid::setScrollOffset(int offset) {
if (offset < 0) offset = 0;
if (offset > (int)m_history.size()) offset = m_history.size();
m_scroll_offset = offset;
}