-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGridManager.cs
More file actions
417 lines (361 loc) · 13.3 KB
/
Copy pathGridManager.cs
File metadata and controls
417 lines (361 loc) · 13.3 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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
namespace ExcelConsole;
public class GridManager
{
private readonly string[,] _data;
private readonly bool[,] _isFile;
private readonly string[,] _filePath;
public int ColumnCount { get; }
public int RowCount { get; }
public bool IsDirty { get; private set; }
// ── Cursor state ─────────────────────────────────────────────────
private int _selectedRow;
private int _selectedCol;
public (int row, int col) GetCurrentCell() => (_selectedRow, _selectedCol);
public void SelectCell(int row, int col)
{
_selectedRow = Math.Clamp(row, 0, RowCount - 1);
_selectedCol = Math.Clamp(col, 0, ColumnCount - 1);
}
public void MoveUp() { if (_selectedRow > 0) _selectedRow--; }
public void MoveDown() { if (_selectedRow < RowCount - 1) _selectedRow++; }
public void MoveLeft() { if (_selectedCol > 0) _selectedCol--; }
public void MoveRight() { if (_selectedCol < ColumnCount - 1) _selectedCol++; }
public void ClampSelection()
{
_selectedRow = Math.Min(_selectedRow, RowCount - 1);
_selectedCol = Math.Min(_selectedCol, ColumnCount - 1);
}
public GridManager(int availableWidth, int availableHeight, int columnWidth = 20)
{
const int rowHeaderWidth = 4;
ColumnCount = Math.Max(1, (availableWidth - rowHeaderWidth) / columnWidth);
RowCount = Math.Max(1, availableHeight);
_data = new string[RowCount, ColumnCount];
_isFile = new bool[RowCount, ColumnCount];
_filePath = new string[RowCount, ColumnCount];
for (int r = 0; r < RowCount; r++)
for (int c = 0; c < ColumnCount; c++)
{
_data[r, c] = "";
_filePath[r, c] = "";
}
}
public static string GetColumnName(int index)
{
string name = "";
index++;
while (index > 0)
{
index--;
name = (char)('A' + index % 26) + name;
index /= 26;
}
return name;
}
public string GetCellReference(int row, int col)
{
return $"{GetColumnName(col)}{row + 1}";
}
public string GetCellValue(int row, int col)
{
if (row >= 0 && row < RowCount && col >= 0 && col < ColumnCount)
return _data[row, col];
return "";
}
public string GetSelectedCellValue() => GetCellValue(_selectedRow, _selectedCol);
public void SetSelectedCellValue(string value) => SetCellValue(_selectedRow, _selectedCol, value);
public void AppendToSelectedCell(char ch) { var cur = GetSelectedCellValue(); SetSelectedCellValue(cur + ch); }
public void SetCellValue(int row, int col, string value)
{
if (row >= 0 && row < RowCount && col >= 0 && col < ColumnCount)
{
_data[row, col] = value;
IsDirty = true;
}
}
public void SetFileEntry(int row, int col, string displayName, string fullPath)
{
if (row < 0 || row >= RowCount || col < 0 || col >= ColumnCount) return;
_data[row, col] = displayName;
_isFile[row, col] = true;
_filePath[row, col] = fullPath;
}
public bool IsFileEntry(int row, int col)
{
if (row < 0 || row >= RowCount || col < 0 || col >= ColumnCount) return false;
return _isFile[row, col];
}
public string GetFilePath(int row, int col)
{
if (row < 0 || row >= RowCount || col < 0 || col >= ColumnCount) return "";
return _filePath[row, col];
}
public void ClearRow(int row)
{
if (row < 0 || row >= RowCount) return;
for (int c = 0; c < ColumnCount; c++)
_data[row, c] = "";
IsDirty = true;
}
public void DeleteRow(int row)
{
if (row < 0 || row >= RowCount) return;
for (int r = row; r < RowCount - 1; r++)
for (int c = 0; c < ColumnCount; c++)
_data[r, c] = _data[r + 1, c];
for (int c = 0; c < ColumnCount; c++)
_data[RowCount - 1, c] = "";
IsDirty = true;
}
public void DeleteSelectedRow()
{
DeleteRow(_selectedRow);
ClampSelection();
}
public void ShiftRowsDown(int fromRow)
{
if (fromRow < 0 || fromRow >= RowCount) return;
for (int r = RowCount - 1; r > fromRow; r--)
for (int c = 0; c < ColumnCount; c++)
_data[r, c] = _data[r - 1, c];
for (int c = 0; c < ColumnCount; c++)
_data[fromRow, c] = "";
IsDirty = true;
}
public void ShiftRowsUp(int fromRow)
{
if (fromRow < 0 || fromRow >= RowCount) return;
for (int r = fromRow; r < RowCount - 1; r++)
for (int c = 0; c < ColumnCount; c++)
_data[r, c] = _data[r + 1, c];
for (int c = 0; c < ColumnCount; c++)
_data[RowCount - 1, c] = "";
IsDirty = true;
}
public void ShiftSelectedRowDown() => ShiftRowsDown(_selectedRow);
public void ShiftSelectedRowUp() => ShiftRowsUp(_selectedRow);
public double? GetColumnSum(int col)
{
if (col < 0 || col >= ColumnCount)
return null;
double sum = 0;
bool hasNumber = false;
for (int r = 0; r < RowCount; r++)
{
if (double.TryParse(_data[r, col], System.Globalization.NumberStyles.Any,
System.Globalization.CultureInfo.InvariantCulture, out double num))
{
sum += num;
hasNumber = true;
}
}
return hasNumber ? sum : null;
}
public double? GetRowProduct(int row)
{
if (row < 0 || row >= RowCount)
return null;
double product = 1;
bool hasNumber = false;
for (int c = 0; c < ColumnCount; c++)
{
if (double.TryParse(_data[row, c], System.Globalization.NumberStyles.Any,
System.Globalization.CultureInfo.InvariantCulture, out double num))
{
product *= num;
hasNumber = true;
}
}
return hasNumber ? product : null;
}
public void SaveToCsv(string path)
{
// Read existing file to preserve rows/columns beyond the grid bounds
List<List<string>>? existingRows = null;
int existingRowCount = 0;
int existingMaxCols = 0;
if (File.Exists(path))
{
try
{
var lines = File.ReadAllLines(path);
existingRows = new List<List<string>>(lines.Length);
foreach (var line in lines)
{
var parsed = ParseCsvLine(line);
existingRows.Add(parsed);
if (parsed.Count > existingMaxCols)
existingMaxCols = parsed.Count;
}
existingRowCount = existingRows.Count;
}
catch { /* If we can't read, just save what we have */ }
}
int totalRows = Math.Max(RowCount, existingRowCount);
int totalCols = Math.Max(ColumnCount, existingMaxCols);
using var writer = new StreamWriter(path);
for (int r = 0; r < totalRows; r++)
{
var fields = new string[totalCols];
for (int c = 0; c < totalCols; c++)
{
if (r < RowCount && c < ColumnCount)
{
// Within grid bounds: use grid data
fields[c] = EscapeCsvField(_isFile[r, c] ? "" : _data[r, c]);
}
else if (existingRows != null && r < existingRows.Count && c < existingRows[r].Count)
{
// Beyond grid bounds: preserve existing file data
fields[c] = EscapeCsvField(existingRows[r][c]);
}
else
{
fields[c] = "";
}
}
writer.WriteLine(string.Join(",", fields));
}
IsDirty = false;
}
public void LoadFromCsv(string path)
{
if (!File.Exists(path)) return;
var lines = File.ReadAllLines(path);
for (int r = 0; r < Math.Min(lines.Length, RowCount); r++)
{
var fields = ParseCsvLine(lines[r]);
for (int c = 0; c < Math.Min(fields.Count, ColumnCount); c++)
_data[r, c] = fields[c];
}
}
/// <summary>
/// Re-reads the CSV and merges external changes into the grid.
/// Empty local cells are overwritten. Conflicts produce a "c: " marker.
/// File-entry cells are skipped.
/// </summary>
public bool MergeFromCsv(string path)
{
if (!File.Exists(path)) return false;
string[] lines;
try { lines = File.ReadAllLines(path); }
catch { return false; }
bool changed = false;
for (int r = 0; r < Math.Min(lines.Length, RowCount); r++)
{
var fields = ParseCsvLine(lines[r]);
for (int c = 0; c < Math.Min(fields.Count, ColumnCount); c++)
{
if (_isFile[r, c]) continue;
string external = fields[c];
string local = _data[r, c];
if (local == external) continue;
if (string.IsNullOrEmpty(local))
{
_data[r, c] = external;
changed = true;
}
else if (!string.IsNullOrEmpty(external))
{
_data[r, c] = $"c: {external}({local})";
changed = true;
}
}
}
return changed;
}
private static string EscapeCsvField(string field)
{
if (field.Contains(',') || field.Contains('"') || field.Contains('\n'))
return "\"" + field.Replace("\"", "\"\"") + "\"";
return field;
}
private static List<string> ParseCsvLine(string line)
{
var fields = new List<string>();
int i = 0;
while (i <= line.Length)
{
if (i == line.Length) { fields.Add(""); break; }
if (line[i] == '"')
{
// Quoted field
i++;
var field = new System.Text.StringBuilder();
while (i < line.Length)
{
if (line[i] == '"')
{
if (i + 1 < line.Length && line[i + 1] == '"')
{
field.Append('"');
i += 2;
}
else
{
i++; // closing quote
break;
}
}
else
{
field.Append(line[i]);
i++;
}
}
fields.Add(field.ToString());
if (i < line.Length && line[i] == ',') i++; // skip comma
}
else
{
// Unquoted field
int start = i;
while (i < line.Length && line[i] != ',') i++;
fields.Add(line[start..i]);
if (i < line.Length) i++; // skip comma
}
}
return fields;
}
// ── Inline resolution ────────────────────────────────────────────
/// <summary>
/// Resolves an inline reference chain starting from (row, col).
/// Returns the final resolved cell value, or null if the cell isn't an inline ref.
/// Uses a visited set for cycle detection.
/// </summary>
public string? ResolveInline(int row, int col, HashSet<(int, int)>? visited = null)
{
string value = GetCellValue(row, col);
if (!CellPrefix.IsInline(value)) return null;
visited ??= new HashSet<(int, int)>();
if (!visited.Add((row, col))) return "[circular]";
if (visited.Count > 7) return "[too deep]";
// Expand {A1::C10} refs in the i: value before parsing the target cell
string expanded = CellPrefix.ExpandCellReferences(value, this);
var target = CellPrefix.ParseInlineRef(expanded);
if (target == null) return "[invalid ref]";
int tr = target.Value.row, tc = target.Value.col;
if (tr < 0 || tr >= RowCount || tc < 0 || tc >= ColumnCount)
return "[out of bounds]";
string targetValue = GetCellValue(tr, tc);
// If target is also inline, resolve recursively
if (CellPrefix.IsInline(targetValue))
return ResolveInline(tr, tc, visited);
return targetValue;
}
public string? ResolveSelectedInline(HashSet<(int, int)>? visited = null)
=> ResolveInline(_selectedRow, _selectedCol, visited);
/// <summary>
/// Gets the display value for a cell, resolving inline refs if applicable.
/// </summary>
public string GetDisplayValue(int row, int col, HashSet<(int, int)>? visited = null)
{
string raw = GetCellValue(row, col);
if (CellPrefix.IsInline(raw))
{
visited ??= new HashSet<(int, int)>();
return ResolveInline(row, col, visited) ?? raw;
}
return raw;
}
}