@@ -46,7 +46,7 @@ public static bool TryParse(
46
46
[ MaybeNullWhen ( true ) ] out string error )
47
47
{
48
48
if ( template == null ) throw new ArgumentNullException ( nameof ( template ) ) ;
49
- return TryParse ( template , null , null , null , out result , out error ) ;
49
+ return TryParse ( template , null , null , null , false , out result , out error ) ;
50
50
}
51
51
52
52
/// <summary>
@@ -60,12 +60,15 @@ public static bool TryParse(
60
60
/// <param name="error">A description of the error, if unsuccessful.</param>
61
61
/// <param name="nameResolver">Optionally, a <see cref="NameResolver"/>
62
62
/// with which to resolve function names that appear in the template.</param>
63
+ /// <param name="applyThemeWhenOutputIsRedirected">Apply <paramref name="theme"/> even when
64
+ /// <see cref="System.Console.IsOutputRedirected"/> or <see cref="Console.IsErrorRedirected"/> returns <c>true</c>.</param>
63
65
/// <returns><c langword="true">true</c> if the template was well-formed.</returns>
64
66
public static bool TryParse (
65
67
string template ,
66
68
IFormatProvider ? formatProvider ,
67
69
NameResolver ? nameResolver ,
68
70
TemplateTheme ? theme ,
71
+ bool applyThemeWhenOutputIsRedirected ,
69
72
[ MaybeNullWhen ( false ) ] out ExpressionTemplate result ,
70
73
[ MaybeNullWhen ( true ) ] out string error )
71
74
{
@@ -84,7 +87,7 @@ public static bool TryParse(
84
87
TemplateCompiler . Compile (
85
88
planned ,
86
89
formatProvider , DefaultFunctionNameResolver . Build ( nameResolver ) ,
87
- theme ?? TemplateTheme . None ) ) ;
90
+ SelectTheme ( theme , applyThemeWhenOutputIsRedirected ) ) ) ;
88
91
89
92
return true ;
90
93
}
@@ -103,11 +106,14 @@ public static bool TryParse(
103
106
/// <param name="nameResolver">Optionally, a <see cref="NameResolver"/>
104
107
/// with which to resolve function names that appear in the template.</param>
105
108
/// <param name="theme">Optionally, an ANSI theme to apply to the template output.</param>
109
+ /// <param name="applyThemeWhenOutputIsRedirected">Apply <paramref name="theme"/> even when
110
+ /// <see cref="Console.IsOutputRedirected"/> or <see cref="Console.IsErrorRedirected"/> returns <c>true</c>.</param>
106
111
public ExpressionTemplate (
107
112
string template ,
108
113
IFormatProvider ? formatProvider = null ,
109
114
NameResolver ? nameResolver = null ,
110
- TemplateTheme ? theme = null )
115
+ TemplateTheme ? theme = null ,
116
+ bool applyThemeWhenOutputIsRedirected = false )
111
117
{
112
118
if ( template == null ) throw new ArgumentNullException ( nameof ( template ) ) ;
113
119
@@ -119,7 +125,20 @@ public ExpressionTemplate(
119
125
120
126
_compiled = TemplateCompiler . Compile (
121
127
planned ,
122
- formatProvider , DefaultFunctionNameResolver . Build ( nameResolver ) , theme ?? TemplateTheme . None ) ;
128
+ formatProvider ,
129
+ DefaultFunctionNameResolver . Build ( nameResolver ) ,
130
+ SelectTheme ( theme , applyThemeWhenOutputIsRedirected ) ) ;
131
+ }
132
+
133
+ static TemplateTheme SelectTheme ( TemplateTheme ? supplied , bool applyThemeWhenOutputIsRedirected )
134
+ {
135
+ if ( supplied == null ||
136
+ ( Console . IsOutputRedirected || Console . IsErrorRedirected ) && ! applyThemeWhenOutputIsRedirected )
137
+ {
138
+ return TemplateTheme . None ;
139
+ }
140
+
141
+ return supplied ;
123
142
}
124
143
125
144
/// <inheritdoc />
0 commit comments