-
-
Notifications
You must be signed in to change notification settings - Fork 499
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for alternate screen buffers
Closes #250
- Loading branch information
1 parent
2e5d18f
commit fd4b969
Showing
17 changed files
with
243 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net6.0</TargetFramework> | ||
<ExampleTitle>Screens</ExampleTitle> | ||
<ExampleDescription>Demonstrates how to use alternate screens.</ExampleDescription> | ||
<ExampleGroup>Widgets</ExampleGroup> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\Shared\Shared.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// Check if we can use alternate screen buffers | ||
using Spectre.Console; | ||
|
||
if (!AnsiConsole.Profile.Capabilities.AlternateBuffer) | ||
{ | ||
AnsiConsole.MarkupLine( | ||
"[red]Alternate screen buffers are not supported " + | ||
"by your terminal[/] [yellow]:([/]"); | ||
|
||
return; | ||
} | ||
|
||
// Write to the terminal | ||
AnsiConsole.Write(new Rule("[yellow]Normal universe[/]")); | ||
AnsiConsole.Write(new Panel("Hello World!")); | ||
AnsiConsole.MarkupLine("[grey]Press a key to continue[/]"); | ||
AnsiConsole.Console.Input.ReadKey(true); | ||
|
||
AnsiConsole.AlternateScreen(() => | ||
{ | ||
// Now we're in another terminal screen buffer | ||
AnsiConsole.Write(new Rule("[red]Mirror universe[/]")); | ||
AnsiConsole.Write(new Panel("[red]Welcome to the upside down![/]")); | ||
AnsiConsole.MarkupLine("[grey]Press a key to return[/]"); | ||
AnsiConsole.Console.Input.ReadKey(true); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
using System; | ||
|
||
namespace Spectre.Console | ||
{ | ||
/// <summary> | ||
/// A console capable of writing ANSI escape sequences. | ||
/// </summary> | ||
public static partial class AnsiConsole | ||
{ | ||
/// <summary> | ||
/// Switches to an alternate screen buffer if the terminal supports it. | ||
/// </summary> | ||
/// <param name="action">The action to execute within the alternate screen buffer.</param> | ||
public static void AlternateScreen(Action action) | ||
{ | ||
Console.AlternateScreen(action); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
src/Spectre.Console/Extensions/AnsiConsoleExtensions.Screen.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
using System; | ||
|
||
namespace Spectre.Console | ||
{ | ||
/// <summary> | ||
/// Contains extension methods for <see cref="IAnsiConsole"/>. | ||
/// </summary> | ||
public static partial class AnsiConsoleExtensions | ||
{ | ||
/// <summary> | ||
/// Switches to an alternate screen buffer 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> | ||
public static void AlternateScreen(this IAnsiConsole console, Action 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."); | ||
} | ||
|
||
console.ExclusivityMode.Run<object?>(() => | ||
{ | ||
// Switch to alternate screen | ||
console.Write(new ControlCode("\u001b[?1049h\u001b[H")); | ||
// Execute custom action | ||
action(); | ||
// Switch back to primary screen | ||
console.Write(new ControlCode("\u001b[?1049l")); | ||
// Dummy result | ||
return null; | ||
}); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
test/Spectre.Console.Tests/Expectations/AlternateScreen/Show.Output.verified.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
Foo | ||
[?1049h[HBar | ||
[?1049l |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
using System.Threading.Tasks; | ||
using Shouldly; | ||
using Spectre.Console.Testing; | ||
using Spectre.Verify.Extensions; | ||
using VerifyXunit; | ||
using Xunit; | ||
|
||
namespace Spectre.Console.Tests.Unit | ||
{ | ||
[UsesVerify] | ||
[ExpectationPath("AlternateScreen")] | ||
public sealed class AlternateScreenTests | ||
{ | ||
[Fact] | ||
public void Should_Throw_If_Alternative_Buffer_Is_Not_Supported_By_Terminal() | ||
{ | ||
// Given | ||
var console = new TestConsole(); | ||
console.Profile.Capabilities.AlternateBuffer = false; | ||
|
||
// When | ||
var result = Record.Exception(() => | ||
{ | ||
console.WriteLine("Foo"); | ||
console.AlternateScreen(() => | ||
{ | ||
console.WriteLine("Bar"); | ||
}); | ||
}); | ||
|
||
// Then | ||
result.ShouldNotBeNull(); | ||
result.Message.ShouldBe("Alternate buffers are not supported by your terminal."); | ||
} | ||
|
||
[Fact] | ||
public void Should_Throw_If_Ansi_Is_Not_Supported_By_Terminal() | ||
{ | ||
// Given | ||
var console = new TestConsole(); | ||
console.Profile.Capabilities.Ansi = false; | ||
console.Profile.Capabilities.AlternateBuffer = true; | ||
|
||
// When | ||
var result = Record.Exception(() => | ||
{ | ||
console.WriteLine("Foo"); | ||
console.AlternateScreen(() => | ||
{ | ||
console.WriteLine("Bar"); | ||
}); | ||
}); | ||
|
||
// Then | ||
result.ShouldNotBeNull(); | ||
result.Message.ShouldBe("Alternate buffers are not supported since your terminal does not support ANSI."); | ||
} | ||
|
||
[Fact] | ||
[Expectation("Show")] | ||
public async Task Should_Write_To_Alternate_Screen() | ||
{ | ||
// Given | ||
var console = new TestConsole(); | ||
console.EmitAnsiSequences = true; | ||
console.Profile.Capabilities.AlternateBuffer = true; | ||
|
||
// When | ||
console.WriteLine("Foo"); | ||
console.AlternateScreen(() => | ||
{ | ||
console.WriteLine("Bar"); | ||
}); | ||
|
||
// Then | ||
await Verifier.Verify(console.Output); | ||
} | ||
} | ||
} |