diff --git a/README.md b/README.md index 6359458..7f0e794 100644 --- a/README.md +++ b/README.md @@ -13,8 +13,9 @@ for Python. 3. [Usage](#usage) 3.1. [Using the static API](#using-the-static-api) 3.2. [Creating a console](#creating-a-console) -4. [Available styles](#available-styles) -5. [Predefined colors](#predefined-colors) +4. [Running examples](#running-examples) +5. [Available styles](#available-styles) +6. [Predefined colors](#predefined-colors) ## Features @@ -84,6 +85,42 @@ when manually creating a console, remember that the user's terminal might not be able to use it, so unless you're creating an IAnsiConsole for testing, always use `ColorSystemSupport.Detect` and `AnsiSupport.Detect`._ +## Running examples + +To see Spectre.Console in action, install the +[dotnet-example](https://github.com/patriksvensson/dotnet-example) +global tool. + +``` +> dotnet tool install -g dotnet-example +``` + +Now you can list available examples in this repository: + +``` +> dotnet example + +Examples + +Colors Demonstrates how to use colors in the console. +Grid Demonstrates how to render grids in a console. +Panel Demonstrates how to render items in panels. +Table Demonstrates how to render tables in a console. +``` + +And to run an example: + +``` +> dotnet example table +┌──────────┬──────────┬────────┐ +│ Foo │ Bar │ Baz │ +├──────────┼──────────┼────────┤ +│ Hello │ World! │ │ +│ Bounjour │ le │ monde! │ +│ Hej │ Världen! │ │ +└──────────┴──────────┴────────┘ +``` + ## Available styles _NOTE: Not all styles are supported in every terminal._ diff --git a/examples/Colors/Colors.csproj b/examples/Colors/Colors.csproj new file mode 100644 index 0000000..0b87dc9 --- /dev/null +++ b/examples/Colors/Colors.csproj @@ -0,0 +1,14 @@ + + + + Exe + netcoreapp3.1 + false + Demonstrates how to use [yellow]c[/][red]o[/][green]l[/][blue]o[/][aqua]r[/][lime]s[/] in the console. + + + + + + + diff --git a/examples/Colors/Program.cs b/examples/Colors/Program.cs new file mode 100644 index 0000000..e514210 --- /dev/null +++ b/examples/Colors/Program.cs @@ -0,0 +1,78 @@ +using Spectre.Console; + +namespace ColorExample +{ + class Program + { + 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++) + { + AnsiConsole.Background = Color.FromInt32(i); + AnsiConsole.Write(string.Format(" {0,-9}", AnsiConsole.Background.ToString())); + AnsiConsole.ResetColors(); + if ((i + 1) % 8 == 0) + { + AnsiConsole.WriteLine(); + } + } + + ///////////////////////////////////////////////////////////////// + // 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(); + } + } + } + + ///////////////////////////////////////////////////////////////// + // 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/Colors/Utilities.cs b/examples/Colors/Utilities.cs new file mode 100644 index 0000000..3f32bcd --- /dev/null +++ b/examples/Colors/Utilities.cs @@ -0,0 +1,77 @@ +using System; +using Spectre.Console; + +namespace ColorExample +{ + public static class Utilities + { + // Borrowed from https://geekymonkey.com/Programming/CSharp/RGB2HSL_HSL2RGB.htm + public static Color HSL2RGB(double h, double sl, double l) + { + double v; + double r, g, b; + + r = l; // default to gray + g = l; + b = l; + v = (l <= 0.5) ? (l * (1.0 + sl)) : (l + sl - l * sl); + + if (v > 0) + { + double m; + double sv; + int sextant; + double fract, vsf, mid1, mid2; + + m = l + l - v; + sv = (v - m) / v; + h *= 6.0; + + sextant = (int)h; + fract = h - sextant; + vsf = v * sv * fract; + mid1 = m + vsf; + mid2 = v - vsf; + + switch (sextant) + { + case 0: + r = v; + g = mid1; + b = m; + break; + case 1: + r = mid2; + g = v; + b = m; + break; + case 2: + r = m; + g = v; + b = mid1; + break; + case 3: + r = m; + g = mid2; + b = v; + break; + case 4: + r = mid1; + g = m; + b = v; + break; + case 5: + r = v; + g = m; + b = mid2; + break; + } + } + + return new Color( + Convert.ToByte(r * 255.0f), + Convert.ToByte(g * 255.0f), + Convert.ToByte(b * 255.0f)); + } + } +} diff --git a/examples/Grid/Grid.csproj b/examples/Grid/Grid.csproj new file mode 100644 index 0000000..d8f9a16 --- /dev/null +++ b/examples/Grid/Grid.csproj @@ -0,0 +1,14 @@ + + + + Exe + netcoreapp3.1 + false + Demonstrates how to render grids in a console. + + + + + + + diff --git a/examples/Grid/Program.cs b/examples/Grid/Program.cs new file mode 100644 index 0000000..5cfabec --- /dev/null +++ b/examples/Grid/Program.cs @@ -0,0 +1,26 @@ +using System; +using Spectre.Console; + +namespace GridExample +{ + public sealed class Program + { + static void Main(string[] args) + { + AnsiConsole.WriteLine(); + AnsiConsole.MarkupLine("Usage: [grey]dotnet [blue]run[/] [[options] [[[[--] ...]][/]"); + AnsiConsole.WriteLine(); + + var grid = new Grid(); + grid.AddColumn(new GridColumn { NoWrap = true }); + grid.AddColumn(new GridColumn { NoWrap = true, Width = 2 }); + grid.AddColumn(); + grid.AddRow("Options:", "", ""); + grid.AddRow(" [blue]-h[/], [blue]--help[/]", "", "Show command line help."); + grid.AddRow(" [blue]-c[/], [blue]--configuration[/] ", "", "The configuration to run for."); + grid.AddRow(" [blue]-v[/], [blue]--verbosity[/] ", "", "Set the [grey]MSBuild[/] verbosity level."); + + AnsiConsole.Render(grid); + } + } +} diff --git a/examples/Panel/Panel.csproj b/examples/Panel/Panel.csproj new file mode 100644 index 0000000..2be9dc1 --- /dev/null +++ b/examples/Panel/Panel.csproj @@ -0,0 +1,14 @@ + + + + Exe + netcoreapp3.1 + false + Demonstrates how to render items in panels. + + + + + + + diff --git a/examples/Panel/Program.cs b/examples/Panel/Program.cs new file mode 100644 index 0000000..635be6d --- /dev/null +++ b/examples/Panel/Program.cs @@ -0,0 +1,50 @@ +using System; +using Spectre.Console; + +namespace PanelExample +{ + class Program + { + static void Main(string[] args) + { + var content = Text.New( + "[underline]I[/] heard [underline on blue]you[/] like 📦\n\n\n\n" + + "So I put a 📦 in a 📦\n\n" + + "😅", foreground: Color.White); + + AnsiConsole.Render( + new Panel( + new Panel(content) + { + Alignment = Justify.Center, + Border = BorderKind.Rounded + })); + + // Left adjusted panel with text + AnsiConsole.Render(new Panel( + Text.New("Left adjusted\nLeft")) + { + Expand = true, + Alignment = Justify.Left, + }); + + // Centered ASCII panel with text + AnsiConsole.Render(new Panel( + Text.New("Centered\nCenter")) + { + Expand = true, + Alignment = Justify.Center, + Border = BorderKind.Ascii, + }); + + // Right adjusted, rounded panel with text + AnsiConsole.Render(new Panel( + Text.New("Right adjusted\nRight")) + { + Expand = true, + Alignment = Justify.Right, + Border = BorderKind.Rounded, + }); + } + } +} diff --git a/examples/Table/Program.cs b/examples/Table/Program.cs new file mode 100644 index 0000000..d716359 --- /dev/null +++ b/examples/Table/Program.cs @@ -0,0 +1,55 @@ +using System; +using Spectre.Console; + +namespace TableExample +{ + class Program + { + static void Main(string[] args) + { + // A simple table§ + RenderSimpleTable(); + + // A big table + RenderBigTable(); + } + + private static void RenderSimpleTable() + { + // Create the table. + var table = new Table(); + table.AddColumn(new TableColumn("[u]Foo[/]")); + table.AddColumn(new TableColumn("[u]Bar[/]")); + table.AddColumn(new TableColumn("[u]Baz[/]")); + + // Add some rows + table.AddRow("Hello", "[red]World![/]", ""); + table.AddRow("[blue]Bounjour[/]", "[white]le[/]", "[red]monde![/]"); + table.AddRow("[blue]Hej[/]", "[yellow]Världen![/]", ""); + + AnsiConsole.Render(table); + } + + private static void RenderBigTable() + { + // Create the table. + var table = new Table { Border = BorderKind.Rounded }; + table.AddColumn("[red underline]Foo[/]"); + table.AddColumn(new TableColumn("[blue]Bar[/]") { Alignment = Justify.Right, NoWrap = true }); + + // Add some rows + 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"); + table.AddEmptyRow(); + table.AddRow("Hej 👋", "[green]Världen[/]"); + + AnsiConsole.Render(table); + } + } +} diff --git a/examples/Table/Table.csproj b/examples/Table/Table.csproj new file mode 100644 index 0000000..f61793f --- /dev/null +++ b/examples/Table/Table.csproj @@ -0,0 +1,14 @@ + + + + Exe + netcoreapp3.1 + false + Demonstrates how to render tables in a console. + + + + + + + diff --git a/src/Spectre.Console.Tests/Spectre.Console.Tests.csproj b/src/Spectre.Console.Tests/Spectre.Console.Tests.csproj index e77c363..ee1aa9b 100644 --- a/src/Spectre.Console.Tests/Spectre.Console.Tests.csproj +++ b/src/Spectre.Console.Tests/Spectre.Console.Tests.csproj @@ -2,7 +2,6 @@ netcoreapp3.1 - false diff --git a/src/Spectre.Console.sln b/src/Spectre.Console.sln index 6b86d1c..59650a1 100644 --- a/src/Spectre.Console.sln +++ b/src/Spectre.Console.sln @@ -17,6 +17,16 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution stylecop.json = stylecop.json EndProjectSection EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Examples", "Examples", "{F0575243-121F-4DEE-9F6B-246E26DC0844}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Table", "..\examples\Table\Table.csproj", "{94ECCBA8-7EBF-4B53-8379-52EB2327417E}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Panel", "..\examples\Panel\Panel.csproj", "{BFF37228-B376-4ADD-9657-4E501F929713}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Grid", "..\examples\Grid\Grid.csproj", "{C7FF6FDB-FB59-4517-8669-521C96AB7323}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Colors", "..\examples\Colors\Colors.csproj", "{1F51C55C-BA4C-4856-9001-0F7924FFB179}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -63,10 +73,64 @@ Global {272E6092-BD31-4EB6-A9FF-F4179F91958F}.Release|x64.Build.0 = Release|Any CPU {272E6092-BD31-4EB6-A9FF-F4179F91958F}.Release|x86.ActiveCfg = Release|Any CPU {272E6092-BD31-4EB6-A9FF-F4179F91958F}.Release|x86.Build.0 = Release|Any CPU + {94ECCBA8-7EBF-4B53-8379-52EB2327417E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {94ECCBA8-7EBF-4B53-8379-52EB2327417E}.Debug|Any CPU.Build.0 = Debug|Any CPU + {94ECCBA8-7EBF-4B53-8379-52EB2327417E}.Debug|x64.ActiveCfg = Debug|Any CPU + {94ECCBA8-7EBF-4B53-8379-52EB2327417E}.Debug|x64.Build.0 = Debug|Any CPU + {94ECCBA8-7EBF-4B53-8379-52EB2327417E}.Debug|x86.ActiveCfg = Debug|Any CPU + {94ECCBA8-7EBF-4B53-8379-52EB2327417E}.Debug|x86.Build.0 = Debug|Any CPU + {94ECCBA8-7EBF-4B53-8379-52EB2327417E}.Release|Any CPU.ActiveCfg = Release|Any CPU + {94ECCBA8-7EBF-4B53-8379-52EB2327417E}.Release|Any CPU.Build.0 = Release|Any CPU + {94ECCBA8-7EBF-4B53-8379-52EB2327417E}.Release|x64.ActiveCfg = Release|Any CPU + {94ECCBA8-7EBF-4B53-8379-52EB2327417E}.Release|x64.Build.0 = Release|Any CPU + {94ECCBA8-7EBF-4B53-8379-52EB2327417E}.Release|x86.ActiveCfg = Release|Any CPU + {94ECCBA8-7EBF-4B53-8379-52EB2327417E}.Release|x86.Build.0 = Release|Any CPU + {BFF37228-B376-4ADD-9657-4E501F929713}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {BFF37228-B376-4ADD-9657-4E501F929713}.Debug|Any CPU.Build.0 = Debug|Any CPU + {BFF37228-B376-4ADD-9657-4E501F929713}.Debug|x64.ActiveCfg = Debug|Any CPU + {BFF37228-B376-4ADD-9657-4E501F929713}.Debug|x64.Build.0 = Debug|Any CPU + {BFF37228-B376-4ADD-9657-4E501F929713}.Debug|x86.ActiveCfg = Debug|Any CPU + {BFF37228-B376-4ADD-9657-4E501F929713}.Debug|x86.Build.0 = Debug|Any CPU + {BFF37228-B376-4ADD-9657-4E501F929713}.Release|Any CPU.ActiveCfg = Release|Any CPU + {BFF37228-B376-4ADD-9657-4E501F929713}.Release|Any CPU.Build.0 = Release|Any CPU + {BFF37228-B376-4ADD-9657-4E501F929713}.Release|x64.ActiveCfg = Release|Any CPU + {BFF37228-B376-4ADD-9657-4E501F929713}.Release|x64.Build.0 = Release|Any CPU + {BFF37228-B376-4ADD-9657-4E501F929713}.Release|x86.ActiveCfg = Release|Any CPU + {BFF37228-B376-4ADD-9657-4E501F929713}.Release|x86.Build.0 = Release|Any CPU + {C7FF6FDB-FB59-4517-8669-521C96AB7323}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {C7FF6FDB-FB59-4517-8669-521C96AB7323}.Debug|Any CPU.Build.0 = Debug|Any CPU + {C7FF6FDB-FB59-4517-8669-521C96AB7323}.Debug|x64.ActiveCfg = Debug|Any CPU + {C7FF6FDB-FB59-4517-8669-521C96AB7323}.Debug|x64.Build.0 = Debug|Any CPU + {C7FF6FDB-FB59-4517-8669-521C96AB7323}.Debug|x86.ActiveCfg = Debug|Any CPU + {C7FF6FDB-FB59-4517-8669-521C96AB7323}.Debug|x86.Build.0 = Debug|Any CPU + {C7FF6FDB-FB59-4517-8669-521C96AB7323}.Release|Any CPU.ActiveCfg = Release|Any CPU + {C7FF6FDB-FB59-4517-8669-521C96AB7323}.Release|Any CPU.Build.0 = Release|Any CPU + {C7FF6FDB-FB59-4517-8669-521C96AB7323}.Release|x64.ActiveCfg = Release|Any CPU + {C7FF6FDB-FB59-4517-8669-521C96AB7323}.Release|x64.Build.0 = Release|Any CPU + {C7FF6FDB-FB59-4517-8669-521C96AB7323}.Release|x86.ActiveCfg = Release|Any CPU + {C7FF6FDB-FB59-4517-8669-521C96AB7323}.Release|x86.Build.0 = Release|Any CPU + {1F51C55C-BA4C-4856-9001-0F7924FFB179}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {1F51C55C-BA4C-4856-9001-0F7924FFB179}.Debug|Any CPU.Build.0 = Debug|Any CPU + {1F51C55C-BA4C-4856-9001-0F7924FFB179}.Debug|x64.ActiveCfg = Debug|Any CPU + {1F51C55C-BA4C-4856-9001-0F7924FFB179}.Debug|x64.Build.0 = Debug|Any CPU + {1F51C55C-BA4C-4856-9001-0F7924FFB179}.Debug|x86.ActiveCfg = Debug|Any CPU + {1F51C55C-BA4C-4856-9001-0F7924FFB179}.Debug|x86.Build.0 = Debug|Any CPU + {1F51C55C-BA4C-4856-9001-0F7924FFB179}.Release|Any CPU.ActiveCfg = Release|Any CPU + {1F51C55C-BA4C-4856-9001-0F7924FFB179}.Release|Any CPU.Build.0 = Release|Any CPU + {1F51C55C-BA4C-4856-9001-0F7924FFB179}.Release|x64.ActiveCfg = Release|Any CPU + {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 EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE EndGlobalSection + GlobalSection(NestedProjects) = preSolution + {94ECCBA8-7EBF-4B53-8379-52EB2327417E} = {F0575243-121F-4DEE-9F6B-246E26DC0844} + {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} + EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {5729B071-67A0-48FB-8B1B-275E6822086C} EndGlobalSection