forked from PowerShell/PowerShell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathListItemBlockRenderer.cs
More file actions
84 lines (74 loc) · 2.75 KB
/
Copy pathListItemBlockRenderer.cs
File metadata and controls
84 lines (74 loc) · 2.75 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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System;
using System.IO;
using System.Threading;
using Markdig;
using Markdig.Renderers;
using Markdig.Syntax;
namespace Microsoft.PowerShell.MarkdownRender
{
/// <summary>
/// Renderer for adding VT100 escape sequences for items in a list block.
/// </summary>
internal class ListItemBlockRenderer : VT100ObjectRenderer<ListItemBlock>
{
protected override void Write(VT100Renderer renderer, ListItemBlock obj)
{
if (obj.Parent is ListBlock parent)
{
if (!parent.IsOrdered)
{
foreach (var line in obj)
{
RenderWithIndent(renderer, line, parent.BulletType, 0);
}
}
}
}
private void RenderWithIndent(VT100Renderer renderer, MarkdownObject block, char listBullet, int indentLevel)
{
// Indent left by 2 for each level on list.
string indent = Padding(indentLevel * 2);
if (block is ParagraphBlock paragraphBlock)
{
renderer.Write(indent).Write(listBullet).Write(" ").Write(paragraphBlock.Inline);
}
else
{
// If there is a sublist, the block is a ListBlock instead of ParagraphBlock.
if (block is ListBlock subList)
{
foreach (var subListItem in subList)
{
if (subListItem is ListItemBlock subListItemBlock)
{
foreach (var line in subListItemBlock)
{
// Increment indent level for sub list.
RenderWithIndent(renderer, line, listBullet, indentLevel + 1);
}
}
}
}
}
}
// Typical padding is at most a screen's width, any more than that and we won't bother caching.
private const int IndentCacheMax = 120;
private static readonly string[] IndentCache = new string[IndentCacheMax];
internal static string Padding(int countOfSpaces)
{
if (countOfSpaces >= IndentCacheMax)
{
return new string(' ', countOfSpaces);
}
var result = IndentCache[countOfSpaces];
if (result == null)
{
Interlocked.CompareExchange(ref IndentCache[countOfSpaces], new string(' ', countOfSpaces), comparand: null);
result = IndentCache[countOfSpaces];
}
return result;
}
}
}