Skip to content

Commit d3b7a0b

Browse files
committed
NO_COLOR support
1 parent 531697a commit d3b7a0b

File tree

2 files changed

+54
-35
lines changed

2 files changed

+54
-35
lines changed

README.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ By adding to \*.csproj file:
2828
<PackageReference Include="XConsole" Version="1.0.*" />
2929
```
3030
More ways to install see on [NuGet Gallery](https://www.nuget.org/packages/XConsole/).
31-
<br><br>
31+
<br><br><br>
3232

3333
## <a name="setup"></a>Setup
3434
Simplest way to start using the XConsole is by adding `using` to your code:
@@ -44,7 +44,7 @@ This trick is also great for upgrading your regular console application,
4444
because the XConsole is backwards compatible with the standard Console.
4545
Once you add `using`, your application will not change how it works
4646
unless you want to start using the XConsole features.
47-
<br><br>
47+
<br><br><br>
4848

4949
## <a name="coloring"></a>Coloring
5050
To colorize the text you need to add a prefix to a string.
@@ -72,7 +72,11 @@ Console.WriteLine("C`It is easy ", "bW`to use many", "R` colors in ", "Y`one mes
7272
The following table shows all possible colors and their letter designations:
7373

7474
![XConsole color table](https://raw.githubusercontent.com/chubrik/XConsole/main/img/colors-table.png)
75-
<br><br>
75+
<br>
76+
77+
### NO_COLOR
78+
XConsole supports the [`NO_COLOR`](https://no-color.org/) standard with an appropriate environment variable and property.
79+
<br><br><br>
7680

7781
## <a name="pinning"></a>Pinning
7882
You can pin some text below regular log messages with the `Pin` method.

XConsole/XConsole.cs

Lines changed: 47 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,17 @@ public static class XConsole
2121
{
2222
private static readonly object _syncLock = new();
2323
private static readonly string _newLine = Environment.NewLine;
24+
private static bool _colorsEnabled = Environment.GetEnvironmentVariable("NO_COLOR") == null;
2425
private static bool _cursorVisible = Console.CursorVisible;
2526
private static int _maxTop = Console.BufferHeight - 1;
2627
internal static long ShiftTop = 0;
2728

29+
public static bool NO_COLOR
30+
{
31+
get => !_colorsEnabled;
32+
set => _colorsEnabled = !value;
33+
}
34+
2835
public static void Sync(Action action)
2936
{
3037
lock (_syncLock)
@@ -745,43 +752,49 @@ public static (XConsolePosition Begin, XConsolePosition End) WriteLine(ulong val
745752

746753
private static void WriteItems(IReadOnlyList<XConsoleItem> items)
747754
{
748-
foreach (var item in items)
755+
if (_colorsEnabled)
749756
{
750-
if (item.BackColor == XConsoleItem.NoColor)
757+
foreach (var item in items)
751758
{
752-
if (item.ForeColor == XConsoleItem.NoColor)
759+
if (item.BackColor == XConsoleItem.NoColor)
753760
{
754-
Console.Write(item.Value);
755-
}
756-
else
757-
{
758-
var origForeColor = Console.ForegroundColor;
759-
Console.ForegroundColor = item.ForeColor;
760-
Console.Write(item.Value);
761-
Console.ForegroundColor = origForeColor;
762-
}
763-
}
764-
else
765-
{
766-
if (item.ForeColor == XConsoleItem.NoColor)
767-
{
768-
var origBackColor = Console.BackgroundColor;
769-
Console.BackgroundColor = item.BackColor;
770-
Console.Write(item.Value);
771-
Console.BackgroundColor = origBackColor;
761+
if (item.ForeColor == XConsoleItem.NoColor)
762+
{
763+
Console.Write(item.Value);
764+
}
765+
else
766+
{
767+
var origForeColor = Console.ForegroundColor;
768+
Console.ForegroundColor = item.ForeColor;
769+
Console.Write(item.Value);
770+
Console.ForegroundColor = origForeColor;
771+
}
772772
}
773773
else
774774
{
775-
var origBackColor = Console.BackgroundColor;
776-
var origForeColor = Console.ForegroundColor;
777-
Console.BackgroundColor = item.BackColor;
778-
Console.ForegroundColor = item.ForeColor;
779-
Console.Write(item.Value);
780-
Console.BackgroundColor = origBackColor;
781-
Console.ForegroundColor = origForeColor;
775+
if (item.ForeColor == XConsoleItem.NoColor)
776+
{
777+
var origBackColor = Console.BackgroundColor;
778+
Console.BackgroundColor = item.BackColor;
779+
Console.Write(item.Value);
780+
Console.BackgroundColor = origBackColor;
781+
}
782+
else
783+
{
784+
var origBackColor = Console.BackgroundColor;
785+
var origForeColor = Console.ForegroundColor;
786+
Console.BackgroundColor = item.BackColor;
787+
Console.ForegroundColor = item.ForeColor;
788+
Console.Write(item.Value);
789+
Console.BackgroundColor = origBackColor;
790+
Console.ForegroundColor = origForeColor;
791+
}
782792
}
783793
}
784794
}
795+
else
796+
foreach (var item in items)
797+
Console.Write(item.Value);
785798
}
786799

787800
private static int GetLineWrapCount(IReadOnlyList<XConsoleItem> items, int beginLeft)
@@ -855,8 +868,9 @@ public static ConsoleColor BackgroundColor
855868
}
856869
set
857870
{
858-
lock (_syncLock)
859-
Console.BackgroundColor = value;
871+
if (_colorsEnabled)
872+
lock (_syncLock)
873+
Console.BackgroundColor = value;
860874
}
861875
}
862876

@@ -952,8 +966,9 @@ public static ConsoleColor ForegroundColor
952966
}
953967
set
954968
{
955-
lock (_syncLock)
956-
Console.ForegroundColor = value;
969+
if (_colorsEnabled)
970+
lock (_syncLock)
971+
Console.ForegroundColor = value;
957972
}
958973
}
959974

0 commit comments

Comments
 (0)