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(
this IConsole console,
ConsoleColor foregroundColor,
ConsoleColor backgroundColor)
{
var foregroundColorRegistration = console.WithForegroundColor(foregroundColor);
var backgroundColorRegistration = console.WithBackgroundColor(backgroundColor);
return Disposable.Create(() =>
{
foregroundColorRegistration.Dispose();
backgroundColorRegistration.Dispose();
});
}
ConsoleColor backgroundColor) =>
Disposable.Merge(
console.WithForegroundColor(foregroundColor),
console.WithBackgroundColor(backgroundColor)
);
}
}

View File

@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
namespace CliFx.Utils
{
@@ -14,5 +15,14 @@ namespace CliFx.Utils
internal partial class Disposable
{
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);
}
}