Permalink
Browse files

Adding colored console output

  • Loading branch information...
1 parent 1a55b9f commit 4767654ffac7bfc59293bdce1e4c7b29ec865e09 @agross agross committed Jan 21, 2013
View
@@ -79,6 +79,7 @@ Options:
-t, --timeinfo Shows time-related information in HTML output
-s, --silent Suppress progress output (print fatal errors, failures and summary)
-p, --progress Print dotted progress output
+-c, --no-color Suppress colored console output
-w, --wait Wait 15 seconds for debugger to be attached
--teamcity Reporting for TeamCity CI integration (also auto-detected)
--no-teamcity-autodetect Disables TeamCity autodetection
@@ -78,6 +78,7 @@
<Compile Include="ExitCode.cs" />
<Compile Include="IConsole.cs" />
<Compile Include="Options.cs" />
+ <Compile Include="Outputs\ColorOutput.cs" />
<Compile Include="Outputs\FailedSpecificationsSummary.cs" />
<Compile Include="Outputs\IOutput.cs" />
<Compile Include="Outputs\ProgressOutput.cs" />
@@ -35,6 +35,11 @@ public class Options
HelpText = "Print dotted progress output")]
public bool Progress = false;
+ [Option("c",
+ "no-color",
+ HelpText = "Suppress colored console output")]
+ public bool NoColor = false;
+
[Option("t",
"timeinfo",
HelpText = "Adds time-related information in HTML output")]
@@ -82,8 +87,9 @@ public string GetUsage()
sb.AppendLine(" -i, --include Execute all specifications in contexts with these comma delimited tags. Ex. -i \"foo,bar,foo_bar\"");
sb.AppendLine(" -x, --exclude Exclude specifications in contexts with these comma delimited tags. Ex. -x \"foo,bar,foo_bar\"");
sb.AppendLine(" -t, --timeinfo Shows time-related information in HTML output");
- sb.AppendLine(" -s, --silent Suppress console output");
+ sb.AppendLine(" -s, --silent Suppress progress output (print fatal errors, failures and summary)");
sb.AppendLine(" -p, --progress Print progress output");
+ sb.AppendLine(" -c, --no-color Suppress colored console output");
sb.AppendLine(" -w, --wait Wait 15 seconds for debugger to be attached");
sb.AppendLine(" --teamcity Reporting for TeamCity CI integration (also auto-detected)");
sb.AppendLine(" --no-teamcity-autodetect Disables TeamCity autodetection");
@@ -0,0 +1,99 @@
+using System;
+
+using Machine.Specifications.Runner;
+
+namespace Machine.Specifications.ConsoleRunner.Outputs
+{
+ public class ColorOutput : IOutput
+ {
+ readonly IOutput _inner;
+
+ public ColorOutput(IOutput inner)
+ {
+ _inner = inner;
+ }
+
+ public void RunStart()
+ {
+ _inner.RunStart();
+ }
+
+ public void RunEnd()
+ {
+ _inner.RunEnd();
+ }
+
+ public void AssemblyStart(AssemblyInfo assembly)
+ {
+ _inner.AssemblyStart(assembly);
+ }
+
+ public void AssemblyEnd(AssemblyInfo assembly)
+ {
+ _inner.AssemblyEnd(assembly);
+ }
+
+ public void ContextStart(ContextInfo context)
+ {
+ _inner.ContextStart(context);
+ }
+
+ public void ContextEnd(ContextInfo context)
+ {
+ _inner.ContextEnd(context);
+ }
+
+ public void SpecificationStart(SpecificationInfo specification)
+ {
+ }
+
+ public void Passing(SpecificationInfo specification)
+ {
+ Color(ConsoleColor.Green, () =>
+ {
+ _inner.SpecificationStart(specification);
+ _inner.Passing(specification);
+ });
+ }
+
+ public void NotImplemented(SpecificationInfo specification)
+ {
+ Color(ConsoleColor.Gray, () =>
+ {
+ _inner.SpecificationStart(specification);
+ _inner.NotImplemented(specification);
+ });
+ }
+
+ public void Ignored(SpecificationInfo specification)
+ {
+ Color(ConsoleColor.Yellow, () =>
+ {
+ _inner.SpecificationStart(specification);
+ _inner.Ignored(specification);
+ });
+ }
+
+ public void Failed(SpecificationInfo specification, Result result)
+ {
+ Color(ConsoleColor.Red, () =>
+ {
+ _inner.SpecificationStart(specification);
+ _inner.Failed(specification, result);
+ });
+ }
+
+ static void Color(ConsoleColor color, Action action)
+ {
+ try
+ {
+ Console.ForegroundColor = color;
+ action();
+ }
+ finally
+ {
+ Console.ResetColor();
+ }
+ }
+ }
+}
@@ -157,6 +157,7 @@ public ExitCode Run(string[] arguments)
static IOutput DetermineOutput(Options options, IConsole console)
{
IOutput output = new VerboseOutput(console);
+
if (options.Silent)
{
output = new SilentOutput();
@@ -165,7 +166,12 @@ static IOutput DetermineOutput(Options options, IConsole console)
{
output = new ProgressOutput(console);
}
- return output;
+ if (options.NoColor)
+ {
+ return output;
+ }
+
+ return new ColorOutput(output);
}
void WaitForDebugger()
@@ -199,4 +205,4 @@ public ISpecificationRunListener GetHtmlReportListener(Options options)
return new GenerateSparkHtmlReportRunListener(options.HtmlPath, options.ShowTimeInformation);
}
}
-}
+}

0 comments on commit 4767654

Please sign in to comment.