PromptPlus 是一個(gè)專(zhuān)注于 .NET Core 的命令行工具包,它為開(kāi)發(fā)者提供了一套豐富的控件和強(qiáng)大的命令,用來(lái)創(chuàng)建專(zhuān)業(yè)的交互式的命令行應(yīng)用程序。這個(gè)庫(kù)簡(jiǎn)化了命令行界面(CLI)應(yīng)用的開(kāi)發(fā)過(guò)程,使得開(kāi)發(fā)者可以更容易地添加各種輸入控件和增強(qiáng)用戶(hù)體驗(yàn)。此庫(kù)是使用C#開(kāi)發(fā)的,目標(biāo)平臺(tái)支持 netstandard2.1, .NET 6 , .NET 7 和 .NET 8。
主要功能和特點(diǎn):
多樣的輸入控件:
支持多種類(lèi)型的用戶(hù)輸入,如文本、密碼、選擇列表、復(fù)選框、滑塊等。
提供更復(fù)雜的控件,例如日歷、表格、文件瀏覽器等。
高度定制化:
開(kāi)發(fā)者可以根據(jù)需要自定義每個(gè)控件的外觀和行為,包括顏色、樣式以及驗(yàn)證規(guī)則。
支持綁定數(shù)據(jù)源到控件,實(shí)現(xiàn)動(dòng)態(tài)內(nèi)容加載。
跨平臺(tái)支持:
由于是基于 .NET Core 構(gòu)建,因此可以在 Windows、Linux 和 macOS 上運(yùn)行。
異步操作:
允許使用異步方法處理長(zhǎng)時(shí)間運(yùn)行的任務(wù)而不阻塞用戶(hù)界面。
事件驅(qū)動(dòng)編程模型:
支持通過(guò)事件監(jiān)聽(tīng)用戶(hù)交互,如按鍵、選擇變化等,從而做出響應(yīng)。
國(guó)際化與本地化:
內(nèi)置對(duì)多語(yǔ)言的支持,方便創(chuàng)建面向全球用戶(hù)的 CLI 應(yīng)用。
易于集成:
可以輕松地將 PromptPlus 集成到現(xiàn)有的 .NET Core 項(xiàng)目中。
文檔和社區(qū)支持:
提供詳細(xì)的官方文檔和活躍的開(kāi)源社區(qū),有助于解決問(wèn)題并獲取最佳實(shí)踐。
安裝及使用:
1、使用命令進(jìn)行安裝:
Install-Package PromptPlus 或者
dotnet add package PromptPlus
2、使用VS的Nuget包管理器直接搜索添加引用
安裝完成后,按照官方文檔中的指導(dǎo),在代碼里引入相應(yīng)的命名空間,并根據(jù)需求創(chuàng)建和配置所需的控件。
部分控件示例:
Banner
![](/files/attmgn/2025/1/admin20250122170938681_1.jpg)
![](/files/attmgn/2025/1/admin20250122170938700_2.jpg)
條形圖
![](/files/attmgn/2025/1/admin20250122170938839_3.jpg)
進(jìn)度條
![](/files/attmgn/2025/1/admin20250122170938875_4.jpg)
日歷
![](/files/attmgn/2025/1/admin20250122170938945_5.jpg)
輸入及驗(yàn)證
![](/files/attmgn/2025/1/admin20250122170938997_6.jpg)
開(kāi)關(guān)
![](/files/attmgn/2025/1/admin20250122170939034_7.jpg)
表格
![](/files/attmgn/2025/1/admin20250122170939066_8.jpg)
代碼編寫(xiě)支持流式編程,比如以下代碼:
PromptPlus.Setup((cfg) =>
{
cfg.PadLeft = 2;
cfg.PadRight = 2;
cfg.Culture = new CultureInfo("en-us");
cfg.BackgroundColor = ConsoleColor.Blue;
});
PromptPlus.Join()
.SingleDash($"[yellow]Console Information[/]", DashOptions.DoubleBorder, 1 );
.WriteLine($"IsTerminal: {PromptPlus.IsTerminal}");
.WriteLine($"IsUnicodeSupported: {PromptPlus.IsUnicodeSupported}");
.WriteLine($"OutputEncoding: {PromptPlus.OutputEncoding.EncodingName}");
.WriteLine($"ColorDepth: {PromptPlus.ColorDepth}");
.WriteLine($"BackgroundColor: {PromptPlus.BackgroundColor}");
.WriteLine($"ForegroundColor: {PromptPlus.ForegroundColor}");
.WriteLine($"SupportsAnsi: {PromptPlus.SupportsAnsi}");
.WriteLine($"Buffers(Width/Height): {PromptPlus.BufferWidth}/{PromptPlus.BufferHeight}");
.WriteLine($"PadScreen(Left/Right): {PromptPlus.PadLeft}/{PromptPlus.PadRight}\n");
PromptPlus
.KeyPress()
.Config(cfg =>
{
cfg.HideAfterFinish(true)
.ShowTooltip(false)
.ApplyStyle(StyleControls.Tooltips,Style.Plain.Foreground(Color.Grey100));
})
.Spinner(SpinnersType.Balloon)
.Run();
運(yùn)行效果如下:
![](/files/attmgn/2025/1/admin20250122170939117_9.jpg)
PromptPlus支持4/8/24位顏色,在代碼中可以使用以下方式編寫(xiě):
直接指定顏色
PromptPlus.WriteLine("[RGB(255,0,0) ON WHITE]Test[YELLOW] COLOR [/] BACK COLOR [/] other text");
PromptPlus.WriteLine("[RGB(255,0,0):WHITE]Test[YELLOW] COLOR [/] BACK COLOR [/] other text");
PromptPlus.WriteLine("[#ff0000 ON WHITE]Test [YELLOW] COLOR [/] BACK COLOR [/] other text");
PromptPlus.WriteLine("[RED ON WHITE]Test[YELLOW] COLOR [/] BACK COLOR [/] other text");
PromptPlus.WriteLine("[RED:WHITE]Test[YELLOW] COLOR [/] BACK COLOR [/] other text");
通過(guò)Style指定
PromptPlus.WriteLine("Test", new Style(Color.White, Color.Red, Overflow.None));
PromptPlus.WriteLine("Test", new Style(new Color(255, 255, 255), Color.Red, Overflow.None));
PromptPlus.WriteLine("Test", new Style(Color.FromConsoleColor(ConsoleColor.White), Color.Red, Overflow.None));
PromptPlus.WriteLine("Test", new Style(Color.FromInt32(255), Color.Red, Overflow.None));
在控件上指定
PromptPlus
.Input("Input [blue]sample2[/]", "with [yellow]description[/]")
.Run();
PromptPlus支持兩種數(shù)據(jù)驗(yàn)證的書(shū)寫(xiě)方法private class MylCass
{
[Required(ErrorMessage = "{0} is required!")]
[MinLength(3, ErrorMessage = "Min. Length = 3.")]
[MaxLength(5, ErrorMessage = "Max. Length = 5.")]
[Display(Prompt ="My Input")]
public string MyInput { get; set; }
}
以及
PromptPlus
.Input("Input sample2", "import validator from decorate")
.Default(inst.Text)
.AddValidators(PromptValidators.ImportValidators(inst,x => x!.Text!))
.Run();
PromptPlus的開(kāi)源地址為:https://github.com/FRACerqueira/PromptPlus
官方文檔地址:https://fracerqueira.github.io/PromptPlus?
如果無(wú)法訪(fǎng)問(wèn)GitHub,可以查看以下文章
閱讀原文:原文鏈接
該文章在 2025/1/22 17:10:37 編輯過(guò)