diff --git a/CliFx/Infrastructure/IConsole.cs b/CliFx/Infrastructure/IConsole.cs index d575e3c..2f8a1e8 100644 --- a/CliFx/Infrastructure/IConsole.cs +++ b/CliFx/Infrastructure/IConsole.cs @@ -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) + ); } } \ No newline at end of file diff --git a/CliFx/Utils/Disposable.cs b/CliFx/Utils/Disposable.cs index 50be370..8f5c888 100644 --- a/CliFx/Utils/Disposable.cs +++ b/CliFx/Utils/Disposable.cs @@ -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 disposables) => Create(() => + { + foreach (var disposable in disposables) + disposable.Dispose(); + }); + + public static IDisposable Merge(params IDisposable[] disposables) => + Merge((IEnumerable) disposables); } } \ No newline at end of file