Compare commits

...

4 Commits

Author SHA1 Message Date
3c2d32d522 Extra kuriosa 2025-08-10 08:40:10 +02:00
fd2e74e658 Lesson 10 - Displaying Json 2025-08-09 09:14:32 +02:00
017aae6782 Lesson 9 -Figlets 2025-08-09 08:27:42 +02:00
5fb336b84a //Lesson 8 - Panels 2025-08-08 10:06:24 +02:00
4 changed files with 105 additions and 22 deletions

25
SpectreDemos/Helpers.cs Normal file
View File

@ -0,0 +1,25 @@
using Spectre.Console;
namespace SpectreDemos;
public static class Helpers
{
public static async Task<string> FetchApiDataAsync(string apiUrl)
{
using var client = new HttpClient();
try
{
HttpResponseMessage response = await client.GetAsync(apiUrl);
response.EnsureSuccessStatusCode();
return await response.Content.ReadAsStringAsync();
}
catch (Exception ex)
{
AnsiConsole.MarkupLine($"[red]Error fetching data from API:[/]{ex.Message}");
return string.Empty;
}
}
}

View File

@ -1,4 +1,6 @@
using Spectre.Console;
using Spectre.Console.Json;
using SpectreDemos;
#region "Previous Lessons"
//Lesson 2 - Initial Setup
@ -100,36 +102,86 @@
//// AnsiConsole.MarkupLine($"[bold red]{name}[/]");
////}
#endregion
// Lesson 7 - Adding tables to your console
List<Text> person = [
new("Bilbo"),
new("Baggins"),
new("111")
];
////List<Text> person = [
//// new("Bilbo"),
//// new("Baggins"),
//// new("111")
////];
Table table = new Table();
table.Centered();
//table.Expand();
//table.Border(TableBorder.Double);
table.Border(TableBorder.Rounded);
table.ShowRowSeparators();
////Table table = new Table();
////table.Centered();
//////table.Expand();
//////table.Border(TableBorder.Double);
////table.Border(TableBorder.Rounded);
////table.ShowRowSeparators();
table.AddColumn("First Name");
table.AddColumn("Last Name");
table.AddColumn("Age");
////table.AddColumn("First Name");
////table.AddColumn("Last Name");
////table.AddColumn("Age");
table.Columns[0].PadLeft(5).PadRight(5);
table.Columns[1].Width(15);
table.Columns[2].RightAligned();
////table.Columns[0].PadLeft(5).PadRight(5);
////table.Columns[1].Width(15);
////table.Columns[2].RightAligned();
table.AddRow("Tim", "Corey", "46");
table.AddRow("Sue", "Storm", "23");
table.AddRow(person);
////table.AddRow("Tim", "Corey", "46");
////table.AddRow("Sue", "Storm", "23");
////table.AddRow(person);
AnsiConsole.Write(table);
////AnsiConsole.Write(table);
//Lesson 8 - Panels
////List<string> names = [
//// "Tim Corey",
//// "Sue Storm",
//// "[red]Bilbo Baggins[/]",
//// "John Doe",
//// "Princess Donaut",
//// "Steve Rogers"
////];
////string panelInfo = string.Join("\n", names);
//////Panel panel = new Panel(panelInfo);
////Panel panel = new(new Markup(panelInfo).Centered());
////panel.Header = new PanelHeader("Default names");
////panel.Border = BoxBorder.Rounded;
////panel.Padding(2, 1);
////AnsiConsole.Write(panel);
// Lesson 9 - FIGlet Text
////AnsiConsole.Write(new FigletText("Hello")
//// .Centered()
//// .Color(Color.Red)
////);
////FigletText figlet = new("World");
////figlet.Centered();
////figlet.Color(Color.Red);
////AnsiConsole.Write(figlet);
#endregion
// Lesson 10 - Displaying Json
string jsonResponse = await Helpers.FetchApiDataAsync("https://thesampleapi.com/courses/12");
JsonText json = new JsonText(jsonResponse);
json.StringColor(Color.Yellow);
json.ColonColor(Color.Orange1);
AnsiConsole.Write(
new Panel(json)
.Header("API Results")
.Collapse()
.BorderColor(Color.White)
);
Console.ReadLine();
AnsiConsole.Clear();

View File

@ -9,6 +9,7 @@
<ItemGroup>
<PackageReference Include="Spectre.Console" Version="0.50.0" />
<PackageReference Include="Spectre.Console.Json" Version="0.50.0" />
</ItemGroup>
</Project>

View File

@ -0,0 +1,5 @@
L<EFBFBD>nk till Spectre-info.txt: https://spectreconsole.net/