-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTextUtils.cs
More file actions
354 lines (331 loc) · 12.1 KB
/
TextUtils.cs
File metadata and controls
354 lines (331 loc) · 12.1 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
using System.Globalization;
using System.Text;
using System.Text.Json;
using System.Text.RegularExpressions;
static class TextUtils
{
public static string StripMarkup(string s)
{
// Preserve escaped brackets
s = s.Replace("[[", "\x01").Replace("]]", "\x02");
// Strip all markup tags
s = Regex.Replace(s, @"\[[^\[\]]*\]", "");
// Restore escaped brackets to their visible form
s = s.Replace("\x01", "[").Replace("\x02", "]");
return s;
}
public static string GetVisibleText(string s) => StripMarkup(s);
public static string[] SplitLines(string s) => s.Split(["\r\n", "\r", "\n"], StringSplitOptions.None);
public static bool IsWideEmojiInBMP(int value) => value switch
{
0x231A or 0x231B => true, // ⌚⌛
0x23E9 or 0x23EA or 0x23EB or 0x23EC or 0x23F0 or 0x23F3 => true,
>= 0x25AA and <= 0x25AB => true,
0x25B6 or 0x25C0 => true,
>= 0x25FB and <= 0x25FE => true,
>= 0x2600 and <= 0x2604 => true,
0x260E or 0x2611 => true,
>= 0x2614 and <= 0x2615 => true,
0x2618 or 0x261D or 0x2620 => true,
>= 0x2622 and <= 0x2623 => true,
0x2626 or 0x262A or 0x262E or 0x262F => true,
>= 0x2638 and <= 0x263A => true,
0x2640 or 0x2642 => true,
>= 0x2648 and <= 0x2653 => true, // zodiac
0x265F or 0x2660 or 0x2663 or 0x2665 or 0x2666 => true,
0x2668 or 0x267B or 0x267E or 0x267F => true,
>= 0x2692 and <= 0x2697 => true,
0x2699 or 0x269B or 0x269C => true,
>= 0x26A0 and <= 0x26A1 => true,
>= 0x26AA and <= 0x26AB => true,
>= 0x26B0 and <= 0x26B1 => true,
>= 0x26BD and <= 0x26BE => true,
>= 0x26C4 and <= 0x26C5 => true,
0x26C8 or 0x26CE or 0x26CF => true,
0x26D1 or 0x26D3 or 0x26D4 => true,
0x26E9 or 0x26EA => true,
>= 0x26F0 and <= 0x26F5 => true,
>= 0x26F7 and <= 0x26FA => true,
0x26FD => true,
0x2702 or 0x2705 => true,
>= 0x2708 and <= 0x270D => true,
0x270F => true,
0x2712 or 0x2714 or 0x2716 => true,
0x271D or 0x2721 => true,
0x2728 => true,
0x2733 or 0x2734 => true,
0x2744 or 0x2747 => true,
0x274C or 0x274E => true,
>= 0x2753 and <= 0x2755 => true,
0x2757 => true,
>= 0x2763 and <= 0x2764 => true,
>= 0x2795 and <= 0x2797 => true,
0x27A1 or 0x27B0 or 0x27BF => true,
>= 0x2934 and <= 0x2935 => true,
>= 0x2B05 and <= 0x2B07 => true,
0x2B1B or 0x2B1C or 0x2B50 or 0x2B55 => true,
_ => false
};
public static int RuneWidth(Rune rune)
{
int v = rune.Value;
// Zero-width: variation selectors and combining marks
if (v == 0xFE0F || v == 0xFE0E || (v >= 0x200B && v <= 0x200F) || v == 0x2060 || v == 0xFEFF)
return 0;
// Wide: CJK, fullwidth, emoji
if (v >= 0x1100 && (
(v <= 0x115F) || // Hangul Jamo
(v >= 0x2E80 && v <= 0x9FFF) || // CJK
(v >= 0xF900 && v <= 0xFAFF) || // CJK Compatibility
(v >= 0xFE30 && v <= 0xFE6F) || // CJK Compatibility Forms
(v >= 0xFF01 && v <= 0xFF60) || // Fullwidth forms
(v >= 0x1F000))) // Supplementary emoji (🔧💭 etc.)
return 2;
// BMP emoji with default emoji presentation
if (IsWideEmojiInBMP(v))
return 2;
return 1;
}
public static int VisibleWidth(string s)
{
int width = 0;
foreach (var rune in s.EnumerateRunes())
{
width += RuneWidth(rune);
}
return width;
}
public static string TruncateToWidth(string s, int maxWidth)
{
int width = 0;
int i = 0;
foreach (var rune in s.EnumerateRunes())
{
int charWidth = RuneWidth(rune);
if (width + charWidth > maxWidth) break;
width += charWidth;
i += rune.Utf16SequenceLength;
}
return s[..i];
}
public static string TruncateMarkupToWidth(string markupText, int maxWidth)
{
var result = new StringBuilder();
var openTags = new Stack<string>();
int visWidth = 0;
int i = 0;
while (i < markupText.Length && visWidth < maxWidth)
{
// Check for escaped brackets [[ or ]]
if (i + 1 < markupText.Length && markupText[i] == '[' && markupText[i + 1] == '[')
{
if (visWidth + 1 > maxWidth) break;
result.Append("[[");
visWidth++;
i += 2;
continue;
}
if (i + 1 < markupText.Length && markupText[i] == ']' && markupText[i + 1] == ']')
{
if (visWidth + 1 > maxWidth) break;
result.Append("]]");
visWidth++;
i += 2;
continue;
}
// Check for markup tags [xxx] or [/xxx] or [/]
if (markupText[i] == '[')
{
int closeIdx = markupText.IndexOf(']', i + 1);
if (closeIdx > i)
{
var tag = markupText[(i + 1)..closeIdx];
result.Append(markupText[i..(closeIdx + 1)]);
if (tag == "/" || tag.StartsWith("/"))
{
if (openTags.Count > 0) openTags.Pop();
}
else
{
openTags.Push(tag);
}
i = closeIdx + 1;
continue;
}
}
// Regular character — check width
try
{
var rune = Rune.GetRuneAt(markupText, i);
int charWidth = RuneWidth(rune);
if (visWidth + charWidth > maxWidth) break;
result.Append(markupText.AsSpan(i, rune.Utf16SequenceLength));
visWidth += charWidth;
i += rune.Utf16SequenceLength;
}
catch
{
// Invalid surrogate pair — treat as single-width character
if (visWidth + 1 > maxWidth) break;
result.Append(markupText[i]);
visWidth++;
i++;
}
}
// Append ellipsis inside the current markup context if truncated, then close open tags
bool wasTruncated = i < markupText.Length;
if (wasTruncated) result.Append('…');
while (openTags.Count > 0)
{
openTags.Pop();
result.Append("[/]");
}
return result.ToString();
}
public static string SkipMarkupWidth(string markupText, int skipColumns)
{
if (skipColumns <= 0) return markupText;
List<string> openTags = [];
int visWidth = 0;
int i = 0;
while (i < markupText.Length && visWidth < skipColumns)
{
// Escaped brackets [[ or ]]
if (i + 1 < markupText.Length && markupText[i] == '[' && markupText[i + 1] == '[')
{
visWidth++;
i += 2;
continue;
}
if (i + 1 < markupText.Length && markupText[i] == ']' && markupText[i + 1] == ']')
{
visWidth++;
i += 2;
continue;
}
// Markup tags
if (markupText[i] == '[')
{
int closeIdx = markupText.IndexOf(']', i + 1);
if (closeIdx > i)
{
var tag = markupText[(i + 1)..closeIdx];
if (tag == "/" || tag.StartsWith("/"))
{
if (openTags.Count > 0) openTags.RemoveAt(openTags.Count - 1);
}
else
{
openTags.Add(tag);
}
i = closeIdx + 1;
continue;
}
}
// Regular character
try
{
var rune = Rune.GetRuneAt(markupText, i);
int charWidth = RuneWidth(rune);
visWidth += charWidth;
i += rune.Utf16SequenceLength;
}
catch
{
visWidth++;
i++;
}
}
// Re-open any tags that were active at the skip point
var prefix = new StringBuilder();
foreach (var tag in openTags)
prefix.Append($"[{tag}]");
return prefix.ToString() + markupText[i..];
}
public static string PadVisible(string s, int totalWidth)
{
var visible = GetVisibleText(s);
int padding = totalWidth - VisibleWidth(visible);
return padding > 0 ? s + new string(' ', padding) : s;
}
public static string FormatRelativeTime(TimeSpan ts) => ts switch
{
{ TotalSeconds: < 0.1 } => "+0.0s",
{ TotalMinutes: < 1 } => $"+{ts.TotalSeconds:F1}s",
{ TotalHours: < 1 } => $"+{(int)ts.TotalMinutes}m {ts.Seconds}s",
_ => $"+{(int)ts.TotalHours}h {ts.Minutes}m"
};
public static string ExtractContentString(JsonElement el)
{
if (el.ValueKind == JsonValueKind.String)
return el.GetString() ?? "";
if (el.ValueKind == JsonValueKind.Object && el.TryGetProperty("content", out var c) && c.ValueKind == JsonValueKind.String)
return c.GetString() ?? "";
if (el.ValueKind == JsonValueKind.Array)
{
foreach (var item in el.EnumerateArray())
{
if (item.ValueKind == JsonValueKind.Object && item.TryGetProperty("text", out var t) && t.ValueKind == JsonValueKind.String)
return t.GetString() ?? "";
if (item.ValueKind == JsonValueKind.String)
return item.GetString() ?? "";
}
}
return el.GetRawText();
}
public static string SafeGetString(JsonElement el, string prop)
{
if (el.ValueKind == JsonValueKind.Object && el.TryGetProperty(prop, out var v) && v.ValueKind == JsonValueKind.String)
return v.GetString() ?? "";
return "";
}
public static string FormatAge(TimeSpan age) => age switch
{
{ TotalMinutes: < 1 } => "now",
{ TotalHours: < 1 } => $"{(int)age.TotalMinutes}m",
{ TotalDays: < 1 } => $"{(int)age.TotalHours}h",
{ TotalDays: < 30 } => $"{(int)age.TotalDays}d",
_ => $"{(int)(age.TotalDays / 30)}mo"
};
public static string FormatFileSize(long bytes) => bytes switch
{
< 1024 => $"{bytes}B",
< 1024 * 1024 => $"{bytes / 1024}KB",
_ => $"{bytes / (1024 * 1024.0):F1}MB"
};
public static string FormatDuration(TimeSpan ts) => ts switch
{
{ TotalSeconds: < 60 } => $"{(int)ts.TotalSeconds}s",
{ TotalMinutes: < 60 } => $"{(int)ts.TotalMinutes}m {ts.Seconds}s",
_ => $"{(int)ts.TotalHours}h {ts.Minutes}m"
};
public static List<string> ExpandGlob(string pattern)
{
List<string> result = [];
// Check if pattern contains wildcards
if (!pattern.Contains('*') && !pattern.Contains('?'))
{
// No wildcards, treat as literal path
if (File.Exists(pattern))
result.Add(Path.GetFullPath(pattern));
return result;
}
// Split pattern into directory and filename parts
var dirPath = Path.GetDirectoryName(pattern);
var fileName = Path.GetFileName(pattern);
if (string.IsNullOrEmpty(dirPath))
dirPath = ".";
if (!Directory.Exists(dirPath))
return result;
try
{
var files = Directory.GetFiles(dirPath, fileName, SearchOption.TopDirectoryOnly);
result.AddRange(files.Select(Path.GetFullPath));
}
catch
{
// Ignore errors (e.g., access denied)
}
return result;
}
}