recrownedgtk/RecrownedGTK.Tools/ConsoleInterface/ConsoleUtilities.cs
Harrison fa74bc9ae5 Switched to dependency injection for console use.
Created interfaces for input and output for better structure and easier testing.
2020-02-23 20:51:39 -05:00

40 lines
1.2 KiB
C#

using System;
using System.Text;
namespace RecrownedGTK.Tools.ConsoleInterface
{
internal class ConsoleUtilities
{
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(' ');
}
}
stringBuilder.AppendLine();
return stringBuilder.ToString();
}
}
}