diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 05a51d3..0e0c66e 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -31,6 +31,16 @@ jobs: with: dotnet-version: 3.1.301 + - name: Integration Tests + shell: bash + run: | + dotnet tool restore + dotnet example diagnostic + dotnet example table + dotnet example grid + dotnet example panel + dotnet example colors + - name: Build shell: bash run: | diff --git a/dotnet-tools.json b/dotnet-tools.json index f697e67..4c4c563 100644 --- a/dotnet-tools.json +++ b/dotnet-tools.json @@ -13,6 +13,12 @@ "commands": [ "gpr" ] + }, + "dotnet-example": { + "version": "0.8.0", + "commands": [ + "dotnet-example" + ] } } } \ No newline at end of file diff --git a/examples/Colors/Program.cs b/examples/Colors/Program.cs index e514210..0c031e1 100644 --- a/examples/Colors/Program.cs +++ b/examples/Colors/Program.cs @@ -2,75 +2,117 @@ using Spectre.Console; namespace ColorExample { - class Program + public static class Program { - static void Main(string[] args) + public static void Main(string[] args) { - ///////////////////////////////////////////////////////////////// - // 4-BIT - ///////////////////////////////////////////////////////////////// - - AnsiConsole.ResetColors(); - AnsiConsole.WriteLine(); - AnsiConsole.MarkupLine("[bold underline]4-bit Colors[/]"); - AnsiConsole.WriteLine(); - - for (var i = 0; i < 16; i++) + if (AnsiConsole.Capabilities.ColorSystem == ColorSystem.NoColors) { - AnsiConsole.Background = Color.FromInt32(i); - AnsiConsole.Write(string.Format(" {0,-9}", AnsiConsole.Background.ToString())); - AnsiConsole.ResetColors(); - if ((i + 1) % 8 == 0) - { - AnsiConsole.WriteLine(); - } + ///////////////////////////////////////////////////////////////// + // No colors + ///////////////////////////////////////////////////////////////// + + AnsiConsole.WriteLine("No colors are supported."); + return; } - ///////////////////////////////////////////////////////////////// - // 8-BIT - ///////////////////////////////////////////////////////////////// - - AnsiConsole.ResetColors(); - AnsiConsole.WriteLine(); - AnsiConsole.MarkupLine("[bold underline]8-bit Colors[/]"); - AnsiConsole.WriteLine(); - - for (var i = 0; i < 16; i++) + if (AnsiConsole.Capabilities.Supports(ColorSystem.Legacy)) { - for (var j = 0; j < 16; j++) + ///////////////////////////////////////////////////////////////// + // 3-BIT + ///////////////////////////////////////////////////////////////// + + AnsiConsole.ResetColors(); + AnsiConsole.WriteLine(); + AnsiConsole.MarkupLine("[bold underline]3-bit Colors[/]"); + AnsiConsole.WriteLine(); + + for (var i = 0; i < 8; i++) { - var number = i * 16 + j; - AnsiConsole.Background = Color.FromInt32(number); - AnsiConsole.Write(string.Format(" {0,-4}", number)); + AnsiConsole.Background = Color.FromInt32(i); + AnsiConsole.Write(string.Format(" {0,-9}", AnsiConsole.Background.ToString())); AnsiConsole.ResetColors(); - if ((number + 1) % 16 == 0) + if ((i + 1) % 8 == 0) { AnsiConsole.WriteLine(); } } } - ///////////////////////////////////////////////////////////////// - // 24-BIT - ///////////////////////////////////////////////////////////////// - - AnsiConsole.ResetColors(); - AnsiConsole.WriteLine(); - AnsiConsole.MarkupLine("[bold underline]24-bit Colors[/]"); - AnsiConsole.WriteLine(); - - var index = 0; - for (var i = 0.0005; i < 1; i += 0.0025) + if (AnsiConsole.Capabilities.Supports(ColorSystem.Standard)) { - index++; + ///////////////////////////////////////////////////////////////// + // 4-BIT + ///////////////////////////////////////////////////////////////// - var color = Utilities.HSL2RGB(i, 0.5, 0.5); - AnsiConsole.Background = new Color(color.R, color.G, color.B); - AnsiConsole.Write(" "); + AnsiConsole.ResetColors(); + AnsiConsole.WriteLine(); + AnsiConsole.MarkupLine("[bold underline]4-bit Colors[/]"); + AnsiConsole.WriteLine(); - if (index % 50 == 0) + for (var i = 0; i < 16; i++) { - AnsiConsole.WriteLine(); + AnsiConsole.Background = Color.FromInt32(i); + AnsiConsole.Write(string.Format(" {0,-9}", AnsiConsole.Background.ToString())); + AnsiConsole.ResetColors(); + if ((i + 1) % 8 == 0) + { + AnsiConsole.WriteLine(); + } + } + } + + if (AnsiConsole.Capabilities.Supports(ColorSystem.EightBit)) + { + ///////////////////////////////////////////////////////////////// + // 8-BIT + ///////////////////////////////////////////////////////////////// + + AnsiConsole.ResetColors(); + AnsiConsole.WriteLine(); + AnsiConsole.MarkupLine("[bold underline]8-bit Colors[/]"); + AnsiConsole.WriteLine(); + + for (var i = 0; i < 16; i++) + { + for (var j = 0; j < 16; j++) + { + var number = i * 16 + j; + AnsiConsole.Background = Color.FromInt32(number); + AnsiConsole.Write(string.Format(" {0,-4}", number)); + AnsiConsole.ResetColors(); + if ((number + 1) % 16 == 0) + { + AnsiConsole.WriteLine(); + } + } + } + } + + if (AnsiConsole.Capabilities.Supports(ColorSystem.TrueColor)) + { + ///////////////////////////////////////////////////////////////// + // 24-BIT + ///////////////////////////////////////////////////////////////// + + AnsiConsole.ResetColors(); + AnsiConsole.WriteLine(); + AnsiConsole.MarkupLine("[bold underline]24-bit Colors[/]"); + AnsiConsole.WriteLine(); + + var index = 0; + for (var i = 0.0005; i < 1; i += 0.0025) + { + index++; + + var color = Utilities.HSL2RGB(i, 0.5, 0.5); + AnsiConsole.Background = new Color(color.R, color.G, color.B); + AnsiConsole.Write(" "); + + if (index % 50 == 0) + { + AnsiConsole.WriteLine(); + } } } } diff --git a/examples/Diagnostic/Diagnostic.csproj b/examples/Diagnostic/Diagnostic.csproj new file mode 100644 index 0000000..5c16c82 --- /dev/null +++ b/examples/Diagnostic/Diagnostic.csproj @@ -0,0 +1,14 @@ + + + + Exe + netcoreapp3.1 + false + Displays the capabilities of the current console. + + + + + + + diff --git a/examples/Diagnostic/Program.cs b/examples/Diagnostic/Program.cs new file mode 100644 index 0000000..a577ff4 --- /dev/null +++ b/examples/Diagnostic/Program.cs @@ -0,0 +1,18 @@ +using System; +using Spectre.Console; + +namespace Diagnostic +{ + public class Program + { + public static void Main(string[] args) + { + AnsiConsole.MarkupLine("Color system: [bold]{0}[/]", AnsiConsole.Capabilities.ColorSystem); + AnsiConsole.MarkupLine("Supports ansi? [bold]{0}[/]", AnsiConsole.Capabilities.SupportsAnsi); + AnsiConsole.MarkupLine("Legacy console? [bold]{0}[/]", AnsiConsole.Capabilities.LegacyConsole); + AnsiConsole.WriteLine(); + AnsiConsole.MarkupLine("Buffer width: [bold]{0}[/]", AnsiConsole.Console.Width); + AnsiConsole.MarkupLine("Buffer height: [bold]{0}[/]", AnsiConsole.Console.Height); + } + } +} diff --git a/examples/Panel/Program.cs b/examples/Panel/Program.cs index f605e24..7b92f33 100644 --- a/examples/Panel/Program.cs +++ b/examples/Panel/Program.cs @@ -7,9 +7,8 @@ namespace PanelExample static void Main(string[] args) { var content = new Markup( - "[underline]I[/] heard [underline on blue]you[/] like 📦\n\n\n\n" + - "So I put a 📦 in a 📦\n\n" + - "😅").Centered(); + "[underline]I[/] heard [underline on blue]you[/] like panels\n\n\n\n" + + "So I put a panel in a panel").Centered(); AnsiConsole.Render( new Panel( diff --git a/examples/Table/Program.cs b/examples/Table/Program.cs index eaf8af9..3912867 100644 --- a/examples/Table/Program.cs +++ b/examples/Table/Program.cs @@ -41,16 +41,16 @@ namespace TableExample table.AddColumn(new TableColumn("[blue]Bar[/]") { Alignment = Justify.Right, NoWrap = true }); // Add some rows - table.AddRow("[blue][underline]Hell[/]o[/]", "World 🌍"); + table.AddRow("[blue][underline]Hell[/]o[/]", "World"); table.AddRow("[yellow]Patrik [green]\"Hello World\"[/] Svensson[/]", "Was [underline]here[/]!"); table.AddEmptyRow(); table.AddRow( "Lorem ipsum dolor sit amet, consectetur adipiscing elit,sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. " + "Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure " + "dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat " + - "non proident, sunt in culpa qui officia deserunt mollit anim id est laborum", "◀ Strange language"); + "non proident, sunt in culpa qui officia deserunt mollit anim id est laborum", "<- Strange language"); table.AddEmptyRow(); - table.AddRow("Hej 👋", "[green]Världen[/]"); + table.AddRow("Hej", "[green]Världen[/]"); AnsiConsole.Render(table); } @@ -81,7 +81,7 @@ namespace TableExample table.AddColumn(new TableColumn(new Panel("[u]Baz[/]").SetBorderColor(Color.Blue))); // Add some rows - table.AddRow(new Text("Hello").Centered(), new Markup("[red]World![/] 🌍"), Text.Empty); + table.AddRow(new Text("Hello").Centered(), new Markup("[red]World![/]"), Text.Empty); table.AddRow(second, new Text("Whaaat"), new Text("Lol")); table.AddRow(new Markup("[blue]Hej[/]").Centered(), new Markup("[yellow]Världen![/]"), Text.Empty); diff --git a/src/Spectre.Console.sln b/src/Spectre.Console.sln index 2778a43..04365e5 100644 --- a/src/Spectre.Console.sln +++ b/src/Spectre.Console.sln @@ -25,6 +25,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Grid", "..\examples\Grid\Gr EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Colors", "..\examples\Colors\Colors.csproj", "{1F51C55C-BA4C-4856-9001-0F7924FFB179}" EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Diagnostic", "..\examples\Diagnostic\Diagnostic.csproj", "{4337F255-88E9-4408-81A3-DF1AF58AC753}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -107,6 +109,18 @@ Global {1F51C55C-BA4C-4856-9001-0F7924FFB179}.Release|x64.Build.0 = Release|Any CPU {1F51C55C-BA4C-4856-9001-0F7924FFB179}.Release|x86.ActiveCfg = Release|Any CPU {1F51C55C-BA4C-4856-9001-0F7924FFB179}.Release|x86.Build.0 = Release|Any CPU + {4337F255-88E9-4408-81A3-DF1AF58AC753}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {4337F255-88E9-4408-81A3-DF1AF58AC753}.Debug|Any CPU.Build.0 = Debug|Any CPU + {4337F255-88E9-4408-81A3-DF1AF58AC753}.Debug|x64.ActiveCfg = Debug|Any CPU + {4337F255-88E9-4408-81A3-DF1AF58AC753}.Debug|x64.Build.0 = Debug|Any CPU + {4337F255-88E9-4408-81A3-DF1AF58AC753}.Debug|x86.ActiveCfg = Debug|Any CPU + {4337F255-88E9-4408-81A3-DF1AF58AC753}.Debug|x86.Build.0 = Debug|Any CPU + {4337F255-88E9-4408-81A3-DF1AF58AC753}.Release|Any CPU.ActiveCfg = Release|Any CPU + {4337F255-88E9-4408-81A3-DF1AF58AC753}.Release|Any CPU.Build.0 = Release|Any CPU + {4337F255-88E9-4408-81A3-DF1AF58AC753}.Release|x64.ActiveCfg = Release|Any CPU + {4337F255-88E9-4408-81A3-DF1AF58AC753}.Release|x64.Build.0 = Release|Any CPU + {4337F255-88E9-4408-81A3-DF1AF58AC753}.Release|x86.ActiveCfg = Release|Any CPU + {4337F255-88E9-4408-81A3-DF1AF58AC753}.Release|x86.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -116,6 +130,7 @@ Global {BFF37228-B376-4ADD-9657-4E501F929713} = {F0575243-121F-4DEE-9F6B-246E26DC0844} {C7FF6FDB-FB59-4517-8669-521C96AB7323} = {F0575243-121F-4DEE-9F6B-246E26DC0844} {1F51C55C-BA4C-4856-9001-0F7924FFB179} = {F0575243-121F-4DEE-9F6B-246E26DC0844} + {4337F255-88E9-4408-81A3-DF1AF58AC753} = {F0575243-121F-4DEE-9F6B-246E26DC0844} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {5729B071-67A0-48FB-8B1B-275E6822086C} diff --git a/src/Spectre.Console/Capabilities.cs b/src/Spectre.Console/Capabilities.cs index 9232813..7de2932 100644 --- a/src/Spectre.Console/Capabilities.cs +++ b/src/Spectre.Console/Capabilities.cs @@ -38,6 +38,17 @@ namespace Spectre.Console LegacyConsole = legacyConsole; } + /// + /// Checks whether the current capabilities supports + /// the specified color system. + /// + /// The color system to check. + /// true if the color system is supported, otherwise false. + public bool Supports(ColorSystem colorSystem) + { + return (int)colorSystem <= (int)ColorSystem; + } + /// public override string ToString() { diff --git a/src/Spectre.Console/Internal/AnsiConsoleRenderer.cs b/src/Spectre.Console/Internal/AnsiConsoleRenderer.cs index a2b3c2e..840a9c7 100644 --- a/src/Spectre.Console/Internal/AnsiConsoleRenderer.cs +++ b/src/Spectre.Console/Internal/AnsiConsoleRenderer.cs @@ -21,7 +21,7 @@ namespace Spectre.Console.Internal { if (_out.IsStandardOut()) { - return System.Console.BufferWidth; + return ConsoleHelper.GetSafeBufferWidth(Constants.DefaultBufferWidth); } return Constants.DefaultBufferWidth; @@ -34,7 +34,7 @@ namespace Spectre.Console.Internal { if (_out.IsStandardOut()) { - return System.Console.BufferHeight; + return ConsoleHelper.GetSafeBufferHeight(Constants.DefaultBufferHeight); } return Constants.DefaultBufferHeight; diff --git a/src/Spectre.Console/Internal/Extensions/ConsoleExtensions.cs b/src/Spectre.Console/Internal/Extensions/AnsiConsoleExtensions.cs similarity index 98% rename from src/Spectre.Console/Internal/Extensions/ConsoleExtensions.cs rename to src/Spectre.Console/Internal/Extensions/AnsiConsoleExtensions.cs index 7f653f9..05bf631 100644 --- a/src/Spectre.Console/Internal/Extensions/ConsoleExtensions.cs +++ b/src/Spectre.Console/Internal/Extensions/AnsiConsoleExtensions.cs @@ -3,7 +3,7 @@ using System.Diagnostics.CodeAnalysis; namespace Spectre.Console.Internal { - internal static class ConsoleExtensions + internal static class AnsiConsoleExtensions { public static IDisposable PushStyle(this IAnsiConsole console, Style style) { diff --git a/src/Spectre.Console/Internal/FallbackConsoleRenderer.cs b/src/Spectre.Console/Internal/FallbackConsoleRenderer.cs index edd1585..1775744 100644 --- a/src/Spectre.Console/Internal/FallbackConsoleRenderer.cs +++ b/src/Spectre.Console/Internal/FallbackConsoleRenderer.cs @@ -24,7 +24,7 @@ namespace Spectre.Console.Internal { if (_out.IsStandardOut()) { - return System.Console.BufferWidth; + return ConsoleHelper.GetSafeBufferWidth(Constants.DefaultBufferWidth); } return Constants.DefaultBufferWidth; @@ -37,7 +37,7 @@ namespace Spectre.Console.Internal { if (_out.IsStandardOut()) { - return System.Console.BufferHeight; + return ConsoleHelper.GetSafeBufferHeight(Constants.DefaultBufferHeight); } return Constants.DefaultBufferHeight; diff --git a/src/Spectre.Console/Internal/Utilities/ConsoleHelper.cs b/src/Spectre.Console/Internal/Utilities/ConsoleHelper.cs new file mode 100644 index 0000000..54dadc5 --- /dev/null +++ b/src/Spectre.Console/Internal/Utilities/ConsoleHelper.cs @@ -0,0 +1,44 @@ +using System.IO; +using System.Runtime.InteropServices; + +namespace Spectre.Console.Internal +{ + internal static class ConsoleHelper + { + public static int GetSafeBufferWidth(int defaultValue = Constants.DefaultBufferWidth) + { + try + { + var width = System.Console.BufferWidth; + if (width == 0) + { + width = defaultValue; + } + + return width; + } + catch (IOException) + { + return defaultValue; + } + } + + public static int GetSafeBufferHeight(int defaultValue = Constants.DefaultBufferWidth) + { + try + { + var height = System.Console.BufferHeight; + if (height == 0) + { + height = defaultValue; + } + + return height; + } + catch (IOException) + { + return defaultValue; + } + } + } +}