Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add asynchronous AlternateScreen method #1080

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
38 changes: 38 additions & 0 deletions src/Spectre.Console/Extensions/AnsiConsoleExtensions.Screen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,43 @@ public static void AlternateScreen(this IAnsiConsole console, Action action)
// Switch back to primary screen
console.Write(new ControlCode("\u001b[?1049l"));
}
}

/// <summary>
/// Switches to an alternate screen buffer asynchronously if the terminal supports it.
/// </summary>
/// <param name="console">The console.</param>
/// <param name="action">The action to execute within the alternate screen buffer.</param>
/// <returns>The result of the function.</returns>
public static async Task AlternateScreenAsync(this IAnsiConsole console, Func<Task> action)
{
if (console is null)
{
throw new ArgumentNullException(nameof(console));
}

if (!console.Profile.Capabilities.Ansi)
{
throw new NotSupportedException("Alternate buffers are not supported since your terminal does not support ANSI.");
}

if (!console.Profile.Capabilities.AlternateBuffer)
{
throw new NotSupportedException("Alternate buffers are not supported by your terminal.");
}

// Switch to alternate screen
console.Write(new ControlCode("\u001b[?1049h\u001b[H"));

try
{
// Execute custom action
await action();
}
finally
{
// Switch back to primary screen
console.Write(new ControlCode("\u001b[?1049l"));
}
}
}