|
1 | 1 | // Licensed to the .NET Foundation under one or more agreements.
|
2 | 2 | // The .NET Foundation licenses this file to you under the MIT license.
|
3 | 3 |
|
4 |
| -namespace Microsoft.DotNet.Cli.Utils |
| 4 | +namespace Microsoft.DotNet.Cli.Utils; |
| 5 | + |
| 6 | +public class AnsiConsole |
5 | 7 | {
|
6 |
| - public class AnsiConsole |
7 |
| - { |
8 |
| - private const int Light = 0x08; |
9 |
| - private readonly bool _ansiEnabled; |
| 8 | + private const int Light = 0x08; |
| 9 | + private readonly bool _ansiEnabled; |
10 | 10 |
|
11 |
| - private AnsiConsole(TextWriter writer) |
12 |
| - { |
13 |
| - Writer = writer; |
| 11 | + private AnsiConsole(TextWriter writer) |
| 12 | + { |
| 13 | + Writer = writer; |
14 | 14 |
|
15 |
| - OriginalForegroundColor = Console.ForegroundColor; |
16 |
| - _boldRecursion = ((int)OriginalForegroundColor & Light) != 0 ? 1 : 0; |
| 15 | + OriginalForegroundColor = Console.ForegroundColor; |
| 16 | + _boldRecursion = ((int)OriginalForegroundColor & Light) != 0 ? 1 : 0; |
17 | 17 |
|
18 |
| - _ansiEnabled = string.IsNullOrWhiteSpace(Environment.GetEnvironmentVariable("NO_COLOR")); |
19 |
| - } |
| 18 | + _ansiEnabled = string.IsNullOrWhiteSpace(Environment.GetEnvironmentVariable("NO_COLOR")); |
| 19 | + } |
20 | 20 |
|
21 |
| - private int _boldRecursion; |
| 21 | + private int _boldRecursion; |
22 | 22 |
|
23 |
| - public static AnsiConsole GetOutput() |
24 |
| - { |
25 |
| - return new AnsiConsole(Console.Out); |
26 |
| - } |
| 23 | + public static AnsiConsole GetOutput() |
| 24 | + { |
| 25 | + return new AnsiConsole(Console.Out); |
| 26 | + } |
27 | 27 |
|
28 |
| - public static AnsiConsole GetError() |
29 |
| - { |
30 |
| - return new AnsiConsole(Console.Error); |
31 |
| - } |
| 28 | + public static AnsiConsole GetError() |
| 29 | + { |
| 30 | + return new AnsiConsole(Console.Error); |
| 31 | + } |
32 | 32 |
|
33 |
| - public TextWriter Writer { get; } |
| 33 | + public TextWriter Writer { get; } |
34 | 34 |
|
35 |
| - public ConsoleColor OriginalForegroundColor { get; } |
| 35 | + public ConsoleColor OriginalForegroundColor { get; } |
36 | 36 |
|
37 |
| - private void SetColor(ConsoleColor color) |
| 37 | + private void SetColor(ConsoleColor color) |
| 38 | + { |
| 39 | + if (!_ansiEnabled) |
38 | 40 | {
|
39 |
| - if (!_ansiEnabled) |
40 |
| - { |
41 |
| - return; |
42 |
| - } |
43 |
| - |
44 |
| - int c = (int)color; |
45 |
| - |
46 |
| - Console.ForegroundColor = |
47 |
| - c < 0 ? color : // unknown, just use it |
48 |
| - _boldRecursion > 0 ? (ConsoleColor)(c | Light) : // ensure color is light |
49 |
| - (ConsoleColor)(c & ~Light); // ensure color is dark |
| 41 | + return; |
50 | 42 | }
|
51 | 43 |
|
52 |
| - private void SetBold(bool bold) |
53 |
| - { |
54 |
| - if (!_ansiEnabled) |
55 |
| - { |
56 |
| - return; |
57 |
| - } |
| 44 | + int c = (int)color; |
58 | 45 |
|
59 |
| - _boldRecursion += bold ? 1 : -1; |
60 |
| - if (_boldRecursion > 1 || (_boldRecursion == 1 && !bold)) |
61 |
| - { |
62 |
| - return; |
63 |
| - } |
| 46 | + Console.ForegroundColor = |
| 47 | + c < 0 ? color : // unknown, just use it |
| 48 | + _boldRecursion > 0 ? (ConsoleColor)(c | Light) : // ensure color is light |
| 49 | + (ConsoleColor)(c & ~Light); // ensure color is dark |
| 50 | + } |
64 | 51 |
|
65 |
| - // switches on _boldRecursion to handle boldness |
66 |
| - SetColor(Console.ForegroundColor); |
| 52 | + private void SetBold(bool bold) |
| 53 | + { |
| 54 | + if (!_ansiEnabled) |
| 55 | + { |
| 56 | + return; |
67 | 57 | }
|
68 | 58 |
|
69 |
| - public void WriteLine(string message) |
| 59 | + _boldRecursion += bold ? 1 : -1; |
| 60 | + if (_boldRecursion > 1 || (_boldRecursion == 1 && !bold)) |
70 | 61 | {
|
71 |
| - Write(message); |
72 |
| - Writer.WriteLine(); |
| 62 | + return; |
73 | 63 | }
|
74 | 64 |
|
| 65 | + // switches on _boldRecursion to handle boldness |
| 66 | + SetColor(Console.ForegroundColor); |
| 67 | + } |
75 | 68 |
|
76 |
| - public void Write(string message) |
| 69 | + public void WriteLine(string message) |
| 70 | + { |
| 71 | + Write(message); |
| 72 | + Writer.WriteLine(); |
| 73 | + } |
| 74 | + |
| 75 | + |
| 76 | + public void Write(string message) |
| 77 | + { |
| 78 | + var escapeScan = 0; |
| 79 | + for (; ; ) |
77 | 80 | {
|
78 |
| - var escapeScan = 0; |
79 |
| - for (; ; ) |
| 81 | + var escapeIndex = message.IndexOf("\x1b[", escapeScan, StringComparison.Ordinal); |
| 82 | + if (escapeIndex == -1) |
80 | 83 | {
|
81 |
| - var escapeIndex = message.IndexOf("\x1b[", escapeScan, StringComparison.Ordinal); |
82 |
| - if (escapeIndex == -1) |
| 84 | + var text = message.Substring(escapeScan); |
| 85 | + Writer.Write(text); |
| 86 | + break; |
| 87 | + } |
| 88 | + else |
| 89 | + { |
| 90 | + var startIndex = escapeIndex + 2; |
| 91 | + var endIndex = startIndex; |
| 92 | + while (endIndex != message.Length && |
| 93 | + message[endIndex] >= 0x20 && |
| 94 | + message[endIndex] <= 0x3f) |
83 | 95 | {
|
84 |
| - var text = message.Substring(escapeScan); |
85 |
| - Writer.Write(text); |
86 |
| - break; |
| 96 | + endIndex += 1; |
87 | 97 | }
|
88 |
| - else |
| 98 | + |
| 99 | + var text = message.Substring(escapeScan, escapeIndex - escapeScan); |
| 100 | + Writer.Write(text); |
| 101 | + if (endIndex == message.Length) |
89 | 102 | {
|
90 |
| - var startIndex = escapeIndex + 2; |
91 |
| - var endIndex = startIndex; |
92 |
| - while (endIndex != message.Length && |
93 |
| - message[endIndex] >= 0x20 && |
94 |
| - message[endIndex] <= 0x3f) |
95 |
| - { |
96 |
| - endIndex += 1; |
97 |
| - } |
98 |
| - |
99 |
| - var text = message.Substring(escapeScan, escapeIndex - escapeScan); |
100 |
| - Writer.Write(text); |
101 |
| - if (endIndex == message.Length) |
102 |
| - { |
103 |
| - break; |
104 |
| - } |
| 103 | + break; |
| 104 | + } |
105 | 105 |
|
106 |
| - switch (message[endIndex]) |
107 |
| - { |
108 |
| - case 'm': |
109 |
| - int value; |
110 |
| - if (int.TryParse(message.Substring(startIndex, endIndex - startIndex), out value)) |
| 106 | + switch (message[endIndex]) |
| 107 | + { |
| 108 | + case 'm': |
| 109 | + int value; |
| 110 | + if (int.TryParse(message.Substring(startIndex, endIndex - startIndex), out value)) |
| 111 | + { |
| 112 | + switch (value) |
111 | 113 | {
|
112 |
| - switch (value) |
113 |
| - { |
114 |
| - case 1: |
115 |
| - SetBold(true); |
116 |
| - break; |
117 |
| - case 22: |
118 |
| - SetBold(false); |
119 |
| - break; |
120 |
| - case 30: |
121 |
| - SetColor(ConsoleColor.Black); |
122 |
| - break; |
123 |
| - case 31: |
124 |
| - SetColor(ConsoleColor.Red); |
125 |
| - break; |
126 |
| - case 32: |
127 |
| - SetColor(ConsoleColor.Green); |
128 |
| - break; |
129 |
| - case 33: |
130 |
| - SetColor(ConsoleColor.Yellow); |
131 |
| - break; |
132 |
| - case 34: |
133 |
| - SetColor(ConsoleColor.Blue); |
134 |
| - break; |
135 |
| - case 35: |
136 |
| - SetColor(ConsoleColor.Magenta); |
137 |
| - break; |
138 |
| - case 36: |
139 |
| - SetColor(ConsoleColor.Cyan); |
140 |
| - break; |
141 |
| - case 37: |
142 |
| - SetColor(ConsoleColor.Gray); |
143 |
| - break; |
144 |
| - case 39: |
145 |
| - Console.ForegroundColor = OriginalForegroundColor; |
146 |
| - break; |
147 |
| - } |
| 114 | + case 1: |
| 115 | + SetBold(true); |
| 116 | + break; |
| 117 | + case 22: |
| 118 | + SetBold(false); |
| 119 | + break; |
| 120 | + case 30: |
| 121 | + SetColor(ConsoleColor.Black); |
| 122 | + break; |
| 123 | + case 31: |
| 124 | + SetColor(ConsoleColor.Red); |
| 125 | + break; |
| 126 | + case 32: |
| 127 | + SetColor(ConsoleColor.Green); |
| 128 | + break; |
| 129 | + case 33: |
| 130 | + SetColor(ConsoleColor.Yellow); |
| 131 | + break; |
| 132 | + case 34: |
| 133 | + SetColor(ConsoleColor.Blue); |
| 134 | + break; |
| 135 | + case 35: |
| 136 | + SetColor(ConsoleColor.Magenta); |
| 137 | + break; |
| 138 | + case 36: |
| 139 | + SetColor(ConsoleColor.Cyan); |
| 140 | + break; |
| 141 | + case 37: |
| 142 | + SetColor(ConsoleColor.Gray); |
| 143 | + break; |
| 144 | + case 39: |
| 145 | + Console.ForegroundColor = OriginalForegroundColor; |
| 146 | + break; |
148 | 147 | }
|
149 |
| - break; |
150 |
| - } |
151 |
| - |
152 |
| - escapeScan = endIndex + 1; |
| 148 | + } |
| 149 | + break; |
153 | 150 | }
|
| 151 | + |
| 152 | + escapeScan = endIndex + 1; |
154 | 153 | }
|
155 | 154 | }
|
156 | 155 | }
|
|
0 commit comments