@@ -46,7 +46,7 @@ public static bool TryParse(
4646 [ MaybeNullWhen ( true ) ] out string error )
4747 {
4848 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 ) ;
5050 }
5151
5252 /// <summary>
@@ -60,12 +60,15 @@ public static bool TryParse(
6060 /// <param name="error">A description of the error, if unsuccessful.</param>
6161 /// <param name="nameResolver">Optionally, a <see cref="NameResolver"/>
6262 /// 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>
6365 /// <returns><c langword="true">true</c> if the template was well-formed.</returns>
6466 public static bool TryParse (
6567 string template ,
6668 IFormatProvider ? formatProvider ,
6769 NameResolver ? nameResolver ,
6870 TemplateTheme ? theme ,
71+ bool applyThemeWhenOutputIsRedirected ,
6972 [ MaybeNullWhen ( false ) ] out ExpressionTemplate result ,
7073 [ MaybeNullWhen ( true ) ] out string error )
7174 {
@@ -84,7 +87,7 @@ public static bool TryParse(
8487 TemplateCompiler . Compile (
8588 planned ,
8689 formatProvider , DefaultFunctionNameResolver . Build ( nameResolver ) ,
87- theme ?? TemplateTheme . None ) ) ;
90+ SelectTheme ( theme , applyThemeWhenOutputIsRedirected ) ) ) ;
8891
8992 return true ;
9093 }
@@ -103,11 +106,14 @@ public static bool TryParse(
103106 /// <param name="nameResolver">Optionally, a <see cref="NameResolver"/>
104107 /// with which to resolve function names that appear in the template.</param>
105108 /// <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>
106111 public ExpressionTemplate (
107112 string template ,
108113 IFormatProvider ? formatProvider = null ,
109114 NameResolver ? nameResolver = null ,
110- TemplateTheme ? theme = null )
115+ TemplateTheme ? theme = null ,
116+ bool applyThemeWhenOutputIsRedirected = false )
111117 {
112118 if ( template == null ) throw new ArgumentNullException ( nameof ( template ) ) ;
113119
@@ -119,7 +125,20 @@ public ExpressionTemplate(
119125
120126 _compiled = TemplateCompiler . Compile (
121127 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 ;
123142 }
124143
125144 /// <inheritdoc />
0 commit comments