Refactor IConsole.WithColors(...)

This commit is contained in:
Tyrrrz
2021-03-30 16:10:11 +03:00
parent c5a111207f
commit bfe99d620e
2 changed files with 15 additions and 11 deletions

View File

@@ -116,16 +116,10 @@ namespace CliFx.Infrastructure
public static IDisposable WithColors( public static IDisposable WithColors(
this IConsole console, this IConsole console,
ConsoleColor foregroundColor, ConsoleColor foregroundColor,
ConsoleColor backgroundColor) ConsoleColor backgroundColor) =>
{ Disposable.Merge(
var foregroundColorRegistration = console.WithForegroundColor(foregroundColor); console.WithForegroundColor(foregroundColor),
var backgroundColorRegistration = console.WithBackgroundColor(backgroundColor); console.WithBackgroundColor(backgroundColor)
);
return Disposable.Create(() =>
{
foregroundColorRegistration.Dispose();
backgroundColorRegistration.Dispose();
});
}
} }
} }

View File

@@ -1,4 +1,5 @@
using System; using System;
using System.Collections.Generic;
namespace CliFx.Utils namespace CliFx.Utils
{ {
@@ -14,5 +15,14 @@ namespace CliFx.Utils
internal partial class Disposable internal partial class Disposable
{ {
public static IDisposable Create(Action dispose) => new Disposable(dispose); public static IDisposable Create(Action dispose) => new Disposable(dispose);
public static IDisposable Merge(IEnumerable<IDisposable> disposables) => Create(() =>
{
foreach (var disposable in disposables)
disposable.Dispose();
});
public static IDisposable Merge(params IDisposable[] disposables) =>
Merge((IEnumerable<IDisposable>) disposables);
} }
} }