
在.NET 生態系統中,主控台程式的表現相對較差。通常來說,這種專案經常作為 Demo 示範使用。現在是時候讓主控台應用程式得到其應有的尊重了。
終端技術的發展開啟了增強使用者體驗的復興。 ITerm2, Hyper, Windows Terminal,所有這些工具都為單調的主控台體驗增加了一些趣味。雖然這些工具都允許使用者自訂自己體驗,但是對於開發人員來說,他們還希望向主控台應用程式中添加一些程式設計風格。
在本篇博文中,我們將一起看一下如何使用一些出色的開源專案為我們的主控台程式增添趣味。這裡說明的順序並不表明專案的優劣,他們都是改善我們主控台程式體驗的優秀方案。
1. Colorful.Console
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 碼輸出
- FIGLet: http://www.figlet.org/
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的官方站點,了解這個函式庫能實現的所有效果,以便更好的改善主控台程式的體驗。
- Colorful.Console: http://colorfulconsole.com/
2. ConsoleTables
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發布以來,許多開發人員已經研發出自己的主控台表格函式庫了。有一些甚至更好,你可以自行去查找一下。
3. 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, 這真的是一個非常好用的函式庫。
4. GUI.CS
GUI.CS是一個非常棒的主控台 UI 工具包。它提供了一個功能完善的工具箱,開發人員可以使用它構建早期主控台常見的一種使用者介面。

這個 UI 工具箱提供了如下控制項:
- Buttons
- Labels
- Text Entry
- Text View
- User Inputs
- Windows
- Menus
- ScrollBars
使用它,開發人員可以在主控台應用程式中實現一些令人難以置信的效果。這個函式庫是由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, 這是理所當然的,它使我們更有生產力。但是主控台應用程式同樣也很強大。下次當你編寫主控台程式的時候,你可以考慮使用以上介紹的某些函式庫,以便為你的主控台應用增添色彩。
原文標題:Upgrade Your .NET Console App Experience
原文連結:https://khalidabuhakmeh.com/upgraded-dotnet-console-experience
原文作者:Khalid Abuhakmeh
譯文:Lamond Lu
本文轉載自部落格園(古道輕風):https://www.cnblogs.com/88223100/p/upgraded-dotnet-console-experience.html