前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >如何提升.NET控制台应用体验?

如何提升.NET控制台应用体验?

作者头像
李明成
发布2020-03-18 11:59:08
3830
发布2020-03-18 11:59:08
举报
文章被收录于专栏:dotNET知音dotNET知音dotNET知音

原文:Upgrade Your .NET Console App Experience 作者:Khalid Abuhakmeh 译文:Lamond Lu

在.NET生态系统中,控制台程序的表现相对较差。通常来说,这种项目经常作为Demo演示使用。现在是时候让控制台应用程序得到其应有的尊重了。

终端技术的发展开启了增强用户体验的复兴。ITerm2, Hyper, Windows Terminal,所有这些工具都为单调的控制台体验增加了一些趣味。虽然这些工具都允许用户定制自己体验,但是对于开发人员来说,他们还希望向控制台应用程序中添加一些编程风格。

在本篇博文中,我们将一起看一下如何使用一些出色的开源项目为我们的控制台程序增添趣味。这里说明的顺序并不表明项目的优劣,他们都是改善我们控制台程序体验的优秀方案。

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/

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发布以来,许多开发人员已经研发出自己的控制台表格库了。有一些甚至更好,你可以自行去查找一下。

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

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, 这是理所当然的,它试我们更有生产力。但是控制台应用程序同样也很强大。下次当你编写控制台程序的时候,你可以考虑使用以上介绍的某些库,以便为你的控制台应用增添色彩。

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2020-03-10,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 dotNET知音 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Colorful.Console
  • ConsoleTables
  • ShellProgressBar
  • GUI.CS
  • 总结
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档