116 lines
3.7 KiB
C#
116 lines
3.7 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
|
|
using Spectre.Console;
|
|
|
|
namespace SpectreConsoleStudy.Shared
|
|
{
|
|
/// <summary>
|
|
/// 执行器
|
|
/// </summary>
|
|
public class SpectreConsoleRunner
|
|
{
|
|
private static List<Menu> Menus = new List<Menu>()
|
|
{
|
|
new Menu("入门示例",100,"入门示例",0,Hello),
|
|
|
|
new Menu("提示组件",1,"文本提示",1,Hello),
|
|
new Menu("提示组件",1,"选择",2,Hello),
|
|
new Menu("提示组件",1,"多选",3,Hello),
|
|
|
|
new Menu("实时组件",20,"实时展示",1,Hello),
|
|
new Menu("实时组件",20,"进度条",2,Hello),
|
|
new Menu("实时组件",20,"状态条",3,UseStatus.Run),
|
|
|
|
new Menu("小组件",30,"面板",1,Hello),
|
|
new Menu("小组件",30,"桌面",2,Hello),
|
|
new Menu("小组件",30,"日历",3,Hello),
|
|
new Menu("小组件",30,"网格",4,Hello),
|
|
new Menu("小组件",30,"布局",5,Hello),
|
|
|
|
new Menu("符录",40,"颜色",10,Hello),
|
|
new Menu("符录",40,"风格",0,Hello),
|
|
new Menu("符录",40,"边框",0,Hello),
|
|
new Menu("符录",40,"微调器",0,Hello),
|
|
};
|
|
|
|
/// <summary>
|
|
/// 启动执行
|
|
/// </summary>
|
|
public static void Run()
|
|
{
|
|
Welcome();
|
|
|
|
RunMenu();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 欢迎信息
|
|
/// </summary>
|
|
public static void Welcome()
|
|
{
|
|
var hello = new Panel("此为开源多彩控制台项目:[red] Spectre.Console [/]的学习、演示程序。请选择模块,查询效果。")
|
|
.Header(new PanelHeader("[yellow3] 多彩控制台,[/][red]欢迎您 [/]", Justify.Center))
|
|
.Border(BoxBorder.Ascii)
|
|
.BorderStyle(Style.Parse("green"))
|
|
.Padding(new Padding(2, 1))
|
|
.Expand()
|
|
;
|
|
|
|
AnsiConsole.Write(hello);
|
|
AnsiConsole.WriteLine();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 执行菜单
|
|
/// </summary>
|
|
public static void RunMenu()
|
|
{
|
|
//声明多选
|
|
var multiSelect = new MultiSelectionPrompt<Menu>()
|
|
.PageSize(15)
|
|
//.Title("您想运行哪些 [green]示例[/] ?")
|
|
//.MoreChoicesText("[grey](按上下键以显示更选项)[/]")
|
|
.InstructionsText("[grey](按 [blue]<space> 键[/] 切换选中状态, 按[green]<enter> 键[/] 执行选择项)[/]");
|
|
|
|
//添加选项
|
|
var menuGroups = Menus.OrderByDescending(x => x.GroupOrder).GroupBy(x => x.Group);
|
|
foreach (var group in menuGroups)
|
|
{
|
|
int itemNuber = group.Count();
|
|
if (itemNuber == 1)
|
|
{
|
|
multiSelect.AddChoice(group.First());
|
|
}
|
|
else if (itemNuber > 1)
|
|
{
|
|
var menuList = group.OrderByDescending(x => x.Order).ToList();
|
|
var firstMenu = menuList.First();
|
|
multiSelect.AddChoiceGroup(firstMenu, menuList);
|
|
}
|
|
}
|
|
|
|
//展示
|
|
var menus = AnsiConsole.Prompt(multiSelect);
|
|
|
|
//执行选中项
|
|
foreach (var menu in menus)
|
|
{
|
|
menu?.Runner(menu);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 执行模块(start)
|
|
/// </summary>
|
|
/// <param name="menu"></param>
|
|
private static void Hello(Menu menu)
|
|
{
|
|
Rule rule = new Rule(menu.ToString());
|
|
AnsiConsole.Write(rule);
|
|
AnsiConsole.WriteLine();
|
|
}
|
|
}
|
|
}
|