mirror of
https://github.com/nsnail/spectre.console.git
synced 2025-04-20 02:32:50 +08:00
40 lines
1.1 KiB
C#
40 lines
1.1 KiB
C#
using System.Collections.Generic;
|
|
using System.Net.Http;
|
|
using System.Threading.Tasks;
|
|
using Newtonsoft.Json.Linq;
|
|
using Spectre.Console;
|
|
|
|
namespace ColumnsExample
|
|
{
|
|
public static class Program
|
|
{
|
|
public static async Task Main()
|
|
{
|
|
// Download some random users
|
|
using var client = new HttpClient();
|
|
dynamic users = JObject.Parse(
|
|
await client.GetStringAsync("https://randomuser.me/api/?results=15"));
|
|
|
|
// Create a card for each user
|
|
var cards = new List<Panel>();
|
|
foreach(var user in users.results)
|
|
{
|
|
cards.Add(new Panel(GetCard(user))
|
|
.SetHeader($"{user.location.country}")
|
|
.RoundedBorder().Expand());
|
|
}
|
|
|
|
// Render all cards in columns
|
|
AnsiConsole.Render(new Columns(cards));
|
|
}
|
|
|
|
private static string GetCard(dynamic user)
|
|
{
|
|
var name = $"{user.name.first} {user.name.last}";
|
|
var country = $"{user.location.city}";
|
|
|
|
return $"[b]{name}[/]\n[yellow]{country}[/]";
|
|
}
|
|
}
|
|
}
|