Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions PowerKit/Extensions/ConsoleExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using System;

namespace PowerKit.Extensions;

internal static class ConsoleExtensions
{
extension(Console)
{
/// <summary>
/// Temporarily changes the console foreground color and returns a handle that restores the original color when disposed.
/// </summary>
public static IDisposable WithForegroundColor(ConsoleColor color)
{
Comment thread
Tyrrrz marked this conversation as resolved.
var lastColor = Console.ForegroundColor;
Console.ForegroundColor = color;

Comment thread
Tyrrrz marked this conversation as resolved.
return Disposable.Create(() => Console.ForegroundColor = lastColor);
}

/// <summary>
/// Temporarily changes the console background color and returns a handle that restores the original color when disposed.
/// </summary>
public static IDisposable WithBackgroundColor(ConsoleColor color)
{
var lastColor = Console.BackgroundColor;
Console.BackgroundColor = color;

return Disposable.Create(() => Console.BackgroundColor = lastColor);
}

/// <summary>
/// Temporarily changes the console foreground and background colors and returns a handle that restores the original colors when disposed.
/// </summary>
public static IDisposable WithColors(ConsoleColor foregroundColor, ConsoleColor backgroundColor) =>
Disposable.Merge(
Console.WithForegroundColor(foregroundColor),
Console.WithBackgroundColor(backgroundColor)
Comment thread
Tyrrrz marked this conversation as resolved.
);
}
}