Repository navigation
Expand file tree
/
Copy pathCapabilitiesText.cs
More file actions
62 lines (56 loc) · 1.67 KB
/
Copy pathCapabilitiesText.cs
File metadata and controls
62 lines (56 loc) · 1.67 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
using System;
using System.Text;
namespace Screenctl;
internal static class CapabilitiesText
{
public static string Format(string text)
{
text = text.Trim();
var output = new StringBuilder();
var depth = 0;
var position = 0;
while (position < text.Length)
{
if (text[position] == ')')
{
depth--;
Line(")");
position++;
continue;
}
var opening = text.IndexOf('(', position);
var closing = text.IndexOf(')', position);
if (opening < 0 || closing < opening)
{
Line(text[position..closing].Trim());
position = closing;
continue;
}
var prefix = text[position..opening].Trim();
var separator = prefix.LastIndexOfAny([' ', '\r', '\n', '\t']);
if (separator >= 0)
{
Line(prefix[..separator].TrimEnd());
prefix = prefix[(separator + 1)..];
}
var end = text.IndexOf(')', opening + 1);
var nested = text.IndexOf('(', opening + 1);
if (nested < 0 || end < nested)
{
Line(prefix + text[opening..(end + 1)]);
position = end + 1;
}
else
{
Line(prefix + "(");
depth++;
position = opening + 1;
}
}
return output.ToString().TrimEnd();
void Line(string value)
{
if (value.Length > 0) output.Append(' ', depth * 2).AppendLine(value);
}
}
}