C#Mono控制台:如何修复输出中的第一行



我想在Console中绘制一个表,所以当表中有很多信息(比如20多行)时,我会丢失表的标题(名称日期时间下载

如何修复3条第一行,以及

------------------------------------------------
Name    Date    Time    Download
------------------------------------------------
text1   28/10   13:11   not yet
text2   28/10   13:14   not yet
text3   28/10   13:19   not yet
text4   28/10   13:25   not yet
text5   28/10   13:45   not yet
text6   28/10   13:50   not yet
  .       .       .        .
  .       .       .        .
  .       .       .        .
text35  28/10   13:59   not yet

只需检查您的项目是否超过了可以显示的数量,如果是这样,请停止打印项目并打印空行。

这里有一个快速而肮脏的例子:

void Main()
{
    var input = Enumerable.Range(1, 35).Select(e => "Line: " + e).ToList();
    Console.WriteLine("------------------------------------------------");
    Console.WriteLine("THE HEADER");
    Console.WriteLine("------------------------------------------------");
    foreach (var element in GetLines(input))
        Console.WriteLine(element);
}
IEnumerable<string> GetLines(IList<string> input, int maxLines = 10)
{
    for (int i = 0; i < input.Count; i++)
    {
        yield return input[i];
        if (i == maxLines - 5 && input.Count > maxLines)
        {
            yield return " . . . .";
            yield return " . . . .";
            yield return " . . . .";
            yield return input[input.Count - 1];
            yield break;
        }
    }
}

它将打印

------------------------------------------------
THE HEADER
------------------------------------------------
Line: 1
Line: 2
Line: 3
Line: 4
Line: 5
Line: 6
 . . . .
 . . . .
 . . . .
Line: 35

最新更新