mirror of
https://github.com/nsnail/spectre.console.git
synced 2025-06-19 13:28:16 +08:00
Add support for different spinners
This commit is contained in:

committed by
Patrik Svensson

parent
3c504155bc
commit
cbed41e637
@ -1,39 +1,31 @@
|
||||
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()
|
||||
public static void 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)
|
||||
foreach(var user in User.LoadUsers())
|
||||
{
|
||||
cards.Add(new Panel(GetCardContent(user))
|
||||
.Header($"{user.location.country}")
|
||||
.RoundedBorder().Expand());
|
||||
cards.Add(
|
||||
new Panel(GetCardContent(user))
|
||||
.Header($"{user.Country}")
|
||||
.RoundedBorder().Expand());
|
||||
}
|
||||
|
||||
// Render all cards in columns
|
||||
AnsiConsole.Render(new Columns(cards));
|
||||
}
|
||||
|
||||
private static string GetCardContent(dynamic user)
|
||||
private static string GetCardContent(User user)
|
||||
{
|
||||
var name = $"{user.name.first} {user.name.last}";
|
||||
var country = $"{user.location.city}";
|
||||
var name = $"{user.FirstName} {user.LastName}";
|
||||
var city = $"{user.City}";
|
||||
|
||||
return $"[b]{name}[/]\n[yellow]{country}[/]";
|
||||
return $"[b]{name}[/]\n[yellow]{city}[/]";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
89
examples/Columns/User.cs
Normal file
89
examples/Columns/User.cs
Normal file
@ -0,0 +1,89 @@
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace ColumnsExample
|
||||
{
|
||||
public sealed class User
|
||||
{
|
||||
public string FirstName { get; set; }
|
||||
public string LastName { get; set; }
|
||||
public string City { get; set; }
|
||||
public string Country { get; set; }
|
||||
|
||||
public static List<User> LoadUsers()
|
||||
{
|
||||
return new List<User>
|
||||
{
|
||||
new User
|
||||
{
|
||||
FirstName = "Andrea",
|
||||
LastName = "Johansen",
|
||||
City = "Hornbæk",
|
||||
Country = "Denmark",
|
||||
},
|
||||
new User
|
||||
{
|
||||
FirstName = "Brandon",
|
||||
LastName = "Cole",
|
||||
City = "Washington",
|
||||
Country = "United States",
|
||||
},
|
||||
new User
|
||||
{
|
||||
FirstName = "Patrik",
|
||||
LastName = "Svensson",
|
||||
City = "Stockholm",
|
||||
Country = "Sweden",
|
||||
},
|
||||
new User
|
||||
{
|
||||
FirstName = "Freya",
|
||||
LastName = "Thompson",
|
||||
City = "Rotorua",
|
||||
Country = "New Zealand",
|
||||
},
|
||||
new User
|
||||
{
|
||||
FirstName = "طاها",
|
||||
LastName = "رضایی",
|
||||
City = "اهواز",
|
||||
Country = "Iran",
|
||||
},
|
||||
new User
|
||||
{
|
||||
FirstName = "Yara",
|
||||
LastName = "Simon",
|
||||
City = "Develier",
|
||||
Country = "Switzerland",
|
||||
},
|
||||
new User
|
||||
{
|
||||
FirstName = "Giray",
|
||||
LastName = "Erbay",
|
||||
City = "Karabük",
|
||||
Country = "Turkey",
|
||||
},
|
||||
new User
|
||||
{
|
||||
FirstName = "Miodrag",
|
||||
LastName = "Schaffer",
|
||||
City = "Möckern",
|
||||
Country = "Germany",
|
||||
},
|
||||
new User
|
||||
{
|
||||
FirstName = "Carmela",
|
||||
LastName = "Lo Castro",
|
||||
City = "Firenze",
|
||||
Country = "Italy",
|
||||
},
|
||||
new User
|
||||
{
|
||||
FirstName = "Roberto",
|
||||
LastName = "Sims",
|
||||
City = "Mallow",
|
||||
Country = "Ireland",
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user