在控制台上使用填充显示格式化文本 [英] Display formatted text on console using padding

查看:38
本文介绍了在控制台上使用填充显示格式化文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我计划以类似于以下格式的方式为我的控制台应用程序的参数编写一份说明:

The following options are possible:
    myOption:   Text do describe the option, but that should be splitted 
                to several lines if too big. Text should automatically 
                align by a fixed offset.

我已经有了一种在正确位置拆分文本的方法(假设我们不关心是否在任何单词的中间拆分,只有在我们真正关心在单词边界处拆分时才会牵涉到事情)。然而,我仍然坚持对选项的解释。

这是目前为止的代码:

public void DisplayHelpEx()
{
    var offset = this._options.Max(x => x.ParameterName.Length) + 6;
    Console.WriteLine("The following options are possible:");
    foreach (var option in this._corrections)
    {
        Console.Write((option.ParameterName + ": ").PadLeft(offset));
        WriteOffset(offset, option.Explanation);
    }
}

public void WriteOffset(int offset, string text)
{
    var numChars = TOTAL_NUMBER_CHARS_PER_LINE - offset;
    string line;

    while ((line = new String(text.Take(numChars).ToArray())).Any())
    {
        var s = line.PadLeft(numChars);
        Console.Write(s);
        Console.WriteLine();
        text= new String(text.Skip(numChars).ToArray());
    }
}

我尝试了许多.PadLeft.PadRight的组合,但都不能正常工作。

使用上面的方法,我得到以下输出:

The following options are possible:
  myOption: Text do describe the option, but that should be splitted 
to several lines if too big. Text should automatically 
                        align by a fixed offset.

推荐答案

PadLeft采用文本并向左或向右添加一些空格,以使全文具有定义的宽度,请参见https://msdn.microsoft.com/en-us/library/system.string.padleft(v=vs.110).aspx

然而,在您的例子中,您不希望整个文本具有固定的宽度(特别是如果您将来想要在单词边界处很好地拆分),而是让偏移量位于开头。那么为什么不像这样在每一行的开头添加偏移量空格呢?

private const string optionParameterName = "myOption";

private const string optionText =
  "Text do describe the option, but that should be splitted to several lines if too big.Text should automatically align by a fixed offset.";

private const int TOTAL_NUMBER_CHARS_PER_LINE = 60;

public void DisplayHelpEx()
{
  var offset = optionParameterName.Length + 6;
  Console.WriteLine("The following options are possible:");
  WriteOffset(offset, optionParameterName + ": ", optionText);
}

public void WriteOffset(int offset, string label, string text)
{
  var numChars = TOTAL_NUMBER_CHARS_PER_LINE - offset;
  string offsetString = new string(' ', offset);
  string line;

  bool firstLine = true;

  while ((line = new String(text.Take(numChars).ToArray())).Any())
  {
    if (firstLine)
    {
      Console.Write(label.PadRight(offset));
    }
    else
    {
      Console.Write(offsetString);
    }
    firstLine = false;

    Console.Write(line);
    Console.WriteLine();

    text = new String(text.Skip(numChars).ToArray());
  }
}

// output:
// The following options are possible:
// myOption:     Text do describe the option, but that should b
//               e splitted to several lines if too big.Text sh
//               ould automatically align by a fixed offset.

请注意,我在第一行使用了Label.PadRight(偏移量),以确保将带有标签的字符串填充到正确的长度--在这里,填充很有用,因为它允许我们使标签字符串具有与其他偏移量完全相同的宽度。

这篇关于在控制台上使用填充显示格式化文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆