feat: Support NO_COLOR env var to disable CLI coloring

Setting `NO_COLOR=1` in your environment disables ANSI coloring in
console output. This is primarily useful in scenarios such as scripting
and piping output to files.

See: https://no-color.org/

Fixes #223
pull/268/head
Robert Dailey 5 months ago
parent bbc32ef3d7
commit 602f68a6e3

@ -19,6 +19,9 @@ changes you may need to make.
specified in the `config` include directive are resolved starting at this new directory. Relative
paths to include templates located under the `configs` directory is now **DEPRECATED**. See the
"File Structure" page on the wiki for more details.
- Support the [NO_COLOR] environment variable for all Recyclarr commands (#223).
[NO_COLOR]: https://no-color.org/
### Changed

@ -7,7 +7,7 @@ using Serilog.Templates.Themes;
namespace Recyclarr.Cli.Logging;
public class LoggerFactory(IAppPaths paths, LoggingLevelSwitch levelSwitch)
public class LoggerFactory(IAppPaths paths, LoggingLevelSwitch levelSwitch, IEnvironment env)
{
private static string GetBaseTemplateString()
{
@ -18,12 +18,13 @@ public class LoggerFactory(IAppPaths paths, LoggingLevelSwitch levelSwitch)
"{@m}";
}
private static ExpressionTemplate GetConsoleTemplate()
private ExpressionTemplate GetConsoleTemplate()
{
var template = "[{@l:u3}] " + GetBaseTemplateString() +
"{#if SanitizedExceptionMessage is not null}: {SanitizedExceptionMessage}{#end}\n";
return new ExpressionTemplate(template, theme: TemplateTheme.Code);
var raw = !string.IsNullOrEmpty(env.GetEnvironmentVariable("NO_COLOR"));
return new ExpressionTemplate(template, theme: raw ? null : TemplateTheme.Code);
}
private static ExpressionTemplate GetFileTemplate()

Loading…
Cancel
Save