在.net生态系统中,控制台程序的表现通常不如gui应用那么引人注目,它们经常被用作演示程序。然而,现在是时候给予控制台应用程序应有的重视了。
随着终端技术的发展,增强用户体验的机会也随之增加。ITerm2、Hyper和Windows Terminal等工具已经为单调的控制台体验增添了趣味。虽然这些工具允许用户定制体验,但开发人员仍希望在控制台应用程序中加入更多编程风格。
在本文中,我们将探讨如何通过一些优秀的开源项目来增强我们的控制台程序的趣味性。以下介绍的项目顺序并不代表其优劣,它们都是改善控制台程序体验的优秀选择。
Colorful.Console是一个NuGet包,它可以增强我们对控制台输出文字样式的控制。我们可以使用System.Drawing.Color中定义的颜色来定义控制台程序的配色方案。
using System;
using System.Drawing;
using Console = Colorful.Console;
...
Console.WriteLine("console in pink", Color.Pink);
Console.WriteLine("console in default");
此外,Colorful.Console还允许我们使用FIGlet字体编写带颜色的ASCII码输出。
FigletFont font = FigletFont.Load("chunky.flf");
Figlet figlet = new Figlet(font);
Console.WriteLine(figlet.ToAscii("Belvedere"), ColorTranslator.FromHtml("#8AFFEF"));
Console.WriteLine(figlet.ToAscii("ice"), ColorTranslator.FromHtml("#FAD6FF"));
Console.WriteLine(figlet.ToAscii("cream."), ColorTranslator.FromHtml("#B8DBFF"));
这个输出结果完全就是黑客的梦想。我建议你访问colorful.console的官方网站,了解这个库能实现的所有效果,以更好地改善控制台程序的体验。
ConsoleTables是我(作者)自己编写的包,这里有点自卖自夸^.^。使用这个库,开发人员可以轻松地将一组对象以表格形式展示在控制台中。
static void Main(String[] args){
var table = new ConsoleTable("one", "two", "three");
table.AddRow(1, 2, 3)
.AddRow("this line should be longer", "yes it is", "oh");
table.Write();
Console.WriteLine();
var rows = Enumerable.Repeat(new Something(), 10);
ConsoleTable
.From<Something>(rows)
.Configure(o => o.NumberAlignment = Alignment.Right)
.Write(Format.Alternative);
Console.ReadKey();
}以前,谁不希望能在控制台中输出一个表格呢?
FORMAT: Default: -------------------------------------------------- | one | two | three | -------------------------------------------------- | 1 | 2 | 3 | -------------------------------------------------- | this line should be longer | yes it is | oh | -------------------------------------------------- Count: 2 FORMAT: Alternative: +----------------------------+-----------+-------+ | one | two | three | +----------------------------+-----------+-------+ | 1 | 2 | 3 | +----------------------------+-----------+-------+ | this line should be longer | yes it is | oh | +----------------------------+-----------+-------+
自从ConsoleTables发布以来,许多开发人员已经开发出自己的控制台表格库。有些甚至更好,你可以自行查找。
ShellProgressBar和需要其他应用程序一样,控制台程序也可以执行长时间任务。ShellProgressBar是一个非常棒的库,使用它,你可以在控制台输出一些非常惊艳的进度条。而且,ShellProgressBar支持进度条的嵌套使用。例如,如下GIF动画中展示的效果。

ShellProgressBar使用起来非常直接。
const int totalTicks = 10;
var options = new ProgressBarOptions{
ProgressCharacter = '─',
ProgressBarOnBottom = true};
using (var pbar = new ProgressBar(totalTicks, "Initial message", options)){
pbar.Tick(); //will advance pbar to 1 out of 10.
//we can also advance and update the progressbar text
pbar.Tick("Step 2 of 10");
}谢谢你,Martijin Larrman,这真的是一个非常好用的库。
GUI.CS是一个非常棒的控制台UI工具包。它提供了一个功能完善的工具箱,开发人员可以使用它构建早期控制台常见的一种用户界面。

这个UI工具箱提供了如下控件:
使用它,开发人员可以在控制台应用中实现一些令人难以置信的效果。这个库是由Miguel De Icaza编写的,是控制台技术的巅峰之作,下面让我们一起来看一个实例程序。
using Terminal.Gui;
class Demo {
static void Main ()
{
Application.Init ();
var top = Application.Top;
// 创建顶级窗体
var win = new Window ("MyApp") {
X = 0,
Y = 1, // 预留菜单行
// 使用Dim.Fill(), 它可以自动调整窗体大小,实现自适应,而无需手动调整
Width = Dim.Fill (),
Height = Dim.Fill ()
};
top.Add (win);
// 创建一个菜单
var menu = new MenuBar (new MenuBarItem [] {
new MenuBarItem ("_File", new MenuItem [] {
new MenuItem ("_New", "Creates new file", NewFile),
new MenuItem ("_Close", "", () => Close ()),
new MenuItem ("_Quit", "", () => { if (Quit ()) top.Running = false; })
}),
new MenuBarItem ("_Edit", new MenuItem [] {
new MenuItem ("_Copy", "", null),
new MenuItem ("C_ut", "", null),
new MenuItem ("_Paste", "", null)
})
});
top.Add (menu);
var login = new Label ("Login: ") { X = 3, Y = 2 };
var password = new Label ("Password: ") {
X = Pos.Left (login),
Y = Pos.Top (login) + 1
};
var loginText = new TextField ("") {
X = Pos.Right (password),
Y = Pos.Top (login),
Width = 40
};
var passText = new TextField ("") {
Secret = true,
X = Pos.Left (loginText),
Y = Pos.Top (password),
Width = Dim.Width (loginText)
};
// 添加一些其他控件
win.Add (
// 这是我最喜欢的布局
login, password, loginText, passText,
// 这里使用了绝对定位
new CheckBox (3, 6, "Remember me"),
new RadioGroup (3, 8, new [] { "_Personal", "_Company" }),
new Button (3, 14, "Ok"),
new Button (10, 14, "Cancel"),
new Label (3, 18, "Press F9 or ESC plus 9 to activate the menubar")
);
Application.Run ();
}
}总结:作为开发人员,我们可以沉迷于GUI,这是理所当然的,它使我们更有生产力。但是控制台应用程序同样也很强大。下次当你编写控制台程序的时候,你可以考虑使用以上介绍的某些库,以便为你的控制台应用增添色彩。
以上就是如何提升.NET控制台应用体验?的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号