-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
445 lines (398 loc) · 17.3 KB
/
Program.cs
File metadata and controls
445 lines (398 loc) · 17.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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
using Markdig;
using Markdig.Syntax;
using Markdig.Syntax.Inlines;
using Spectre.Console;
using System.Text;
using System.Text.RegularExpressions;
var schemes = new ColorScheme[]
{
new("Monokai", "38;5;228;1", "38;5;81;1", "38;5;166;1", "38;5;141;1",
"38;5;197", "38;5;186", "38;5;242", "38;5;81", "38;5;141;48;5;236", "38;5;242", "1"),
new("Dracula", "38;5;141;1", "38;5;117;1", "38;5;84;1", "38;5;215;1",
"38;5;212", "38;5;228", "38;5;103", "38;5;117", "38;5;215;48;5;236", "38;5;103", "1"),
new("Nord", "38;5;110;1", "38;5;180;1", "38;5;108;1", "38;5;139;1",
"38;5;110", "38;5;108", "38;5;60", "38;5;180", "38;5;139;48;5;236", "38;5;60", "1"),
new("GitHub Dark","38;5;111;1", "38;5;215;1", "38;5;150;1", "38;5;176;1",
"38;5;204", "38;5;150", "38;5;245", "38;5;111", "38;5;215;48;5;236", "38;5;245", "1"),
new("Solarized", "38;5;136;1", "38;5;37;1", "38;5;64;1", "38;5;166;1",
"38;5;37", "38;5;36", "38;5;246", "38;5;33", "38;5;136;48;5;236", "38;5;246", "1"),
new("Catppuccin", "38;5;183;1", "38;5;116;1", "38;5;150;1", "38;5;217;1",
"38;5;183", "38;5;150", "38;5;243", "38;5;116", "38;5;217;48;5;236", "38;5;243", "1"),
};
// --- Parse input ---
var pipeline = new MarkdownPipelineBuilder().UseAdvancedExtensions().Build();
string markdown;
string fileName = "stdin";
if (args.Length > 0 && args[0] != "-")
{
if (!File.Exists(args[0]))
{
AnsiConsole.MarkupLine($"[red]File not found:[/] {args[0]}");
return 1;
}
markdown = File.ReadAllText(args[0]);
fileName = Path.GetFileName(args[0]);
}
else
{
using var reader = new StreamReader(Console.OpenStandardInput());
markdown = reader.ReadToEnd();
}
var width = AnsiConsole.Console.Profile.Width;
var doc = Markdown.Parse(markdown, pipeline);
int schemeIndex = 0;
var renderer = new TerminalRenderer(width, schemes[schemeIndex]);
var lines = renderer.RenderToLines(doc);
// Non-interactive: dump and exit
if (!AnsiConsole.Console.Profile.Capabilities.Interactive || lines.Count <= Console.WindowHeight - 2)
{
foreach (var line in lines)
Console.WriteLine(line);
return 0;
}
// --- Interactive pager ---
var pageHeight = Console.WindowHeight - 2;
int offset = 0;
int maxOffset = Math.Max(0, lines.Count - pageHeight);
Console.CursorVisible = false;
Console.Clear();
DrawPage(lines, offset, pageHeight, fileName, width, schemes[schemeIndex]);
while (true)
{
var key = Console.ReadKey(true);
int prevOffset = offset;
bool redraw = false;
switch (key.Key)
{
case ConsoleKey.DownArrow:
case ConsoleKey.J:
offset = Math.Min(offset + 1, maxOffset);
break;
case ConsoleKey.UpArrow:
case ConsoleKey.K:
offset = Math.Max(offset - 1, 0);
break;
case ConsoleKey.PageDown:
case ConsoleKey.Spacebar:
offset = Math.Min(offset + pageHeight, maxOffset);
break;
case ConsoleKey.PageUp:
case ConsoleKey.B:
offset = Math.Max(offset - pageHeight, 0);
break;
case ConsoleKey.Home:
case ConsoleKey.G when key.Modifiers == 0 && !char.IsUpper(key.KeyChar):
offset = 0;
break;
case ConsoleKey.End:
case ConsoleKey.G when char.IsUpper(key.KeyChar):
offset = maxOffset;
break;
case ConsoleKey.T:
schemeIndex = (schemeIndex + 1) % schemes.Length;
renderer = new TerminalRenderer(width, schemes[schemeIndex]);
lines = renderer.RenderToLines(doc);
maxOffset = Math.Max(0, lines.Count - pageHeight);
offset = Math.Min(offset, maxOffset);
redraw = true;
break;
case ConsoleKey.Q:
case ConsoleKey.Escape:
Console.Clear();
Console.CursorVisible = true;
return 0;
}
if (offset != prevOffset || redraw)
DrawPage(lines, offset, pageHeight, fileName, width, schemes[schemeIndex]);
}
static void DrawPage(List<string> lines, int offset, int pageHeight, string fileName, int width, ColorScheme scheme)
{
Console.SetCursorPosition(0, 0);
for (int i = 0; i < pageHeight; i++)
{
var idx = offset + i;
if (idx < lines.Count)
Console.Write(lines[idx]);
Console.Write("\x1b[K");
Console.WriteLine();
}
// Status bar: progress
var progress = lines.Count > 0 ? (int)((offset + pageHeight) * 100.0 / lines.Count) : 100;
progress = Math.Min(progress, 100);
var status = $" {fileName} │ {offset + 1}–{Math.Min(offset + pageHeight, lines.Count)} of {lines.Count} lines │ {progress}%";
Console.Write($"\x1b[7m{status.PadRight(width)}\x1b[0m\x1b[K");
// Keymap bar with theme button showing current scheme name
var keySb = new StringBuilder(" ");
var keys = new[] { "↑↓", "PgUp", "PgDn", "Home", "End", "t", "q" };
var labels = new[] { "scroll", "page up", "page down", "top", "bottom", scheme.Name, "quit" };
for (int k = 0; k < keys.Length; k++)
keySb.Append($"\x1b[97;48;5;238m {keys[k]} \x1b[0m\x1b[90m {labels[k]} \x1b[0m");
Console.Write(keySb.ToString());
Console.Write("\x1b[K");
}
// --- Renderer ---
class TerminalRenderer
{
private readonly int _width;
private readonly ColorScheme _cs;
public TerminalRenderer(int width, ColorScheme cs) { _width = width; _cs = cs; }
public List<string> RenderToLines(MarkdownDocument doc)
{
var output = new List<string>();
foreach (var block in doc)
RenderBlock(block, output);
return output;
}
private void RenderBlock(Block block, List<string> output)
{
switch (block)
{
case HeadingBlock h:
var level = h.Level;
var text = GetInlineText(h.Inline);
var color = level switch { 1 => _cs.Heading1, 2 => _cs.Heading2, 3 => _cs.Heading3, _ => _cs.Heading4 };
output.Add($"\x1b[{color}m{new string('#', level)} {text}\x1b[0m");
output.Add("");
break;
case ParagraphBlock p:
var paraText = RenderInlines(p.Inline);
foreach (var line in WrapText(paraText, _width))
output.Add(line);
output.Add("");
break;
case FencedCodeBlock code:
var lang = code.Info ?? "";
var codeText = string.Join('\n', code.Lines.Lines.Take(code.Lines.Count).Select(l => l.ToString()));
RenderCodeBlock(codeText, lang, output);
output.Add("");
break;
case CodeBlock code2:
var plainCode = string.Join('\n', code2.Lines.Lines.Take(code2.Lines.Count).Select(l => l.ToString()));
RenderCodeBlock(plainCode, "", output);
output.Add("");
break;
case Markdig.Extensions.Tables.Table table:
RenderTable(table, output);
output.Add("");
break;
case ListBlock list:
RenderList(list, 0, output);
output.Add("");
break;
case ThematicBreakBlock:
output.Add($"\x1b[{_cs.Border}m{"".PadRight(_width, '─')}\x1b[0m");
output.Add("");
break;
case QuoteBlock quote:
foreach (var child in quote)
{
if (child is ParagraphBlock qp)
{
var qText = RenderInlines(qp.Inline);
output.Add($"\x1b[{_cs.Border}m│\x1b[0m \x1b[3m{qText}\x1b[0m");
}
else RenderBlock(child, output);
}
output.Add("");
break;
case ContainerBlock container:
foreach (var child in container)
RenderBlock(child, output);
break;
}
}
private void RenderList(ListBlock list, int indent, List<string> output)
{
int index = 1;
foreach (var item in list)
{
if (item is ListItemBlock li)
{
var prefix = list.IsOrdered ? $"{index++}. " : "• ";
var pad = new string(' ', indent * 2);
foreach (var child in li)
{
if (child is ParagraphBlock p)
{
var text = RenderInlines(p.Inline);
output.Add($"{pad}{prefix}{text}");
prefix = " ";
}
else if (child is ListBlock nested)
RenderList(nested, indent + 1, output);
else RenderBlock(child, output);
}
}
}
}
private void RenderTable(Markdig.Extensions.Tables.Table table, List<string> output)
{
var rows = new List<List<string>>();
foreach (var rowObj in table)
if (rowObj is Markdig.Extensions.Tables.TableRow row)
{
var cells = new List<string>();
foreach (var cell in row)
if (cell is Markdig.Extensions.Tables.TableCell tc)
cells.Add(GetBlockText(tc));
rows.Add(cells);
}
if (rows.Count == 0) return;
var colCount = rows.Max(r => r.Count);
foreach (var row in rows) while (row.Count < colCount) row.Add("");
var colWidths = new int[colCount];
foreach (var row in rows)
for (int i = 0; i < colCount; i++)
colWidths[i] = Math.Max(colWidths[i], row[i].Length);
var totalContent = colWidths.Sum();
var borderChars = 1 + colCount * 3;
var available = _width - borderChars;
if (totalContent > available && available > 0)
{
var scale = (double)available / totalContent;
for (int i = 0; i < colCount; i++)
colWidths[i] = Math.Max(3, (int)(colWidths[i] * scale));
}
var b = _cs.Border;
var topBorder = "╭" + string.Join("┬", colWidths.Select(w => new string('─', w + 2))) + "╮";
var midBorder = "├" + string.Join("┼", colWidths.Select(w => new string('─', w + 2))) + "┤";
var botBorder = "╰" + string.Join("┴", colWidths.Select(w => new string('─', w + 2))) + "╯";
output.Add($"\x1b[{b}m{topBorder}\x1b[0m");
for (int r = 0; r < rows.Count; r++)
{
var sb = new StringBuilder();
sb.Append($"\x1b[{b}m│\x1b[0m");
for (int c = 0; c < colCount; c++)
{
var cell = rows[r][c];
if (cell.Length > colWidths[c]) cell = cell[..(colWidths[c] - 1)] + "…";
var padded = cell.PadRight(colWidths[c]);
if (r == 0)
sb.Append($" \x1b[{_cs.Bold}m{padded}\x1b[0m \x1b[{b}m│\x1b[0m");
else
sb.Append($" {padded} \x1b[{b}m│\x1b[0m");
}
output.Add(sb.ToString());
if (r == 0) output.Add($"\x1b[{b}m{midBorder}\x1b[0m");
}
output.Add($"\x1b[{b}m{botBorder}\x1b[0m");
}
private void RenderCodeBlock(string code, string lang, List<string> output)
{
var innerWidth = _width - 4;
var codeLines = code.Split('\n');
var b = _cs.Border;
var topLabel = string.IsNullOrEmpty(lang)
? $"\x1b[{b}m╭{new string('─', _width - 2)}╮\x1b[0m"
: $"\x1b[{b}m╭─\x1b[{_cs.Keyword}m{lang}\x1b[{b}m{new string('─', Math.Max(0, _width - 4 - lang.Length))}╮\x1b[0m";
output.Add(topLabel);
foreach (var line in codeLines)
{
var highlighted = HighlightLine(line, lang);
var visibleLen = VisibleLength(line);
var padding = Math.Max(0, innerWidth - visibleLen);
output.Add($"\x1b[{b}m│\x1b[0m {highlighted}{new string(' ', padding)} \x1b[{b}m│\x1b[0m");
}
output.Add($"\x1b[{b}m╰{new string('─', _width - 2)}╯\x1b[0m");
}
private string HighlightLine(string line, string lang)
{
if (string.IsNullOrEmpty(lang)) return line;
var keywords = GetKeywords(lang);
if (keywords.Length == 0) return line;
var result = Regex.Replace(line, @"""[^""]*""", m => $"\x1b[{_cs.String}m{m.Value}\x1b[0m");
result = Regex.Replace(result, @"(//.*|#\s.*)", m => $"\x1b[{_cs.Comment}m{m.Value}\x1b[0m");
foreach (var kw in keywords)
result = Regex.Replace(result, $@"\b{Regex.Escape(kw)}\b", $"\x1b[{_cs.Keyword}m{kw}\x1b[0m");
return result;
}
private static int VisibleLength(string s) =>
Regex.Replace(s, @"\x1b\[[0-9;]*m", "").Length;
private static string[] GetKeywords(string lang) => lang.ToLowerInvariant() switch
{
"csharp" or "cs" or "c#" => ["using", "namespace", "class", "struct", "interface", "enum", "public", "private", "protected", "internal", "static", "void", "int", "string", "bool", "var", "new", "return", "if", "else", "for", "foreach", "while", "switch", "case", "break", "async", "await", "null", "true", "false", "readonly", "override", "virtual", "abstract", "sealed"],
"javascript" or "js" or "typescript" or "ts" => ["const", "let", "var", "function", "return", "if", "else", "for", "while", "class", "import", "export", "from", "async", "await", "new", "null", "undefined", "true", "false", "this", "typeof", "interface", "type"],
"python" or "py" => ["def", "class", "import", "from", "return", "if", "elif", "else", "for", "while", "in", "not", "and", "or", "None", "True", "False", "self", "with", "as", "try", "except", "raise", "yield", "async", "await", "lambda"],
"rust" or "rs" => ["fn", "let", "mut", "pub", "struct", "enum", "impl", "trait", "use", "mod", "return", "if", "else", "for", "while", "match", "self", "Self", "true", "false", "async", "await", "where", "type"],
"go" => ["func", "package", "import", "var", "const", "type", "struct", "interface", "return", "if", "else", "for", "range", "switch", "case", "defer", "go", "chan", "map", "nil", "true", "false"],
_ => []
};
private string RenderInlines(ContainerInline? inlines)
{
if (inlines == null) return "";
var sb = new StringBuilder();
foreach (var inline in inlines)
{
switch (inline)
{
case LiteralInline lit:
sb.Append(lit.Content.ToString());
break;
case EmphasisInline em:
var inner = RenderInlines(em);
sb.Append(em.DelimiterCount == 2
? $"\x1b[{_cs.Bold}m{inner}\x1b[22m"
: $"\x1b[3m{inner}\x1b[23m");
break;
case CodeInline code:
sb.Append($"\x1b[{_cs.InlineCode}m{code.Content}\x1b[0m");
break;
case LinkInline link:
var linkText = RenderInlines(link);
sb.Append($"\x1b[{_cs.Link};4m{linkText}\x1b[0m \x1b[{_cs.Comment}m({link.Url})\x1b[0m");
break;
case LineBreakInline:
sb.Append('\n');
break;
default:
sb.Append(inline.ToString());
break;
}
}
return sb.ToString();
}
private static string GetInlineText(ContainerInline? inlines)
{
if (inlines == null) return "";
var sb = new StringBuilder();
foreach (var inline in inlines)
{
if (inline is LiteralInline lit) sb.Append(lit.Content.ToString());
else if (inline is ContainerInline container) sb.Append(GetInlineText(container));
else sb.Append(inline.ToString());
}
return sb.ToString();
}
private static string GetBlockText(Markdig.Extensions.Tables.TableCell cell)
{
var sb = new StringBuilder();
foreach (var block in cell)
if (block is ParagraphBlock p && p.Inline != null)
sb.Append(GetInlineText(p.Inline));
return sb.ToString();
}
private static List<string> WrapText(string text, int width)
{
if (width <= 0) return [text];
var result = new List<string>();
foreach (var segment in text.Split('\n'))
{
if (VisibleLength(segment) <= width) { result.Add(segment); continue; }
var words = segment.Split(' ');
var current = new StringBuilder();
int currentLen = 0;
foreach (var word in words)
{
var wLen = VisibleLength(word);
if (currentLen + wLen + (currentLen > 0 ? 1 : 0) > width && currentLen > 0)
{ result.Add(current.ToString()); current.Clear(); currentLen = 0; }
if (currentLen > 0) { current.Append(' '); currentLen++; }
current.Append(word); currentLen += wLen;
}
if (current.Length > 0) result.Add(current.ToString());
}
return result;
}
}
record ColorScheme(string Name, string Heading1, string Heading2, string Heading3, string Heading4,
string Keyword, string String, string Comment, string Link, string InlineCode, string Border, string Bold);