Compare commits

...

2 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
4 changed files with 59 additions and 9 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
@ -149,20 +151,37 @@
////panel.Padding(2, 1);
////AnsiConsole.Write(panel);
#endregion
// Lesson 9 - FIGlet Text
AnsiConsole.Write(new FigletText("Hello")
.Centered()
.Color(Color.Red)
);
////AnsiConsole.Write(new FigletText("Hello")
//// .Centered()
//// .Color(Color.Red)
////);
FigletText figlet = new("World");
figlet.Centered();
figlet.Color(Color.Red);
////FigletText figlet = new("World");
////figlet.Centered();
////figlet.Color(Color.Red);
AnsiConsole.Write(figlet);
////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/