recrownedgtk/RecrownedGTK.Tools/ConsoleUtilities.cs

51 lines
1.5 KiB
C#
Raw Normal View History

2020-02-17 02:44:21 +00:00
using System;
using System.Text;
namespace RecrownedGTK.Tools
{
internal class ConsoleUtilities
{
public static void WriteWrapped(string message, bool line = false)
{
message = WrapMessageToConsoleWidth(message);
if (line) message = message + "\n";
Console.Write(message);
}
public static string WrapMessageToConsoleWidth(string message)
{
string[] words = message.Split(' ');
StringBuilder stringBuilder = new StringBuilder();
int currentLineSize = 0;
for (int i = 0; i < words.Length; i++)
{
if (currentLineSize + words[i].Length >= Console.BufferWidth - Console.CursorLeft -1)
{
currentLineSize = 0;
stringBuilder.AppendLine();
}
if (words[i].Contains("\n"))
{
currentLineSize = 0;
}
currentLineSize += words[i].Length + 1;
if (words[i].Contains("\n"))
{
currentLineSize -= 2;
}
stringBuilder.Append(words[i]);
if (i + 1 < words.Length)
{
stringBuilder.Append(' ');
}
}
return stringBuilder.ToString();
}
public static void WriteWrappedLine(string message)
{
WriteWrapped(message, true);
}
}
}